utils.js
7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'use strict';
/*!
* Module dependencies.
*/
var RegExpClone = require('regexp-clone')
/**
* Clones objects
*
* @param {Object} obj the object to clone
* @param {Object} options
* @return {Object} the cloned object
* @api private
*/
var clone = exports.clone = function clone (obj, options) {
if (obj === undefined || obj === null)
return obj;
if (Array.isArray(obj))
return exports.cloneArray(obj, options);
if (obj.constructor) {
if (/ObjectI[dD]$/.test(obj.constructor.name)) {
return 'function' == typeof obj.clone
? obj.clone()
: new obj.constructor(obj.id);
}
if ('ReadPreference' === obj._type && obj.isValid && obj.toObject) {
return 'function' == typeof obj.clone
? obj.clone()
: new obj.constructor(obj.mode, clone(obj.tags, options));
}
if ('Binary' == obj._bsontype && obj.buffer && obj.value) {
return 'function' == typeof obj.clone
? obj.clone()
: new obj.constructor(obj.value(true), obj.sub_type);
}
if ('Date' === obj.constructor.name || 'Function' === obj.constructor.name)
return new obj.constructor(+obj);
if ('RegExp' === obj.constructor.name)
return RegExpClone(obj);
if ('Buffer' === obj.constructor.name)
return exports.cloneBuffer(obj);
}
if (isObject(obj))
return exports.cloneObject(obj, options);
if (obj.valueOf)
return obj.valueOf();
};
/*!
* ignore
*/
var cloneObject = exports.cloneObject = 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) {
val = clone(obj[k], options);
if (!minimize || ('undefined' !== typeof val)) {
hasKeys || (hasKeys = true);
ret[k] = val;
}
}
} else {
// faster
keys = Object.keys(obj);
i = keys.length;
while (i--) {
k = keys[i];
val = clone(obj[k], options);
if (!minimize || ('undefined' !== typeof val)) {
if (!hasKeys) hasKeys = true;
ret[k] = val;
}
}
}
return minimize
? hasKeys && ret
: ret;
};
var cloneArray = exports.cloneArray = function cloneArray (arr, options) {
var ret = [];
for (var i = 0, l = arr.length; i < l; i++)
ret.push(clone(arr[i], options));
return ret;
};
/**
* process.nextTick helper.
*
* Wraps the given `callback` in a try/catch. If an error is
* caught it will be thrown on nextTick.
*
* node-mongodb-native had a habit of state corruption when
* an error was immediately thrown from within a collection
* method (find, update, etc) callback.
*
* @param {Function} [callback]
* @api private
*/
var tick = exports.tick = function tick (callback) {
if ('function' !== typeof callback) return;
return function () {
// callbacks should always be fired on the next
// turn of the event loop. A side benefit is
// errors thrown from executing the callback
// will not cause drivers state to be corrupted
// which has historically been a problem.
var args = arguments;
soon(function(){
callback.apply(this, args);
});
}
}
/**
* Merges `from` into `to` without overwriting existing properties.
*
* @param {Object} to
* @param {Object} from
* @api private
*/
var merge = 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];
}
}
}
}
/**
* Same as merge but clones the assigned values.
*
* @param {Object} to
* @param {Object} from
* @api private
*/
var mergeClone = exports.mergeClone = function mergeClone (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] = clone(from[key], { retainKeyOrder : 1});
} else {
if (exports.isObject(from[key])) {
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] = clone(from[key], { retainKeyOrder : 1});
}
}
}
}
/**
* Read pref helper (mongo 2.2 drivers support this)
*
* Allows using aliases instead of full preference names:
*
* p primary
* pp primaryPreferred
* s secondary
* sp secondaryPreferred
* n nearest
*
* @param {String} pref
*/
exports.readPref = function readPref (pref) {
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 pref;
}
/**
* Object.prototype.toString.call helper
*/
var _toString = Object.prototype.toString;
var toString = exports.toString = function (arg) {
return _toString.call(arg);
}
/**
* Determines if `arg` is an object.
*
* @param {Object|Array|String|Function|RegExp|any} arg
* @return {Boolean}
*/
var isObject = exports.isObject = function (arg) {
return '[object Object]' == exports.toString(arg);
}
/**
* Determines if `arg` is an array.
*
* @param {Object}
* @return {Boolean}
* @see nodejs utils
*/
var isArray = exports.isArray = function (arg) {
return Array.isArray(arg) ||
'object' == typeof arg && '[object Array]' == exports.toString(arg);
}
/**
* Object.keys helper
*/
exports.keys = Object.keys || function (obj) {
var keys = [];
for (var k in obj) if (obj.hasOwnProperty(k)) {
keys.push(k);
}
return keys;
}
/**
* Basic Object.create polyfill.
* Only one argument is supported.
*
* Based on https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
*/
exports.create = 'function' == typeof Object.create
? Object.create
: create;
function create (proto) {
if (arguments.length > 1) {
throw new Error("Adding properties is not supported")
}
function F () {}
F.prototype = proto;
return new F;
}
/**
* inheritance
*/
exports.inherits = function (ctor, superCtor) {
ctor.prototype = exports.create(superCtor.prototype);
ctor.prototype.constructor = ctor;
}
/**
* nextTick helper
* compat with node 0.10 which behaves differently than previous versions
*/
var soon = exports.soon = 'function' == typeof setImmediate
? setImmediate
: process.nextTick;
/**
* Clones the contents of a buffer.
*
* @param {Buffer} buff
* @return {Buffer}
*/
exports.cloneBuffer = function (buff) {
var dupe = new Buffer(buff.length);
buff.copy(dupe, 0, 0, buff.length);
return dupe;
};
/**
* Check if this object is an arguments object
*
* @param {Any} v
* @return {Boolean}
*/
exports.isArgumentsObject = function(v) {
return Object.prototype.toString.call(v) === '[object Arguments]';
};