UNPKG

18.7 kBJavaScriptView Raw
1'use strict';
2
3/*!
4 * Module dependencies.
5 */
6
7const ms = require('ms');
8const mpath = require('mpath');
9const sliced = require('sliced');
10const Buffer = require('safe-buffer').Buffer;
11const Decimal = require('./types/decimal128');
12const ObjectId = require('./types/objectid');
13const PopulateOptions = require('./options/PopulateOptions');
14const clone = require('./helpers/clone');
15const isObject = require('./helpers/isObject');
16const isBsonType = require('./helpers/isBsonType');
17const getFunctionName = require('./helpers/getFunctionName');
18const isMongooseObject = require('./helpers/isMongooseObject');
19const promiseOrCallback = require('./helpers/promiseOrCallback');
20const specialProperties = require('./helpers/specialProperties');
21
22let Document;
23
24exports.specialProperties = specialProperties;
25
26/*!
27 * Produces a collection name from model `name`. By default, just returns
28 * the model name
29 *
30 * @param {String} name a model name
31 * @param {Function} pluralize function that pluralizes the collection name
32 * @return {String} a collection name
33 * @api private
34 */
35
36exports.toCollectionName = function(name, pluralize) {
37 if (name === 'system.profile') {
38 return name;
39 }
40 if (name === 'system.indexes') {
41 return name;
42 }
43 if (typeof pluralize === 'function') {
44 return pluralize(name);
45 }
46 return name;
47};
48
49/*!
50 * Determines if `a` and `b` are deep equal.
51 *
52 * Modified from node/lib/assert.js
53 *
54 * @param {any} a a value to compare to `b`
55 * @param {any} b a value to compare to `a`
56 * @return {Boolean}
57 * @api private
58 */
59
60exports.deepEqual = function deepEqual(a, b) {
61 if (a === b) {
62 return true;
63 }
64
65 if (a instanceof Date && b instanceof Date) {
66 return a.getTime() === b.getTime();
67 }
68
69 if ((isBsonType(a, 'ObjectID') && isBsonType(b, 'ObjectID')) ||
70 (isBsonType(a, 'Decimal128') && isBsonType(b, 'Decimal128'))) {
71 return a.toString() === b.toString();
72 }
73
74 if (a instanceof RegExp && b instanceof RegExp) {
75 return a.source === b.source &&
76 a.ignoreCase === b.ignoreCase &&
77 a.multiline === b.multiline &&
78 a.global === b.global;
79 }
80
81 if (typeof a !== 'object' && typeof b !== 'object') {
82 return a == b;
83 }
84
85 if (a === null || b === null || a === undefined || b === undefined) {
86 return false;
87 }
88
89 if (a.prototype !== b.prototype) {
90 return false;
91 }
92
93 // Handle MongooseNumbers
94 if (a instanceof Number && b instanceof Number) {
95 return a.valueOf() === b.valueOf();
96 }
97
98 if (Buffer.isBuffer(a)) {
99 return exports.buffer.areEqual(a, b);
100 }
101
102 if (isMongooseObject(a)) {
103 a = a.toObject();
104 }
105 if (isMongooseObject(b)) {
106 b = b.toObject();
107 }
108
109 let ka;
110 let kb;
111 let key;
112 let i;
113 try {
114 ka = Object.keys(a);
115 kb = Object.keys(b);
116 } catch (e) {
117 // happens when one is a string literal and the other isn't
118 return false;
119 }
120
121 // having the same number of owned properties (keys incorporates
122 // hasOwnProperty)
123 if (ka.length !== kb.length) {
124 return false;
125 }
126
127 // the same set of keys (although not necessarily the same order),
128 ka.sort();
129 kb.sort();
130
131 // ~~~cheap key test
132 for (i = ka.length - 1; i >= 0; i--) {
133 if (ka[i] !== kb[i]) {
134 return false;
135 }
136 }
137
138 // equivalent values for every corresponding key, and
139 // ~~~possibly expensive deep test
140 for (i = ka.length - 1; i >= 0; i--) {
141 key = ka[i];
142 if (!deepEqual(a[key], b[key])) {
143 return false;
144 }
145 }
146
147 return true;
148};
149
150/*!
151 * Get the last element of an array
152 */
153
154exports.last = function(arr) {
155 if (arr.length > 0) {
156 return arr[arr.length - 1];
157 }
158 return void 0;
159};
160
161exports.clone = clone;
162
163/*!
164 * ignore
165 */
166
167exports.promiseOrCallback = promiseOrCallback;
168
169/*!
170 * ignore
171 */
172
173exports.omit = function omit(obj, keys) {
174 if (keys == null) {
175 return Object.assign({}, obj);
176 }
177 if (!Array.isArray(keys)) {
178 keys = [keys];
179 }
180
181 const ret = Object.assign({}, obj);
182 for (const key of keys) {
183 delete ret[key];
184 }
185 return ret;
186};
187
188
189/*!
190 * Shallow copies defaults into options.
191 *
192 * @param {Object} defaults
193 * @param {Object} options
194 * @return {Object} the merged object
195 * @api private
196 */
197
198exports.options = function(defaults, options) {
199 const keys = Object.keys(defaults);
200 let i = keys.length;
201 let k;
202
203 options = options || {};
204
205 while (i--) {
206 k = keys[i];
207 if (!(k in options)) {
208 options[k] = defaults[k];
209 }
210 }
211
212 return options;
213};
214
215/*!
216 * Generates a random string
217 *
218 * @api private
219 */
220
221exports.random = function() {
222 return Math.random().toString().substr(3);
223};
224
225/*!
226 * Merges `from` into `to` without overwriting existing properties.
227 *
228 * @param {Object} to
229 * @param {Object} from
230 * @api private
231 */
232
233exports.merge = function merge(to, from, options, path) {
234 options = options || {};
235
236 const keys = Object.keys(from);
237 let i = 0;
238 const len = keys.length;
239 let key;
240
241 path = path || '';
242 const omitNested = options.omitNested || {};
243
244 while (i < len) {
245 key = keys[i++];
246 if (options.omit && options.omit[key]) {
247 continue;
248 }
249 if (omitNested[path]) {
250 continue;
251 }
252 if (specialProperties.has(key)) {
253 continue;
254 }
255 if (to[key] == null) {
256 to[key] = from[key];
257 } else if (exports.isObject(from[key])) {
258 if (!exports.isObject(to[key])) {
259 to[key] = {};
260 }
261 if (from[key] != null) {
262 if (from[key].instanceOfSchema) {
263 if (to[key].instanceOfSchema) {
264 to[key].add(from[key].clone());
265 } else {
266 to[key] = from[key].clone();
267 }
268 continue;
269 } else if (from[key] instanceof ObjectId) {
270 to[key] = new ObjectId(from[key]);
271 continue;
272 }
273 }
274 merge(to[key], from[key], options, path ? path + '.' + key : key);
275 } else if (options.overwrite) {
276 to[key] = from[key];
277 }
278 }
279};
280
281/*!
282 * Applies toObject recursively.
283 *
284 * @param {Document|Array|Object} obj
285 * @return {Object}
286 * @api private
287 */
288
289exports.toObject = function toObject(obj) {
290 Document || (Document = require('./document'));
291 let ret;
292
293 if (obj == null) {
294 return obj;
295 }
296
297 if (obj instanceof Document) {
298 return obj.toObject();
299 }
300
301 if (Array.isArray(obj)) {
302 ret = [];
303
304 for (const doc of obj) {
305 ret.push(toObject(doc));
306 }
307
308 return ret;
309 }
310
311 if (exports.isPOJO(obj)) {
312 ret = {};
313
314 for (const k in obj) {
315 if (specialProperties.has(k)) {
316 continue;
317 }
318 ret[k] = toObject(obj[k]);
319 }
320
321 return ret;
322 }
323
324 return obj;
325};
326
327exports.isObject = isObject;
328
329/*!
330 * Determines if `arg` is a plain old JavaScript object (POJO). Specifically,
331 * `arg` must be an object but not an instance of any special class, like String,
332 * ObjectId, etc.
333 *
334 * `Object.getPrototypeOf()` is part of ES5: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
335 *
336 * @param {Object|Array|String|Function|RegExp|any} arg
337 * @api private
338 * @return {Boolean}
339 */
340
341exports.isPOJO = function isPOJO(arg) {
342 if (arg == null || typeof arg !== 'object') {
343 return false;
344 }
345 const proto = Object.getPrototypeOf(arg);
346 // Prototype may be null if you used `Object.create(null)`
347 // Checking `proto`'s constructor is safe because `getPrototypeOf()`
348 // explicitly crosses the boundary from object data to object metadata
349 return !proto || proto.constructor.name === 'Object';
350};
351
352/*!
353 * Determines if `obj` is a built-in object like an array, date, boolean,
354 * etc.
355 */
356
357exports.isNativeObject = function(arg) {
358 return Array.isArray(arg) ||
359 arg instanceof Date ||
360 arg instanceof Boolean ||
361 arg instanceof Number ||
362 arg instanceof String;
363};
364
365/*!
366 * Determines if `val` is an object that has no own keys
367 */
368
369exports.isEmptyObject = function(val) {
370 return val != null &&
371 typeof val === 'object' &&
372 Object.keys(val).length === 0;
373};
374
375/*!
376 * Search if `obj` or any POJOs nested underneath `obj` has a property named
377 * `key`
378 */
379
380exports.hasKey = function hasKey(obj, key) {
381 const props = Object.keys(obj);
382 for (const prop of props) {
383 if (prop === key) {
384 return true;
385 }
386 if (exports.isPOJO(obj[prop]) && exports.hasKey(obj[prop], key)) {
387 return true;
388 }
389 }
390 return false;
391};
392
393/*!
394 * A faster Array.prototype.slice.call(arguments) alternative
395 * @api private
396 */
397
398exports.args = sliced;
399
400/*!
401 * process.nextTick helper.
402 *
403 * Wraps `callback` in a try/catch + nextTick.
404 *
405 * node-mongodb-native has a habit of state corruption when an error is immediately thrown from within a collection callback.
406 *
407 * @param {Function} callback
408 * @api private
409 */
410
411exports.tick = function tick(callback) {
412 if (typeof callback !== 'function') {
413 return;
414 }
415 return function() {
416 try {
417 callback.apply(this, arguments);
418 } catch (err) {
419 // only nextTick on err to get out of
420 // the event loop and avoid state corruption.
421 process.nextTick(function() {
422 throw err;
423 });
424 }
425 };
426};
427
428/*!
429 * Returns true if `v` is an object that can be serialized as a primitive in
430 * MongoDB
431 */
432
433exports.isMongooseType = function(v) {
434 return v instanceof ObjectId || v instanceof Decimal || v instanceof Buffer;
435};
436
437exports.isMongooseObject = isMongooseObject;
438
439/*!
440 * Converts `expires` options of index objects to `expiresAfterSeconds` options for MongoDB.
441 *
442 * @param {Object} object
443 * @api private
444 */
445
446exports.expires = function expires(object) {
447 if (!(object && object.constructor.name === 'Object')) {
448 return;
449 }
450 if (!('expires' in object)) {
451 return;
452 }
453
454 let when;
455 if (typeof object.expires !== 'string') {
456 when = object.expires;
457 } else {
458 when = Math.round(ms(object.expires) / 1000);
459 }
460 object.expireAfterSeconds = when;
461 delete object.expires;
462};
463
464/*!
465 * populate helper
466 */
467
468exports.populate = function populate(path, select, model, match, options, subPopulate, justOne, count) {
469 // might have passed an object specifying all arguments
470 let obj = null;
471 if (arguments.length === 1) {
472 if (path instanceof PopulateOptions) {
473 return [path];
474 }
475
476 if (Array.isArray(path)) {
477 const singles = makeSingles(path);
478 return singles.map(o => exports.populate(o)[0]);
479 }
480
481 if (exports.isObject(path)) {
482 obj = Object.assign({}, path);
483 } else {
484 obj = { path: path };
485 }
486 } else if (typeof model === 'object') {
487 obj = {
488 path: path,
489 select: select,
490 match: model,
491 options: match
492 };
493 } else {
494 obj = {
495 path: path,
496 select: select,
497 model: model,
498 match: match,
499 options: options,
500 populate: subPopulate,
501 justOne: justOne,
502 count: count
503 };
504 }
505
506 if (typeof obj.path !== 'string') {
507 throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
508 }
509
510 return _populateObj(obj);
511
512 // The order of select/conditions args is opposite Model.find but
513 // necessary to keep backward compatibility (select could be
514 // an array, string, or object literal).
515 function makeSingles(arr) {
516 const ret = [];
517 arr.forEach(function(obj) {
518 if (/[\s]/.test(obj.path)) {
519 const paths = obj.path.split(' ');
520 paths.forEach(function(p) {
521 const copy = Object.assign({}, obj);
522 copy.path = p;
523 ret.push(copy);
524 });
525 } else {
526 ret.push(obj);
527 }
528 });
529
530 return ret;
531 }
532};
533
534function _populateObj(obj) {
535 if (Array.isArray(obj.populate)) {
536 const ret = [];
537 obj.populate.forEach(function(obj) {
538 if (/[\s]/.test(obj.path)) {
539 const copy = Object.assign({}, obj);
540 const paths = copy.path.split(' ');
541 paths.forEach(function(p) {
542 copy.path = p;
543 ret.push(exports.populate(copy)[0]);
544 });
545 } else {
546 ret.push(exports.populate(obj)[0]);
547 }
548 });
549 obj.populate = exports.populate(ret);
550 } else if (obj.populate != null && typeof obj.populate === 'object') {
551 obj.populate = exports.populate(obj.populate);
552 }
553
554 const ret = [];
555 const paths = obj.path.split(' ');
556 if (obj.options != null) {
557 obj.options = exports.clone(obj.options);
558 }
559
560 for (const path of paths) {
561 ret.push(new PopulateOptions(Object.assign({}, obj, { path: path })));
562 }
563
564 return ret;
565}
566
567/*!
568 * Return the value of `obj` at the given `path`.
569 *
570 * @param {String} path
571 * @param {Object} obj
572 */
573
574exports.getValue = function(path, obj, map) {
575 return mpath.get(path, obj, '_doc', map);
576};
577
578/*!
579 * Sets the value of `obj` at the given `path`.
580 *
581 * @param {String} path
582 * @param {Anything} val
583 * @param {Object} obj
584 */
585
586exports.setValue = function(path, val, obj, map, _copying) {
587 mpath.set(path, val, obj, '_doc', map, _copying);
588};
589
590/*!
591 * Returns an array of values from object `o`.
592 *
593 * @param {Object} o
594 * @return {Array}
595 * @private
596 */
597
598exports.object = {};
599exports.object.vals = function vals(o) {
600 const keys = Object.keys(o);
601 let i = keys.length;
602 const ret = [];
603
604 while (i--) {
605 ret.push(o[keys[i]]);
606 }
607
608 return ret;
609};
610
611/*!
612 * @see exports.options
613 */
614
615exports.object.shallowCopy = exports.options;
616
617/*!
618 * Safer helper for hasOwnProperty checks
619 *
620 * @param {Object} obj
621 * @param {String} prop
622 */
623
624const hop = Object.prototype.hasOwnProperty;
625exports.object.hasOwnProperty = function(obj, prop) {
626 return hop.call(obj, prop);
627};
628
629/*!
630 * Determine if `val` is null or undefined
631 *
632 * @return {Boolean}
633 */
634
635exports.isNullOrUndefined = function(val) {
636 return val === null || val === undefined;
637};
638
639/*!
640 * ignore
641 */
642
643exports.array = {};
644
645/*!
646 * Flattens an array.
647 *
648 * [ 1, [ 2, 3, [4] ]] -> [1,2,3,4]
649 *
650 * @param {Array} arr
651 * @param {Function} [filter] If passed, will be invoked with each item in the array. If `filter` returns a falsy value, the item will not be included in the results.
652 * @return {Array}
653 * @private
654 */
655
656exports.array.flatten = function flatten(arr, filter, ret) {
657 ret || (ret = []);
658
659 arr.forEach(function(item) {
660 if (Array.isArray(item)) {
661 flatten(item, filter, ret);
662 } else {
663 if (!filter || filter(item)) {
664 ret.push(item);
665 }
666 }
667 });
668
669 return ret;
670};
671
672/*!
673 * ignore
674 */
675
676const _hasOwnProperty = Object.prototype.hasOwnProperty;
677
678exports.hasUserDefinedProperty = function(obj, key) {
679 if (obj == null) {
680 return false;
681 }
682
683 if (Array.isArray(key)) {
684 for (const k of key) {
685 if (exports.hasUserDefinedProperty(obj, k)) {
686 return true;
687 }
688 }
689 return false;
690 }
691
692 if (_hasOwnProperty.call(obj, key)) {
693 return true;
694 }
695 if (typeof obj === 'object' && key in obj) {
696 const v = obj[key];
697 return v !== Object.prototype[key] && v !== Array.prototype[key];
698 }
699
700 return false;
701};
702
703/*!
704 * ignore
705 */
706
707const MAX_ARRAY_INDEX = Math.pow(2, 32) - 1;
708
709exports.isArrayIndex = function(val) {
710 if (typeof val === 'number') {
711 return val >= 0 && val <= MAX_ARRAY_INDEX;
712 }
713 if (typeof val === 'string') {
714 if (!/^\d+$/.test(val)) {
715 return false;
716 }
717 val = +val;
718 return val >= 0 && val <= MAX_ARRAY_INDEX;
719 }
720
721 return false;
722};
723
724/*!
725 * Removes duplicate values from an array
726 *
727 * [1, 2, 3, 3, 5] => [1, 2, 3, 5]
728 * [ ObjectId("550988ba0c19d57f697dc45e"), ObjectId("550988ba0c19d57f697dc45e") ]
729 * => [ObjectId("550988ba0c19d57f697dc45e")]
730 *
731 * @param {Array} arr
732 * @return {Array}
733 * @private
734 */
735
736exports.array.unique = function(arr) {
737 const primitives = {};
738 const ids = {};
739 const ret = [];
740
741 for (const item of arr) {
742 if (typeof item === 'number' || typeof item === 'string' || item == null) {
743 if (primitives[item]) {
744 continue;
745 }
746 ret.push(item);
747 primitives[item] = true;
748 } else if (item instanceof ObjectId) {
749 if (ids[item.toString()]) {
750 continue;
751 }
752 ret.push(item);
753 ids[item.toString()] = true;
754 } else {
755 ret.push(item);
756 }
757 }
758
759 return ret;
760};
761
762/*!
763 * Determines if two buffers are equal.
764 *
765 * @param {Buffer} a
766 * @param {Object} b
767 */
768
769exports.buffer = {};
770exports.buffer.areEqual = function(a, b) {
771 if (!Buffer.isBuffer(a)) {
772 return false;
773 }
774 if (!Buffer.isBuffer(b)) {
775 return false;
776 }
777 if (a.length !== b.length) {
778 return false;
779 }
780 for (let i = 0, len = a.length; i < len; ++i) {
781 if (a[i] !== b[i]) {
782 return false;
783 }
784 }
785 return true;
786};
787
788exports.getFunctionName = getFunctionName;
789/*!
790 * Decorate buffers
791 */
792
793exports.decorate = function(destination, source) {
794 for (const key in source) {
795 if (specialProperties.has(key)) {
796 continue;
797 }
798 destination[key] = source[key];
799 }
800};
801
802/**
803 * merges to with a copy of from
804 *
805 * @param {Object} to
806 * @param {Object} fromObj
807 * @api private
808 */
809
810exports.mergeClone = function(to, fromObj) {
811 if (isMongooseObject(fromObj)) {
812 fromObj = fromObj.toObject({
813 transform: false,
814 virtuals: false,
815 depopulate: true,
816 getters: false,
817 flattenDecimals: false
818 });
819 }
820 const keys = Object.keys(fromObj);
821 const len = keys.length;
822 let i = 0;
823 let key;
824
825 while (i < len) {
826 key = keys[i++];
827 if (specialProperties.has(key)) {
828 continue;
829 }
830 if (typeof to[key] === 'undefined') {
831 to[key] = exports.clone(fromObj[key], {
832 transform: false,
833 virtuals: false,
834 depopulate: true,
835 getters: false,
836 flattenDecimals: false
837 });
838 } else {
839 let val = fromObj[key];
840 if (val != null && val.valueOf && !(val instanceof Date)) {
841 val = val.valueOf();
842 }
843 if (exports.isObject(val)) {
844 let obj = val;
845 if (isMongooseObject(val) && !val.isMongooseBuffer) {
846 obj = obj.toObject({
847 transform: false,
848 virtuals: false,
849 depopulate: true,
850 getters: false,
851 flattenDecimals: false
852 });
853 }
854 if (val.isMongooseBuffer) {
855 obj = Buffer.from(obj);
856 }
857 exports.mergeClone(to[key], obj);
858 } else {
859 to[key] = exports.clone(val, {
860 flattenDecimals: false
861 });
862 }
863 }
864 }
865};
866
867/**
868 * Executes a function on each element of an array (like _.each)
869 *
870 * @param {Array} arr
871 * @param {Function} fn
872 * @api private
873 */
874
875exports.each = function(arr, fn) {
876 for (const item of arr) {
877 fn(item);
878 }
879};
880
881/*!
882 * ignore
883 */
884
885exports.getOption = function(name) {
886 const sources = Array.prototype.slice.call(arguments, 1);
887
888 for (const source of sources) {
889 if (source[name] != null) {
890 return source[name];
891 }
892 }
893
894 return null;
895};
896
897/*!
898 * ignore
899 */
900
901exports.noop = function() {};