1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const Decimal = require('./types/decimal128');
|
8 | const ObjectId = require('./types/objectid');
|
9 | const PromiseProvider = require('./promise_provider');
|
10 | const cloneRegExp = require('regexp-clone');
|
11 | const get = require('./helpers/get');
|
12 | const sliced = require('sliced');
|
13 | const mpath = require('mpath');
|
14 | const ms = require('ms');
|
15 | const Buffer = require('safe-buffer').Buffer;
|
16 |
|
17 | const emittedSymbol = Symbol.for('mongoose:emitted');
|
18 |
|
19 | let MongooseBuffer;
|
20 | let MongooseArray;
|
21 | let Document;
|
22 |
|
23 | const specialProperties = new Set(['__proto__', 'constructor', 'prototype']);
|
24 |
|
25 | exports.specialProperties = specialProperties;
|
26 |
|
27 | /*!
|
28 | * Produces a collection name from model `name`. By default, just returns
|
29 | * the model name
|
30 | *
|
31 | * @param {String} name a model name
|
32 | * @param {Function} pluralize function that pluralizes the collection name
|
33 | * @return {String} a collection name
|
34 | * @api private
|
35 | */
|
36 |
|
37 | exports.toCollectionName = function(name, pluralize) {
|
38 | if (name === 'system.profile') {
|
39 | return name;
|
40 | }
|
41 | if (name === 'system.indexes') {
|
42 | return name;
|
43 | }
|
44 | if (typeof pluralize === 'function') {
|
45 | return pluralize(name);
|
46 | }
|
47 | return name;
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | exports.deepEqual = function deepEqual(a, b) {
|
62 | if (a === b) {
|
63 | return true;
|
64 | }
|
65 |
|
66 | if (a instanceof Date && b instanceof Date) {
|
67 | return a.getTime() === b.getTime();
|
68 | }
|
69 |
|
70 | if ((isBsonType(a, 'ObjectID') && isBsonType(b, 'ObjectID')) ||
|
71 | (isBsonType(a, 'Decimal128') && isBsonType(b, 'Decimal128'))) {
|
72 | return a.toString() === b.toString();
|
73 | }
|
74 |
|
75 | if (a instanceof RegExp && b instanceof RegExp) {
|
76 | return a.source === b.source &&
|
77 | a.ignoreCase === b.ignoreCase &&
|
78 | a.multiline === b.multiline &&
|
79 | a.global === b.global;
|
80 | }
|
81 |
|
82 | if (typeof a !== 'object' && typeof b !== 'object') {
|
83 | return a == b;
|
84 | }
|
85 |
|
86 | if (a === null || b === null || a === undefined || b === undefined) {
|
87 | return false;
|
88 | }
|
89 |
|
90 | if (a.prototype !== b.prototype) {
|
91 | return false;
|
92 | }
|
93 |
|
94 |
|
95 | if (a instanceof Number && b instanceof Number) {
|
96 | return a.valueOf() === b.valueOf();
|
97 | }
|
98 |
|
99 | if (Buffer.isBuffer(a)) {
|
100 | return exports.buffer.areEqual(a, b);
|
101 | }
|
102 |
|
103 | if (isMongooseObject(a)) {
|
104 | a = a.toObject();
|
105 | }
|
106 | if (isMongooseObject(b)) {
|
107 | b = b.toObject();
|
108 | }
|
109 |
|
110 | let ka;
|
111 | let kb;
|
112 | let key;
|
113 | let i;
|
114 | try {
|
115 | ka = Object.keys(a);
|
116 | kb = Object.keys(b);
|
117 | } catch (e) {
|
118 |
|
119 | return false;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 | if (ka.length !== kb.length) {
|
125 | return false;
|
126 | }
|
127 |
|
128 |
|
129 | ka.sort();
|
130 | kb.sort();
|
131 |
|
132 |
|
133 | for (i = ka.length - 1; i >= 0; i--) {
|
134 | if (ka[i] !== kb[i]) {
|
135 | return false;
|
136 | }
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 | for (i = ka.length - 1; i >= 0; i--) {
|
142 | key = ka[i];
|
143 | if (!deepEqual(a[key], b[key])) {
|
144 | return false;
|
145 | }
|
146 | }
|
147 |
|
148 | return true;
|
149 | };
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function isBsonType(obj, typename) {
|
156 | return get(obj, '_bsontype', void 0) === typename;
|
157 | }
|
158 |
|
159 | /*!
|
160 | * Get the last element of an array
|
161 | */
|
162 |
|
163 | exports.last = function(arr) {
|
164 | if (arr.length > 0) {
|
165 | return arr[arr.length - 1];
|
166 | }
|
167 | return void 0;
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | exports.clone = function clone(obj, options, isArrayChild) {
|
185 | if (obj == null) {
|
186 | return obj;
|
187 | }
|
188 |
|
189 | if (Array.isArray(obj)) {
|
190 | return cloneArray(obj, options);
|
191 | }
|
192 |
|
193 | if (isMongooseObject(obj)) {
|
194 | if (options && options.json && typeof obj.toJSON === 'function') {
|
195 | return obj.toJSON(options);
|
196 | }
|
197 | return obj.toObject(options);
|
198 | }
|
199 |
|
200 | if (obj.constructor) {
|
201 | switch (exports.getFunctionName(obj.constructor)) {
|
202 | case 'Object':
|
203 | return cloneObject(obj, options, isArrayChild);
|
204 | case 'Date':
|
205 | return new obj.constructor(+obj);
|
206 | case 'RegExp':
|
207 | return cloneRegExp(obj);
|
208 | default:
|
209 |
|
210 | break;
|
211 | }
|
212 | }
|
213 |
|
214 | if (obj instanceof ObjectId) {
|
215 | return new ObjectId(obj.id);
|
216 | }
|
217 | if (isBsonType(obj, 'Decimal128')) {
|
218 | if (options && options.flattenDecimals) {
|
219 | return obj.toJSON();
|
220 | }
|
221 | return Decimal.fromString(obj.toString());
|
222 | }
|
223 |
|
224 | if (!obj.constructor && exports.isObject(obj)) {
|
225 |
|
226 | return cloneObject(obj, options, isArrayChild);
|
227 | }
|
228 |
|
229 | if (obj.valueOf) {
|
230 | return obj.valueOf();
|
231 | }
|
232 |
|
233 | return cloneObject(obj, options, isArrayChild);
|
234 | };
|
235 | const clone = exports.clone;
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | exports.promiseOrCallback = function promiseOrCallback(callback, fn, ee) {
|
242 | if (typeof callback === 'function') {
|
243 | return fn(function(error) {
|
244 | if (error != null) {
|
245 | if (ee != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
|
246 | error[emittedSymbol] = true;
|
247 | ee.emit('error', error);
|
248 | }
|
249 | try {
|
250 | callback(error);
|
251 | } catch (error) {
|
252 | return process.nextTick(() => {
|
253 | throw error;
|
254 | });
|
255 | }
|
256 | return;
|
257 | }
|
258 | callback.apply(this, arguments);
|
259 | });
|
260 | }
|
261 |
|
262 | const Promise = PromiseProvider.get();
|
263 |
|
264 | return new Promise((resolve, reject) => {
|
265 | fn(function(error, res) {
|
266 | if (error != null) {
|
267 | if (ee != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
|
268 | error[emittedSymbol] = true;
|
269 | ee.emit('error', error);
|
270 | }
|
271 | return reject(error);
|
272 | }
|
273 | if (arguments.length > 2) {
|
274 | return resolve(Array.prototype.slice.call(arguments, 1));
|
275 | }
|
276 | resolve(res);
|
277 | });
|
278 | });
|
279 | };
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | function cloneObject(obj, options, isArrayChild) {
|
286 | const minimize = options && options.minimize;
|
287 | const ret = {};
|
288 | let hasKeys;
|
289 |
|
290 | for (const k in obj) {
|
291 | if (specialProperties.has(k)) {
|
292 | continue;
|
293 | }
|
294 |
|
295 |
|
296 | const val = clone(obj[k], options);
|
297 |
|
298 | if (!minimize || (typeof val !== 'undefined')) {
|
299 | hasKeys || (hasKeys = true);
|
300 | ret[k] = val;
|
301 | }
|
302 | }
|
303 |
|
304 | return minimize && !isArrayChild ? hasKeys && ret : ret;
|
305 | }
|
306 |
|
307 | function cloneArray(arr, options) {
|
308 | const ret = [];
|
309 | for (let i = 0, l = arr.length; i < l; i++) {
|
310 | ret.push(clone(arr[i], options, true));
|
311 | }
|
312 | return ret;
|
313 | }
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | exports.options = function(defaults, options) {
|
325 | const keys = Object.keys(defaults);
|
326 | let i = keys.length;
|
327 | let k;
|
328 |
|
329 | options = options || {};
|
330 |
|
331 | while (i--) {
|
332 | k = keys[i];
|
333 | if (!(k in options)) {
|
334 | options[k] = defaults[k];
|
335 | }
|
336 | }
|
337 |
|
338 | return options;
|
339 | };
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | exports.random = function() {
|
348 | return Math.random().toString().substr(3);
|
349 | };
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | exports.merge = function merge(to, from, options, path) {
|
360 | options = options || {};
|
361 |
|
362 | const keys = Object.keys(from);
|
363 | let i = 0;
|
364 | const len = keys.length;
|
365 | let key;
|
366 |
|
367 | path = path || '';
|
368 | const omitNested = options.omitNested || {};
|
369 |
|
370 | while (i < len) {
|
371 | key = keys[i++];
|
372 | if (options.omit && options.omit[key]) {
|
373 | continue;
|
374 | }
|
375 | if (omitNested[path]) {
|
376 | continue;
|
377 | }
|
378 | if (specialProperties.has(key)) {
|
379 | continue;
|
380 | }
|
381 | if (to[key] == null) {
|
382 | to[key] = from[key];
|
383 | } else if (exports.isObject(from[key])) {
|
384 | if (!exports.isObject(to[key])) {
|
385 | to[key] = {};
|
386 | }
|
387 | if (from[key] != null) {
|
388 | if (from[key].instanceOfSchema) {
|
389 | to[key] = from[key].clone();
|
390 | continue;
|
391 | } else if (from[key] instanceof ObjectId) {
|
392 | to[key] = new ObjectId(from[key]);
|
393 | continue;
|
394 | }
|
395 | }
|
396 | merge(to[key], from[key], options, path ? path + '.' + key : key);
|
397 | } else if (options.overwrite) {
|
398 | to[key] = from[key];
|
399 | }
|
400 | }
|
401 | };
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | exports.toObject = function toObject(obj) {
|
412 | Document || (Document = require('./document'));
|
413 | let ret;
|
414 |
|
415 | if (obj == null) {
|
416 | return obj;
|
417 | }
|
418 |
|
419 | if (obj instanceof Document) {
|
420 | return obj.toObject();
|
421 | }
|
422 |
|
423 | if (Array.isArray(obj)) {
|
424 | ret = [];
|
425 |
|
426 | for (let i = 0, len = obj.length; i < len; ++i) {
|
427 | ret.push(toObject(obj[i]));
|
428 | }
|
429 |
|
430 | return ret;
|
431 | }
|
432 |
|
433 | if ((obj.constructor && exports.getFunctionName(obj.constructor) === 'Object') ||
|
434 | (!obj.constructor && exports.isObject(obj))) {
|
435 | ret = {};
|
436 |
|
437 | for (const k in obj) {
|
438 | if (specialProperties.has(k)) {
|
439 | continue;
|
440 | }
|
441 | ret[k] = toObject(obj[k]);
|
442 | }
|
443 |
|
444 | return ret;
|
445 | }
|
446 |
|
447 | return obj;
|
448 | };
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | exports.isObject = function(arg) {
|
459 | if (Buffer.isBuffer(arg)) {
|
460 | return true;
|
461 | }
|
462 | return Object.prototype.toString.call(arg) === '[object Object]';
|
463 | };
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 | exports.isPOJO = function(arg) {
|
474 | return arg instanceof Object && arg.constructor.name === 'Object';
|
475 | };
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 | exports.args = sliced;
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 | exports.tick = function tick(callback) {
|
496 | if (typeof callback !== 'function') {
|
497 | return;
|
498 | }
|
499 | return function() {
|
500 | try {
|
501 | callback.apply(this, arguments);
|
502 | } catch (err) {
|
503 |
|
504 |
|
505 | process.nextTick(function() {
|
506 | throw err;
|
507 | });
|
508 | }
|
509 | };
|
510 | };
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | exports.isMongooseObject = function(v) {
|
522 | Document || (Document = require('./document'));
|
523 | MongooseArray || (MongooseArray = require('./types').Array);
|
524 | MongooseBuffer || (MongooseBuffer = require('./types').Buffer);
|
525 |
|
526 | if (v == null) {
|
527 | return false;
|
528 | }
|
529 |
|
530 | return v.$__ != null ||
|
531 | v.isMongooseArray ||
|
532 | v.isMongooseBuffer ||
|
533 | v.$isMongooseMap;
|
534 | };
|
535 |
|
536 | const isMongooseObject = exports.isMongooseObject;
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 | exports.expires = function expires(object) {
|
546 | if (!(object && object.constructor.name === 'Object')) {
|
547 | return;
|
548 | }
|
549 | if (!('expires' in object)) {
|
550 | return;
|
551 | }
|
552 |
|
553 | let when;
|
554 | if (typeof object.expires !== 'string') {
|
555 | when = object.expires;
|
556 | } else {
|
557 | when = Math.round(ms(object.expires) / 1000);
|
558 | }
|
559 | object.expireAfterSeconds = when;
|
560 | delete object.expires;
|
561 | };
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 | function PopulateOptions(obj) {
|
568 | this.path = obj.path;
|
569 | this.match = obj.match;
|
570 | this.select = obj.select;
|
571 | this.options = obj.options;
|
572 | this.model = obj.model;
|
573 | if (typeof obj.subPopulate === 'object') {
|
574 | this.populate = obj.subPopulate;
|
575 | }
|
576 | if (obj.justOne != null) {
|
577 | this.justOne = obj.justOne;
|
578 | }
|
579 | if (obj.count != null) {
|
580 | this.count = obj.count;
|
581 | }
|
582 | this._docs = {};
|
583 | }
|
584 |
|
585 |
|
586 | PopulateOptions.prototype.constructor = Object;
|
587 |
|
588 |
|
589 | exports.PopulateOptions = PopulateOptions;
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 | exports.populate = function populate(path, select, model, match, options, subPopulate, justOne, count) {
|
596 |
|
597 |
|
598 |
|
599 | function makeSingles(arr) {
|
600 | const ret = [];
|
601 | arr.forEach(function(obj) {
|
602 | if (/[\s]/.test(obj.path)) {
|
603 | const paths = obj.path.split(' ');
|
604 | paths.forEach(function(p) {
|
605 | const copy = Object.assign({}, obj);
|
606 | copy.path = p;
|
607 | ret.push(copy);
|
608 | });
|
609 | } else {
|
610 | ret.push(obj);
|
611 | }
|
612 | });
|
613 |
|
614 | return ret;
|
615 | }
|
616 |
|
617 |
|
618 | if (arguments.length === 1) {
|
619 | if (path instanceof PopulateOptions) {
|
620 | return [path];
|
621 | }
|
622 |
|
623 | if (Array.isArray(path)) {
|
624 | const singles = makeSingles(path);
|
625 | return singles.map(function(o) {
|
626 | if (o.populate && !(o.match || o.options)) {
|
627 | return exports.populate(o)[0];
|
628 | } else {
|
629 | return exports.populate(o)[0];
|
630 | }
|
631 | });
|
632 | }
|
633 |
|
634 | if (exports.isObject(path)) {
|
635 | match = path.match;
|
636 | options = path.options;
|
637 | select = path.select;
|
638 | model = path.model;
|
639 | subPopulate = path.populate;
|
640 | justOne = path.justOne;
|
641 | path = path.path;
|
642 | count = path.count;
|
643 | }
|
644 | } else if (typeof model === 'object') {
|
645 | options = match;
|
646 | match = model;
|
647 | model = undefined;
|
648 | }
|
649 |
|
650 | if (typeof path !== 'string') {
|
651 | throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
|
652 | }
|
653 |
|
654 | if (Array.isArray(subPopulate)) {
|
655 | const ret = [];
|
656 | subPopulate.forEach(function(obj) {
|
657 | if (/[\s]/.test(obj.path)) {
|
658 | const copy = Object.assign({}, obj);
|
659 | const paths = copy.path.split(' ');
|
660 | paths.forEach(function(p) {
|
661 | copy.path = p;
|
662 | ret.push(exports.populate(copy)[0]);
|
663 | });
|
664 | } else {
|
665 | ret.push(exports.populate(obj)[0]);
|
666 | }
|
667 | });
|
668 | subPopulate = exports.populate(ret);
|
669 | } else if (typeof subPopulate === 'object') {
|
670 | subPopulate = exports.populate(subPopulate);
|
671 | }
|
672 |
|
673 | const ret = [];
|
674 | const paths = path.split(' ');
|
675 | options = exports.clone(options);
|
676 | for (let i = 0; i < paths.length; ++i) {
|
677 | ret.push(new PopulateOptions({
|
678 | path: paths[i],
|
679 | select: select,
|
680 | match: match,
|
681 | options: options,
|
682 | model: model,
|
683 | subPopulate: subPopulate,
|
684 | justOne: justOne,
|
685 | count: count
|
686 | }));
|
687 | }
|
688 |
|
689 | return ret;
|
690 | };
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | exports.getValue = function(path, obj, map) {
|
700 | return mpath.get(path, obj, '_doc', map);
|
701 | };
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 | exports.setValue = function(path, val, obj, map, _copying) {
|
712 | mpath.set(path, val, obj, '_doc', map, _copying);
|
713 | };
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 | exports.object = {};
|
724 | exports.object.vals = function vals(o) {
|
725 | const keys = Object.keys(o);
|
726 | let i = keys.length;
|
727 | const ret = [];
|
728 |
|
729 | while (i--) {
|
730 | ret.push(o[keys[i]]);
|
731 | }
|
732 |
|
733 | return ret;
|
734 | };
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 | exports.object.shallowCopy = exports.options;
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 | const hop = Object.prototype.hasOwnProperty;
|
750 | exports.object.hasOwnProperty = function(obj, prop) {
|
751 | return hop.call(obj, prop);
|
752 | };
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 | exports.isNullOrUndefined = function(val) {
|
761 | return val === null || val === undefined;
|
762 | };
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 | exports.array = {};
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 | exports.array.flatten = function flatten(arr, filter, ret) {
|
782 | ret || (ret = []);
|
783 |
|
784 | arr.forEach(function(item) {
|
785 | if (Array.isArray(item)) {
|
786 | flatten(item, filter, ret);
|
787 | } else {
|
788 | if (!filter || filter(item)) {
|
789 | ret.push(item);
|
790 | }
|
791 | }
|
792 | });
|
793 |
|
794 | return ret;
|
795 | };
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 | exports.array.unique = function(arr) {
|
810 | const primitives = {};
|
811 | const ids = {};
|
812 | const ret = [];
|
813 | const length = arr.length;
|
814 | for (let i = 0; i < length; ++i) {
|
815 | if (typeof arr[i] === 'number' || typeof arr[i] === 'string' || arr[i] == null) {
|
816 | if (primitives[arr[i]]) {
|
817 | continue;
|
818 | }
|
819 | ret.push(arr[i]);
|
820 | primitives[arr[i]] = true;
|
821 | } else if (arr[i] instanceof ObjectId) {
|
822 | if (ids[arr[i].toString()]) {
|
823 | continue;
|
824 | }
|
825 | ret.push(arr[i]);
|
826 | ids[arr[i].toString()] = true;
|
827 | } else {
|
828 | ret.push(arr[i]);
|
829 | }
|
830 | }
|
831 |
|
832 | return ret;
|
833 | };
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 | exports.buffer = {};
|
843 | exports.buffer.areEqual = function(a, b) {
|
844 | if (!Buffer.isBuffer(a)) {
|
845 | return false;
|
846 | }
|
847 | if (!Buffer.isBuffer(b)) {
|
848 | return false;
|
849 | }
|
850 | if (a.length !== b.length) {
|
851 | return false;
|
852 | }
|
853 | for (let i = 0, len = a.length; i < len; ++i) {
|
854 | if (a[i] !== b[i]) {
|
855 | return false;
|
856 | }
|
857 | }
|
858 | return true;
|
859 | };
|
860 |
|
861 | exports.getFunctionName = function(fn) {
|
862 | if (fn.name) {
|
863 | return fn.name;
|
864 | }
|
865 | return (fn.toString().trim().match(/^function\s*([^\s(]+)/) || [])[1];
|
866 | };
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 | exports.decorate = function(destination, source) {
|
873 | for (const key in source) {
|
874 | if (specialProperties.has(key)) {
|
875 | continue;
|
876 | }
|
877 | destination[key] = source[key];
|
878 | }
|
879 | };
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 | exports.mergeClone = function(to, fromObj) {
|
890 | if (isMongooseObject(fromObj)) {
|
891 | fromObj = fromObj.toObject({
|
892 | transform: false,
|
893 | virtuals: false,
|
894 | depopulate: true,
|
895 | getters: false,
|
896 | flattenDecimals: false
|
897 | });
|
898 | }
|
899 | const keys = Object.keys(fromObj);
|
900 | const len = keys.length;
|
901 | let i = 0;
|
902 | let key;
|
903 |
|
904 | while (i < len) {
|
905 | key = keys[i++];
|
906 | if (specialProperties.has(key)) {
|
907 | continue;
|
908 | }
|
909 | if (typeof to[key] === 'undefined') {
|
910 | to[key] = exports.clone(fromObj[key], {
|
911 | transform: false,
|
912 | virtuals: false,
|
913 | depopulate: true,
|
914 | getters: false,
|
915 | flattenDecimals: false
|
916 | });
|
917 | } else {
|
918 | let val = fromObj[key];
|
919 | if (val != null && val.valueOf && !(val instanceof Date)) {
|
920 | val = val.valueOf();
|
921 | }
|
922 | if (exports.isObject(val)) {
|
923 | let obj = val;
|
924 | if (isMongooseObject(val) && !val.isMongooseBuffer) {
|
925 | obj = obj.toObject({
|
926 | transform: false,
|
927 | virtuals: false,
|
928 | depopulate: true,
|
929 | getters: false,
|
930 | flattenDecimals: false
|
931 | });
|
932 | }
|
933 | if (val.isMongooseBuffer) {
|
934 | obj = Buffer.from(obj);
|
935 | }
|
936 | exports.mergeClone(to[key], obj);
|
937 | } else {
|
938 | to[key] = exports.clone(val, {
|
939 | flattenDecimals: false
|
940 | });
|
941 | }
|
942 | }
|
943 | }
|
944 | };
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 | exports.each = function(arr, fn) {
|
955 | for (let i = 0; i < arr.length; ++i) {
|
956 | fn(arr[i]);
|
957 | }
|
958 | };
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 | exports.noop = function() {};
|