Cleanup
This commit is contained in:
104
node_modules/mongoose/lib/utils.js
generated
vendored
104
node_modules/mongoose/lib/utils.js
generated
vendored
@@ -12,11 +12,13 @@ const Decimal = require('./types/decimal128');
|
||||
const ObjectId = require('./types/objectid');
|
||||
const PopulateOptions = require('./options/PopulateOptions');
|
||||
const clone = require('./helpers/clone');
|
||||
const immediate = require('./helpers/immediate');
|
||||
const isObject = require('./helpers/isObject');
|
||||
const isBsonType = require('./helpers/isBsonType');
|
||||
const getFunctionName = require('./helpers/getFunctionName');
|
||||
const isMongooseObject = require('./helpers/isMongooseObject');
|
||||
const promiseOrCallback = require('./helpers/promiseOrCallback');
|
||||
const schemaMerge = require('./helpers/schema/merge');
|
||||
const specialProperties = require('./helpers/specialProperties');
|
||||
|
||||
let Document;
|
||||
@@ -62,6 +64,10 @@ exports.deepEqual = function deepEqual(a, b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof a !== 'object' && typeof b !== 'object') {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
if (a instanceof Date && b instanceof Date) {
|
||||
return a.getTime() === b.getTime();
|
||||
}
|
||||
@@ -78,11 +84,7 @@ exports.deepEqual = function deepEqual(a, b) {
|
||||
a.global === b.global;
|
||||
}
|
||||
|
||||
if (typeof a !== 'object' && typeof b !== 'object') {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
if (a === null || b === null || a === undefined || b === undefined) {
|
||||
if (a == null || b == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -90,6 +92,11 @@ exports.deepEqual = function deepEqual(a, b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (a instanceof Map && b instanceof Map) {
|
||||
return deepEqual(Array.from(a.keys()), Array.from(b.keys())) &&
|
||||
deepEqual(Array.from(a.values()), Array.from(b.values()));
|
||||
}
|
||||
|
||||
// Handle MongooseNumbers
|
||||
if (a instanceof Number && b instanceof Number) {
|
||||
return a.valueOf() === b.valueOf();
|
||||
@@ -99,28 +106,38 @@ exports.deepEqual = function deepEqual(a, b) {
|
||||
return exports.buffer.areEqual(a, b);
|
||||
}
|
||||
|
||||
if (isMongooseObject(a)) {
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
const len = a.length;
|
||||
if (len !== b.length) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < len; ++i) {
|
||||
if (!deepEqual(a[i], b[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (a.$__ != null) {
|
||||
a = a._doc;
|
||||
} else if (isMongooseObject(a)) {
|
||||
a = a.toObject();
|
||||
}
|
||||
if (isMongooseObject(b)) {
|
||||
|
||||
if (b.$__ != null) {
|
||||
b = b._doc;
|
||||
} else if (isMongooseObject(b)) {
|
||||
b = b.toObject();
|
||||
}
|
||||
|
||||
let ka;
|
||||
let kb;
|
||||
let key;
|
||||
let i;
|
||||
try {
|
||||
ka = Object.keys(a);
|
||||
kb = Object.keys(b);
|
||||
} catch (e) {
|
||||
// happens when one is a string literal and the other isn't
|
||||
return false;
|
||||
}
|
||||
const ka = Object.keys(a);
|
||||
const kb = Object.keys(b);
|
||||
const kaLength = ka.length;
|
||||
|
||||
// having the same number of owned properties (keys incorporates
|
||||
// hasOwnProperty)
|
||||
if (ka.length !== kb.length) {
|
||||
if (kaLength !== kb.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -129,7 +146,7 @@ exports.deepEqual = function deepEqual(a, b) {
|
||||
kb.sort();
|
||||
|
||||
// ~~~cheap key test
|
||||
for (i = ka.length - 1; i >= 0; i--) {
|
||||
for (let i = kaLength - 1; i >= 0; i--) {
|
||||
if (ka[i] !== kb[i]) {
|
||||
return false;
|
||||
}
|
||||
@@ -137,8 +154,7 @@ exports.deepEqual = function deepEqual(a, b) {
|
||||
|
||||
// equivalent values for every corresponding key, and
|
||||
// ~~~possibly expensive deep test
|
||||
for (i = ka.length - 1; i >= 0; i--) {
|
||||
key = ka[i];
|
||||
for (const key of ka) {
|
||||
if (!deepEqual(a[key], b[key])) {
|
||||
return false;
|
||||
}
|
||||
@@ -259,9 +275,16 @@ exports.merge = function merge(to, from, options, path) {
|
||||
to[key] = {};
|
||||
}
|
||||
if (from[key] != null) {
|
||||
if (from[key].instanceOfSchema) {
|
||||
// Skip merging schemas if we're creating a discriminator schema and
|
||||
// base schema has a given path as a single nested but discriminator schema
|
||||
// has the path as a document array, or vice versa (gh-9534)
|
||||
if (options.isDiscriminatorSchemaMerge &&
|
||||
(from[key].$isSingleNested && to[key].$isMongooseDocumentArray) ||
|
||||
(from[key].$isMongooseDocumentArray && to[key].$isSingleNested)) {
|
||||
continue;
|
||||
} else if (from[key].instanceOfSchema) {
|
||||
if (to[key].instanceOfSchema) {
|
||||
to[key].add(from[key].clone());
|
||||
schemaMerge(to[key], from[key].clone(), options.isDiscriminatorSchemaMerge);
|
||||
} else {
|
||||
to[key] = from[key].clone();
|
||||
}
|
||||
@@ -311,7 +334,7 @@ exports.toObject = function toObject(obj) {
|
||||
if (exports.isPOJO(obj)) {
|
||||
ret = {};
|
||||
|
||||
for (const k in obj) {
|
||||
for (const k of Object.keys(obj)) {
|
||||
if (specialProperties.has(k)) {
|
||||
continue;
|
||||
}
|
||||
@@ -418,7 +441,7 @@ exports.tick = function tick(callback) {
|
||||
} catch (err) {
|
||||
// only nextTick on err to get out of
|
||||
// the event loop and avoid state corruption.
|
||||
process.nextTick(function() {
|
||||
immediate(function() {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
@@ -734,23 +757,23 @@ exports.isArrayIndex = function(val) {
|
||||
*/
|
||||
|
||||
exports.array.unique = function(arr) {
|
||||
const primitives = {};
|
||||
const ids = {};
|
||||
const primitives = new Set();
|
||||
const ids = new Set();
|
||||
const ret = [];
|
||||
|
||||
for (const item of arr) {
|
||||
if (typeof item === 'number' || typeof item === 'string' || item == null) {
|
||||
if (primitives[item]) {
|
||||
if (primitives.has(item)) {
|
||||
continue;
|
||||
}
|
||||
ret.push(item);
|
||||
primitives[item] = true;
|
||||
primitives.add(item);
|
||||
} else if (item instanceof ObjectId) {
|
||||
if (ids[item.toString()]) {
|
||||
if (ids.has(item.toString())) {
|
||||
continue;
|
||||
}
|
||||
ret.push(item);
|
||||
ids[item.toString()] = true;
|
||||
ids.add(item.toString());
|
||||
} else {
|
||||
ret.push(item);
|
||||
}
|
||||
@@ -899,3 +922,20 @@ exports.getOption = function(name) {
|
||||
*/
|
||||
|
||||
exports.noop = function() {};
|
||||
|
||||
exports.errorToPOJO = function errorToPOJO(error) {
|
||||
const isError = error instanceof Error;
|
||||
if (!isError) {
|
||||
throw new Error('`error` must be `instanceof Error`.');
|
||||
}
|
||||
|
||||
const ret = {};
|
||||
for (const properyName of Object.getOwnPropertyNames(error)) {
|
||||
ret[properyName] = error[properyName];
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
exports.nodeMajorVersion = function nodeMajorVersion() {
|
||||
return parseInt(process.versions.node.split('.')[0], 10);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user