mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Updated mongoose
This commit is contained in:
376
node_modules/mongoose/lib/utils.js
generated
vendored
376
node_modules/mongoose/lib/utils.js
generated
vendored
@ -2,15 +2,14 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var ReadPref = require('mongodb').ReadPreference
|
||||
, ObjectId = require('./types/objectid')
|
||||
, cloneRegExp = require('regexp-clone')
|
||||
, sliced = require('sliced')
|
||||
, mpath = require('mpath')
|
||||
, ms = require('ms')
|
||||
, MongooseBuffer
|
||||
, MongooseArray
|
||||
, Document
|
||||
var ObjectId = require('./types/objectid');
|
||||
var cloneRegExp = require('regexp-clone');
|
||||
var sliced = require('sliced');
|
||||
var mpath = require('mpath');
|
||||
var ms = require('ms');
|
||||
var MongooseBuffer;
|
||||
var MongooseArray;
|
||||
var Document;
|
||||
|
||||
/*!
|
||||
* Produces a collection name from model `name`.
|
||||
@ -20,9 +19,11 @@ var ReadPref = require('mongodb').ReadPreference
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.toCollectionName = function (name) {
|
||||
exports.toCollectionName = function(name, options) {
|
||||
options = options || {};
|
||||
if ('system.profile' === name) return name;
|
||||
if ('system.indexes' === name) return name;
|
||||
if (options.pluralization === false) return name;
|
||||
return pluralize(name.toLowerCase());
|
||||
};
|
||||
|
||||
@ -52,8 +53,10 @@ exports.pluralization = [
|
||||
[/(x|ch|ss|sh)$/gi, '$1es'],
|
||||
[/(matr|vert|ind)ix|ex$/gi, '$1ices'],
|
||||
[/([m|l])ouse$/gi, '$1ice'],
|
||||
[/(kn|w|l)ife$/gi, '$1ives'],
|
||||
[/(quiz)$/gi, '$1zes'],
|
||||
[/s$/gi, 's'],
|
||||
[/([^a-z])$/, '$1'],
|
||||
[/$/gi, 's']
|
||||
];
|
||||
var rules = exports.pluralization;
|
||||
@ -104,16 +107,16 @@ var uncountables = exports.uncountables;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function pluralize (str) {
|
||||
var rule, found;
|
||||
if (!~uncountables.indexOf(str.toLowerCase())){
|
||||
found = rules.filter(function(rule){
|
||||
function pluralize(str) {
|
||||
var found;
|
||||
if (!~uncountables.indexOf(str.toLowerCase())) {
|
||||
found = rules.filter(function(rule) {
|
||||
return str.match(rule[0]);
|
||||
});
|
||||
if (found[0]) return str.replace(found[0][0], found[0][1]);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Determines if `a` and `b` are deep equal.
|
||||
@ -126,7 +129,7 @@ function pluralize (str) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.deepEqual = function deepEqual (a, b) {
|
||||
exports.deepEqual = function deepEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
|
||||
if (a instanceof Date && b instanceof Date)
|
||||
@ -147,7 +150,7 @@ exports.deepEqual = function deepEqual (a, b) {
|
||||
return a == b;
|
||||
|
||||
if (a === null || b === null || a === undefined || b === undefined)
|
||||
return false
|
||||
return false;
|
||||
|
||||
if (a.prototype !== b.prototype) return false;
|
||||
|
||||
@ -157,12 +160,7 @@ exports.deepEqual = function deepEqual (a, b) {
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(a)) {
|
||||
if (!Buffer.isBuffer(b)) return false;
|
||||
if (a.length !== b.length) return false;
|
||||
for (var i = 0, len = a.length; i < len; ++i) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
return exports.buffer.areEqual(a, b);
|
||||
}
|
||||
|
||||
if (isMongooseObject(a)) a = a.toObject();
|
||||
@ -214,7 +212,7 @@ exports.deepEqual = function deepEqual (a, b) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.clone = function clone (obj, options) {
|
||||
exports.clone = function clone(obj, options) {
|
||||
if (obj === undefined || obj === null)
|
||||
return obj;
|
||||
|
||||
@ -230,7 +228,7 @@ exports.clone = function clone (obj, options) {
|
||||
}
|
||||
|
||||
if (obj.constructor) {
|
||||
switch (obj.constructor.name) {
|
||||
switch (exports.getFunctionName(obj.constructor)) {
|
||||
case 'Object':
|
||||
return cloneObject(obj, options);
|
||||
case 'Date':
|
||||
@ -260,15 +258,15 @@ var clone = exports.clone;
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function cloneObject (obj, options) {
|
||||
var retainKeyOrder = options && options.retainKeyOrder
|
||||
, minimize = options && options.minimize
|
||||
, ret = {}
|
||||
, hasKeys
|
||||
, keys
|
||||
, val
|
||||
, k
|
||||
, i
|
||||
function cloneObject(obj, options) {
|
||||
var retainKeyOrder = options && options.retainKeyOrder,
|
||||
minimize = options && options.minimize,
|
||||
ret = {},
|
||||
hasKeys,
|
||||
keys,
|
||||
val,
|
||||
k,
|
||||
i;
|
||||
|
||||
if (retainKeyOrder) {
|
||||
for (k in obj) {
|
||||
@ -299,14 +297,14 @@ function cloneObject (obj, options) {
|
||||
return minimize
|
||||
? hasKeys && ret
|
||||
: ret;
|
||||
};
|
||||
}
|
||||
|
||||
function cloneArray (arr, options) {
|
||||
function cloneArray(arr, options) {
|
||||
var ret = [];
|
||||
for (var i = 0, l = arr.length; i < l; i++)
|
||||
ret.push(clone(arr[i], options));
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Shallow copies defaults into options.
|
||||
@ -317,10 +315,10 @@ function cloneArray (arr, options) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.options = function (defaults, options) {
|
||||
var keys = Object.keys(defaults)
|
||||
, i = keys.length
|
||||
, k ;
|
||||
exports.options = function(defaults, options) {
|
||||
var keys = Object.keys(defaults),
|
||||
i = keys.length,
|
||||
k;
|
||||
|
||||
options = options || {};
|
||||
|
||||
@ -340,7 +338,7 @@ exports.options = function (defaults, options) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.random = function () {
|
||||
exports.random = function() {
|
||||
return Math.random().toString().substr(3);
|
||||
};
|
||||
|
||||
@ -352,21 +350,17 @@ exports.random = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.merge = function merge (to, from) {
|
||||
var keys = Object.keys(from)
|
||||
, i = keys.length
|
||||
, key
|
||||
exports.merge = function merge(to, from) {
|
||||
var keys = Object.keys(from),
|
||||
i = keys.length,
|
||||
key;
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
if ('undefined' === typeof to[key]) {
|
||||
to[key] = from[key];
|
||||
} else {
|
||||
if (exports.isObject(from[key])) {
|
||||
merge(to[key], from[key]);
|
||||
} else {
|
||||
to[key] = from[key];
|
||||
}
|
||||
} else if (exports.isObject(from[key])) {
|
||||
merge(to[key], from[key]);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -377,6 +371,49 @@ exports.merge = function merge (to, from) {
|
||||
|
||||
var toString = Object.prototype.toString;
|
||||
|
||||
/*!
|
||||
* Applies toObject recursively.
|
||||
*
|
||||
* @param {Document|Array|Object} obj
|
||||
* @return {Object}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.toObject = function toObject(obj) {
|
||||
var ret;
|
||||
|
||||
if (exports.isNullOrUndefined(obj)) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (obj instanceof Document) {
|
||||
return obj.toObject();
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
ret = [];
|
||||
|
||||
for (var i = 0, len = obj.length; i < len; ++i) {
|
||||
ret.push(toObject(obj[i]));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((obj.constructor && exports.getFunctionName(obj.constructor) === 'Object') ||
|
||||
(!obj.constructor && exports.isObject(obj))) {
|
||||
ret = {};
|
||||
|
||||
for (var k in obj) {
|
||||
ret[k] = toObject(obj[k]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Determines if `arg` is an object.
|
||||
*
|
||||
@ -385,9 +422,12 @@ var toString = Object.prototype.toString;
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
exports.isObject = function (arg) {
|
||||
exports.isObject = function(arg) {
|
||||
if (Buffer.isBuffer(arg)) {
|
||||
return true;
|
||||
}
|
||||
return '[object Object]' == toString.call(arg);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* A faster Array.prototype.slice.call(arguments) alternative
|
||||
@ -407,20 +447,20 @@ exports.args = sliced;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.tick = function tick (callback) {
|
||||
exports.tick = function tick(callback) {
|
||||
if ('function' !== typeof callback) return;
|
||||
return function () {
|
||||
return function() {
|
||||
try {
|
||||
callback.apply(this, arguments);
|
||||
} catch (err) {
|
||||
// only nextTick on err to get out of
|
||||
// the event loop and avoid state corruption.
|
||||
process.nextTick(function () {
|
||||
process.nextTick(function() {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/*!
|
||||
* Returns if `v` is a mongoose object that has a `toObject()` method we can use.
|
||||
@ -431,15 +471,15 @@ exports.tick = function tick (callback) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.isMongooseObject = function (v) {
|
||||
exports.isMongooseObject = function(v) {
|
||||
Document || (Document = require('./document'));
|
||||
MongooseArray || (MongooseArray = require('./types').Array);
|
||||
MongooseBuffer || (MongooseBuffer = require('./types').Buffer);
|
||||
|
||||
return v instanceof Document ||
|
||||
v instanceof MongooseArray ||
|
||||
v instanceof MongooseBuffer
|
||||
}
|
||||
(v && v.isMongooseArray) ||
|
||||
(v && v.isMongooseBuffer);
|
||||
};
|
||||
var isMongooseObject = exports.isMongooseObject;
|
||||
|
||||
/*!
|
||||
@ -449,7 +489,7 @@ var isMongooseObject = exports.isMongooseObject;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.expires = function expires (object) {
|
||||
exports.expires = function expires(object) {
|
||||
if (!(object && 'Object' == object.constructor.name)) return;
|
||||
if (!('expires' in object)) return;
|
||||
|
||||
@ -461,54 +501,21 @@ exports.expires = function expires (object) {
|
||||
}
|
||||
object.expireAfterSeconds = when;
|
||||
delete object.expires;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Converts arguments to ReadPrefs the driver
|
||||
* can understand.
|
||||
*
|
||||
* @TODO move this into the driver layer
|
||||
* @param {String|Array} pref
|
||||
* @param {Array} [tags]
|
||||
*/
|
||||
|
||||
exports.readPref = function readPref (pref, tags) {
|
||||
if (Array.isArray(pref)) {
|
||||
tags = pref[1];
|
||||
pref = pref[0];
|
||||
}
|
||||
|
||||
switch (pref) {
|
||||
case 'p':
|
||||
pref = 'primary';
|
||||
break;
|
||||
case 'pp':
|
||||
pref = 'primaryPreferred';
|
||||
break;
|
||||
case 's':
|
||||
pref = 'secondary';
|
||||
break;
|
||||
case 'sp':
|
||||
pref = 'secondaryPreferred';
|
||||
break;
|
||||
case 'n':
|
||||
pref = 'nearest';
|
||||
break;
|
||||
}
|
||||
|
||||
return new ReadPref(pref, tags);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Populate options constructor
|
||||
*/
|
||||
|
||||
function PopulateOptions (path, select, match, options, model) {
|
||||
function PopulateOptions(path, select, match, options, model, subPopulate) {
|
||||
this.path = path;
|
||||
this.match = match;
|
||||
this.select = select;
|
||||
this.options = options;
|
||||
this.model = model;
|
||||
if (typeof subPopulate === 'object') {
|
||||
this.populate = subPopulate;
|
||||
}
|
||||
this._docs = {};
|
||||
}
|
||||
|
||||
@ -522,7 +529,7 @@ exports.PopulateOptions = PopulateOptions;
|
||||
* populate helper
|
||||
*/
|
||||
|
||||
exports.populate = function populate (path, select, model, match, options) {
|
||||
exports.populate = function populate(path, select, model, match, options, subPopulate) {
|
||||
// The order of select/conditions args is opposite Model.find but
|
||||
// necessary to keep backward compatibility (select could be
|
||||
// an array, string, or object literal).
|
||||
@ -534,7 +541,7 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
}
|
||||
|
||||
if (Array.isArray(path)) {
|
||||
return path.map(function(o){
|
||||
return path.map(function(o) {
|
||||
return exports.populate(o)[0];
|
||||
});
|
||||
}
|
||||
@ -544,9 +551,10 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
options = path.options;
|
||||
select = path.select;
|
||||
model = path.model;
|
||||
subPopulate = path.populate;
|
||||
path = path.path;
|
||||
}
|
||||
} else if ('string' !== typeof model) {
|
||||
} else if ('string' !== typeof model && 'function' !== typeof model) {
|
||||
options = match;
|
||||
match = model;
|
||||
model = undefined;
|
||||
@ -556,14 +564,18 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
|
||||
}
|
||||
|
||||
if (typeof subPopulate === 'object') {
|
||||
subPopulate = exports.populate(subPopulate);
|
||||
}
|
||||
|
||||
var ret = [];
|
||||
var paths = path.split(' ');
|
||||
for (var i = 0; i < paths.length; ++i) {
|
||||
ret.push(new PopulateOptions(paths[i], select, match, options, model));
|
||||
ret.push(new PopulateOptions(paths[i], select, match, options, model, subPopulate));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Return the value of `obj` at the given `path`.
|
||||
@ -572,9 +584,9 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
* @param {Object} obj
|
||||
*/
|
||||
|
||||
exports.getValue = function (path, obj, map) {
|
||||
exports.getValue = function(path, obj, map) {
|
||||
return mpath.get(path, obj, '_doc', map);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Sets the value of `obj` at the given `path`.
|
||||
@ -584,9 +596,9 @@ exports.getValue = function (path, obj, map) {
|
||||
* @param {Object} obj
|
||||
*/
|
||||
|
||||
exports.setValue = function (path, val, obj, map) {
|
||||
exports.setValue = function(path, val, obj, map) {
|
||||
mpath.set(path, val, obj, '_doc', map);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Returns an array of values from object `o`.
|
||||
@ -597,17 +609,17 @@ exports.setValue = function (path, val, obj, map) {
|
||||
*/
|
||||
|
||||
exports.object = {};
|
||||
exports.object.vals = function vals (o) {
|
||||
var keys = Object.keys(o)
|
||||
, i = keys.length
|
||||
, ret = [];
|
||||
exports.object.vals = function vals(o) {
|
||||
var keys = Object.keys(o),
|
||||
i = keys.length,
|
||||
ret = [];
|
||||
|
||||
while (i--) {
|
||||
ret.push(o[keys[i]]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* @see exports.options
|
||||
@ -622,9 +634,10 @@ exports.object.shallowCopy = exports.options;
|
||||
* @param {String} prop
|
||||
*/
|
||||
|
||||
exports.object.hasOwnProperty = function (obj, prop) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, prop);
|
||||
}
|
||||
var hop = Object.prototype.hasOwnProperty;
|
||||
exports.object.hasOwnProperty = function(obj, prop) {
|
||||
return hop.call(obj, prop);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Determine if `val` is null or undefined
|
||||
@ -632,9 +645,9 @@ exports.object.hasOwnProperty = function (obj, prop) {
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
exports.isNullOrUndefined = function (val) {
|
||||
return null == val
|
||||
}
|
||||
exports.isNullOrUndefined = function(val) {
|
||||
return null == val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
@ -653,10 +666,10 @@ exports.array = {};
|
||||
* @private
|
||||
*/
|
||||
|
||||
exports.array.flatten = function flatten (arr, filter, ret) {
|
||||
exports.array.flatten = function flatten(arr, filter, ret) {
|
||||
ret || (ret = []);
|
||||
|
||||
arr.forEach(function (item) {
|
||||
arr.forEach(function(item) {
|
||||
if (Array.isArray(item)) {
|
||||
flatten(item, filter, ret);
|
||||
} else {
|
||||
@ -667,5 +680,118 @@ exports.array.flatten = function flatten (arr, filter, ret) {
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Removes duplicate values from an array
|
||||
*
|
||||
* [1, 2, 3, 3, 5] => [1, 2, 3, 5]
|
||||
* [ ObjectId("550988ba0c19d57f697dc45e"), ObjectId("550988ba0c19d57f697dc45e") ]
|
||||
* => [ObjectId("550988ba0c19d57f697dc45e")]
|
||||
*
|
||||
* @param {Array} arr
|
||||
* @return {Array}
|
||||
* @private
|
||||
*/
|
||||
|
||||
exports.array.unique = function(arr) {
|
||||
var primitives = {};
|
||||
var ids = {};
|
||||
var ret = [];
|
||||
var length = arr.length;
|
||||
for (var i = 0; i < length; ++i) {
|
||||
if (typeof arr[i] === 'number' || typeof arr[i] === 'string') {
|
||||
if (primitives[arr[i]]) {
|
||||
continue;
|
||||
}
|
||||
ret.push(arr[i]);
|
||||
primitives[arr[i]] = true;
|
||||
} else if (arr[i] instanceof ObjectId) {
|
||||
if (ids[arr[i].toString()]) {
|
||||
continue;
|
||||
}
|
||||
ret.push(arr[i]);
|
||||
ids[arr[i].toString()] = true;
|
||||
} else {
|
||||
ret.push(arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Determines if two buffers are equal.
|
||||
*
|
||||
* @param {Buffer} a
|
||||
* @param {Object} b
|
||||
*/
|
||||
|
||||
exports.buffer = {};
|
||||
exports.buffer.areEqual = function(a, b) {
|
||||
if (!Buffer.isBuffer(a)) return false;
|
||||
if (!Buffer.isBuffer(b)) return false;
|
||||
if (a.length !== b.length) return false;
|
||||
for (var i = 0, len = a.length; i < len; ++i) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
exports.getFunctionName = function(fn) {
|
||||
if (fn.name) {
|
||||
return fn.name;
|
||||
}
|
||||
return (fn.toString().trim().match(/^function\s*([^\s(]+)/) || [])[1];
|
||||
};
|
||||
|
||||
exports.decorate = function(destination, source) {
|
||||
for (var key in source) {
|
||||
destination[key] = source[key];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* merges to with a copy of from
|
||||
*
|
||||
* @param {Object} to
|
||||
* @param {Object} from
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.mergeClone = function(to, from) {
|
||||
var keys = Object.keys(from),
|
||||
i = keys.length,
|
||||
key;
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
if ('undefined' === typeof to[key]) {
|
||||
// make sure to retain key order here because of a bug handling the $each
|
||||
// operator in mongodb 2.4.4
|
||||
to[key] = exports.clone(from[key], { retainKeyOrder : 1});
|
||||
} else {
|
||||
if (exports.isObject(from[key])) {
|
||||
exports.mergeClone(to[key], from[key]);
|
||||
} else {
|
||||
// make sure to retain key order here because of a bug handling the
|
||||
// $each operator in mongodb 2.4.4
|
||||
to[key] = exports.clone(from[key], { retainKeyOrder : 1});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes a function on each element of an array (like _.each)
|
||||
*
|
||||
* @param {Array} arr
|
||||
* @param {Function} fn
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.each = function(arr, fn) {
|
||||
for (var i = 0; i < arr.length; ++i) {
|
||||
fn(arr[i]);
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user