modifiedPaths.js 845 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const _modifiedPaths = require('../common').modifiedPaths;
  3. /**
  4. * Given an update document with potential update operators (`$set`, etc.)
  5. * returns an object whose keys are the directly modified paths.
  6. *
  7. * If there are any top-level keys that don't start with `$`, we assume those
  8. * will get wrapped in a `$set`. The Mongoose Query is responsible for wrapping
  9. * top-level keys in `$set`.
  10. *
  11. * @param {Object} update
  12. * @return {Object} modified
  13. */
  14. module.exports = function modifiedPaths(update) {
  15. const keys = Object.keys(update);
  16. const res = {};
  17. const withoutDollarKeys = {};
  18. for (const key of keys) {
  19. if (key.startsWith('$')) {
  20. _modifiedPaths(update[key], '', res);
  21. continue;
  22. }
  23. withoutDollarKeys[key] = update[key];
  24. }
  25. _modifiedPaths(withoutDollarKeys, '', res);
  26. return res;
  27. };