Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const Decimal128 = require('../types/decimal128');
  6. const ObjectId = require('../types/objectid');
  7. const utils = require('../utils');
  8. exports.flatten = flatten;
  9. exports.modifiedPaths = modifiedPaths;
  10. /*!
  11. * ignore
  12. */
  13. function flatten(update, path, options) {
  14. let keys;
  15. if (update && utils.isMongooseObject(update) && !Buffer.isBuffer(update)) {
  16. keys = Object.keys(update.toObject({ transform: false, virtuals: false }));
  17. } else {
  18. keys = Object.keys(update || {});
  19. }
  20. const numKeys = keys.length;
  21. const result = {};
  22. path = path ? path + '.' : '';
  23. for (let i = 0; i < numKeys; ++i) {
  24. const key = keys[i];
  25. const val = update[key];
  26. result[path + key] = val;
  27. if (shouldFlatten(val)) {
  28. if (options && options.skipArrays && Array.isArray(val)) {
  29. continue;
  30. }
  31. const flat = flatten(val, path + key, options);
  32. for (const k in flat) {
  33. result[k] = flat[k];
  34. }
  35. if (Array.isArray(val)) {
  36. result[path + key] = val;
  37. }
  38. }
  39. }
  40. return result;
  41. }
  42. /*!
  43. * ignore
  44. */
  45. function modifiedPaths(update, path, result) {
  46. const keys = Object.keys(update || {});
  47. const numKeys = keys.length;
  48. result = result || {};
  49. path = path ? path + '.' : '';
  50. for (let i = 0; i < numKeys; ++i) {
  51. const key = keys[i];
  52. let val = update[key];
  53. result[path + key] = true;
  54. if (utils.isMongooseObject(val) && !Buffer.isBuffer(val)) {
  55. val = val.toObject({ transform: false, virtuals: false });
  56. }
  57. if (shouldFlatten(val)) {
  58. modifiedPaths(val, path + key, result);
  59. }
  60. }
  61. return result;
  62. }
  63. /*!
  64. * ignore
  65. */
  66. function shouldFlatten(val) {
  67. return val &&
  68. typeof val === 'object' &&
  69. !(val instanceof Date) &&
  70. !(val instanceof ObjectId) &&
  71. (!Array.isArray(val) || val.length > 0) &&
  72. !(val instanceof Buffer) &&
  73. !(val instanceof Decimal128);
  74. }