lookupLocalFields.js 720 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. module.exports = function lookupLocalFields(cur, path, val) {
  3. if (cur == null) {
  4. return cur;
  5. }
  6. if (cur._doc != null) {
  7. cur = cur._doc;
  8. }
  9. if (arguments.length >= 3) {
  10. if (typeof cur !== 'object') {
  11. return void 0;
  12. }
  13. if (val === void 0) {
  14. return void 0;
  15. }
  16. if (cur instanceof Map) {
  17. cur.set(path, val);
  18. } else {
  19. cur[path] = val;
  20. }
  21. return val;
  22. }
  23. // Support populating paths under maps using `map.$*.subpath`
  24. if (path === '$*') {
  25. return cur instanceof Map ?
  26. Array.from(cur.values()) :
  27. Object.keys(cur).map(key => cur[key]);
  28. }
  29. if (cur instanceof Map) {
  30. return cur.get(path);
  31. }
  32. return cur[path];
  33. };