js_stack 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +1 -1
- data/README.md +6 -4
- data/lib/js_stack/version.rb +1 -1
- data/vendor/assets/javascripts/js_stack/base/underscore/1.7.0.js +1424 -0
- data/vendor/assets/javascripts/js_stack/base/underscore.js +1 -1
- metadata +3 -2
@@ -0,0 +1,1424 @@
|
|
1
|
+
// Underscore.js 1.7.0
|
2
|
+
// http://underscorejs.org
|
3
|
+
// (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
4
|
+
// Underscore may be freely distributed under the MIT license.
|
5
|
+
|
6
|
+
(function() {
|
7
|
+
|
8
|
+
// Baseline setup
|
9
|
+
// --------------
|
10
|
+
|
11
|
+
// Establish the root object, `window` in the browser, or `exports` on the server.
|
12
|
+
var root = this;
|
13
|
+
|
14
|
+
// Save the previous value of the `_` variable.
|
15
|
+
var previousUnderscore = root._;
|
16
|
+
|
17
|
+
// Save bytes in the minified (but not gzipped) version:
|
18
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
|
19
|
+
|
20
|
+
// Create quick reference variables for speed access to core prototypes.
|
21
|
+
var
|
22
|
+
push = ArrayProto.push,
|
23
|
+
slice = ArrayProto.slice,
|
24
|
+
concat = ArrayProto.concat,
|
25
|
+
toString = ObjProto.toString,
|
26
|
+
hasOwnProperty = ObjProto.hasOwnProperty;
|
27
|
+
|
28
|
+
// All **ECMAScript 5** native function implementations that we hope to use
|
29
|
+
// are declared here.
|
30
|
+
var
|
31
|
+
nativeIsArray = Array.isArray,
|
32
|
+
nativeKeys = Object.keys,
|
33
|
+
nativeBind = FuncProto.bind;
|
34
|
+
|
35
|
+
// Create a safe reference to the Underscore object for use below.
|
36
|
+
var _ = function(obj) {
|
37
|
+
if (obj instanceof _) return obj;
|
38
|
+
if (!(this instanceof _)) return new _(obj);
|
39
|
+
this._wrapped = obj;
|
40
|
+
};
|
41
|
+
|
42
|
+
// Export the Underscore object for **Node.js**, with
|
43
|
+
// backwards-compatibility for the old `require()` API. If we're in
|
44
|
+
// the browser, add `_` as a global object.
|
45
|
+
if (typeof exports !== 'undefined') {
|
46
|
+
if (typeof module !== 'undefined' && module.exports) {
|
47
|
+
exports = module.exports = _;
|
48
|
+
}
|
49
|
+
exports._ = _;
|
50
|
+
} else {
|
51
|
+
root._ = _;
|
52
|
+
}
|
53
|
+
|
54
|
+
// Current version.
|
55
|
+
_.VERSION = '1.7.0';
|
56
|
+
|
57
|
+
// Internal function that returns an efficient (for current engines) version
|
58
|
+
// of the passed-in callback, to be repeatedly applied in other Underscore
|
59
|
+
// functions.
|
60
|
+
var createCallback = function(func, context, argCount) {
|
61
|
+
if (context === void 0) return func;
|
62
|
+
switch (argCount == null ? 3 : argCount) {
|
63
|
+
case 1: return function(value) {
|
64
|
+
return func.call(context, value);
|
65
|
+
};
|
66
|
+
case 2: return function(value, other) {
|
67
|
+
return func.call(context, value, other);
|
68
|
+
};
|
69
|
+
case 3: return function(value, index, collection) {
|
70
|
+
return func.call(context, value, index, collection);
|
71
|
+
};
|
72
|
+
case 4: return function(accumulator, value, index, collection) {
|
73
|
+
return func.call(context, accumulator, value, index, collection);
|
74
|
+
};
|
75
|
+
}
|
76
|
+
return function() {
|
77
|
+
return func.apply(context, arguments);
|
78
|
+
};
|
79
|
+
};
|
80
|
+
|
81
|
+
// A mostly-internal function to generate callbacks that can be applied
|
82
|
+
// to each element in a collection, returning the desired result — either
|
83
|
+
// identity, an arbitrary callback, a property matcher, or a property accessor.
|
84
|
+
_.iteratee = function(value, context, argCount) {
|
85
|
+
if (value == null) return _.identity;
|
86
|
+
if (_.isFunction(value)) return createCallback(value, context, argCount);
|
87
|
+
if (_.isObject(value)) return _.matches(value);
|
88
|
+
return _.property(value);
|
89
|
+
};
|
90
|
+
|
91
|
+
// Collection Functions
|
92
|
+
// --------------------
|
93
|
+
|
94
|
+
// The cornerstone, an `each` implementation, aka `forEach`.
|
95
|
+
// Handles raw objects in addition to array-likes. Treats all
|
96
|
+
// sparse array-likes as if they were dense.
|
97
|
+
_.each = _.forEach = function(obj, iteratee, context) {
|
98
|
+
if (obj == null) return obj;
|
99
|
+
iteratee = createCallback(iteratee, context);
|
100
|
+
var i, length = obj.length;
|
101
|
+
if (length === +length) {
|
102
|
+
for (i = 0; i < length; i++) {
|
103
|
+
iteratee(obj[i], i, obj);
|
104
|
+
}
|
105
|
+
} else {
|
106
|
+
var keys = _.keys(obj);
|
107
|
+
for (i = 0, length = keys.length; i < length; i++) {
|
108
|
+
iteratee(obj[keys[i]], keys[i], obj);
|
109
|
+
}
|
110
|
+
}
|
111
|
+
return obj;
|
112
|
+
};
|
113
|
+
|
114
|
+
// Return the results of applying the iteratee to each element.
|
115
|
+
_.map = _.collect = function(obj, iteratee, context) {
|
116
|
+
if (obj == null) return [];
|
117
|
+
iteratee = _.iteratee(iteratee, context);
|
118
|
+
var keys = obj.length !== +obj.length && _.keys(obj),
|
119
|
+
length = (keys || obj).length,
|
120
|
+
results = Array(length),
|
121
|
+
currentKey;
|
122
|
+
for (var index = 0; index < length; index++) {
|
123
|
+
currentKey = keys ? keys[index] : index;
|
124
|
+
results[index] = iteratee(obj[currentKey], currentKey, obj);
|
125
|
+
}
|
126
|
+
return results;
|
127
|
+
};
|
128
|
+
|
129
|
+
var reduceError = 'Reduce of empty array with no initial value';
|
130
|
+
|
131
|
+
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
132
|
+
// or `foldl`.
|
133
|
+
_.reduce = _.foldl = _.inject = function(obj, iteratee, memo, context) {
|
134
|
+
if (obj == null) obj = [];
|
135
|
+
iteratee = createCallback(iteratee, context, 4);
|
136
|
+
var keys = obj.length !== +obj.length && _.keys(obj),
|
137
|
+
length = (keys || obj).length,
|
138
|
+
index = 0, currentKey;
|
139
|
+
if (arguments.length < 3) {
|
140
|
+
if (!length) throw new TypeError(reduceError);
|
141
|
+
memo = obj[keys ? keys[index++] : index++];
|
142
|
+
}
|
143
|
+
for (; index < length; index++) {
|
144
|
+
currentKey = keys ? keys[index] : index;
|
145
|
+
memo = iteratee(memo, obj[currentKey], currentKey, obj);
|
146
|
+
}
|
147
|
+
return memo;
|
148
|
+
};
|
149
|
+
|
150
|
+
// The right-associative version of reduce, also known as `foldr`.
|
151
|
+
_.reduceRight = _.foldr = function(obj, iteratee, memo, context) {
|
152
|
+
if (obj == null) obj = [];
|
153
|
+
iteratee = createCallback(iteratee, context, 4);
|
154
|
+
var keys = obj.length !== + obj.length && _.keys(obj),
|
155
|
+
index = (keys || obj).length,
|
156
|
+
currentKey;
|
157
|
+
if (arguments.length < 3) {
|
158
|
+
if (!index) throw new TypeError(reduceError);
|
159
|
+
memo = obj[keys ? keys[--index] : --index];
|
160
|
+
}
|
161
|
+
while (index--) {
|
162
|
+
currentKey = keys ? keys[index] : index;
|
163
|
+
memo = iteratee(memo, obj[currentKey], currentKey, obj);
|
164
|
+
}
|
165
|
+
return memo;
|
166
|
+
};
|
167
|
+
|
168
|
+
// Return the first value which passes a truth test. Aliased as `detect`.
|
169
|
+
_.find = _.detect = function(obj, predicate, context) {
|
170
|
+
var result;
|
171
|
+
predicate = _.iteratee(predicate, context);
|
172
|
+
_.some(obj, function(value, index, list) {
|
173
|
+
if (predicate(value, index, list)) {
|
174
|
+
result = value;
|
175
|
+
return true;
|
176
|
+
}
|
177
|
+
});
|
178
|
+
return result;
|
179
|
+
};
|
180
|
+
|
181
|
+
// Return all the elements that pass a truth test.
|
182
|
+
// Aliased as `select`.
|
183
|
+
_.filter = _.select = function(obj, predicate, context) {
|
184
|
+
var results = [];
|
185
|
+
if (obj == null) return results;
|
186
|
+
predicate = _.iteratee(predicate, context);
|
187
|
+
_.each(obj, function(value, index, list) {
|
188
|
+
if (predicate(value, index, list)) results.push(value);
|
189
|
+
});
|
190
|
+
return results;
|
191
|
+
};
|
192
|
+
|
193
|
+
// Return all the elements for which a truth test fails.
|
194
|
+
_.reject = function(obj, predicate, context) {
|
195
|
+
return _.filter(obj, _.negate(_.iteratee(predicate)), context);
|
196
|
+
};
|
197
|
+
|
198
|
+
// Determine whether all of the elements match a truth test.
|
199
|
+
// Aliased as `all`.
|
200
|
+
_.every = _.all = function(obj, predicate, context) {
|
201
|
+
if (obj == null) return true;
|
202
|
+
predicate = _.iteratee(predicate, context);
|
203
|
+
var keys = obj.length !== +obj.length && _.keys(obj),
|
204
|
+
length = (keys || obj).length,
|
205
|
+
index, currentKey;
|
206
|
+
for (index = 0; index < length; index++) {
|
207
|
+
currentKey = keys ? keys[index] : index;
|
208
|
+
if (!predicate(obj[currentKey], currentKey, obj)) return false;
|
209
|
+
}
|
210
|
+
return true;
|
211
|
+
};
|
212
|
+
|
213
|
+
// Determine if at least one element in the object matches a truth test.
|
214
|
+
// Aliased as `any`.
|
215
|
+
_.some = _.any = function(obj, predicate, context) {
|
216
|
+
if (obj == null) return false;
|
217
|
+
predicate = _.iteratee(predicate, context);
|
218
|
+
var keys = obj.length !== +obj.length && _.keys(obj),
|
219
|
+
length = (keys || obj).length,
|
220
|
+
index, currentKey;
|
221
|
+
for (index = 0; index < length; index++) {
|
222
|
+
currentKey = keys ? keys[index] : index;
|
223
|
+
if (predicate(obj[currentKey], currentKey, obj)) return true;
|
224
|
+
}
|
225
|
+
return false;
|
226
|
+
};
|
227
|
+
|
228
|
+
// Determine if the array or object contains a given value (using `===`).
|
229
|
+
// Aliased as `include`.
|
230
|
+
_.contains = _.include = function(obj, target) {
|
231
|
+
if (obj == null) return false;
|
232
|
+
if (obj.length !== +obj.length) obj = _.values(obj);
|
233
|
+
return _.indexOf(obj, target) >= 0;
|
234
|
+
};
|
235
|
+
|
236
|
+
// Invoke a method (with arguments) on every item in a collection.
|
237
|
+
_.invoke = function(obj, method) {
|
238
|
+
var args = slice.call(arguments, 2);
|
239
|
+
var isFunc = _.isFunction(method);
|
240
|
+
return _.map(obj, function(value) {
|
241
|
+
return (isFunc ? method : value[method]).apply(value, args);
|
242
|
+
});
|
243
|
+
};
|
244
|
+
|
245
|
+
// Convenience version of a common use case of `map`: fetching a property.
|
246
|
+
_.pluck = function(obj, key) {
|
247
|
+
return _.map(obj, _.property(key));
|
248
|
+
};
|
249
|
+
|
250
|
+
// Convenience version of a common use case of `filter`: selecting only objects
|
251
|
+
// containing specific `key:value` pairs.
|
252
|
+
_.where = function(obj, attrs) {
|
253
|
+
return _.filter(obj, _.matches(attrs));
|
254
|
+
};
|
255
|
+
|
256
|
+
// Convenience version of a common use case of `find`: getting the first object
|
257
|
+
// containing specific `key:value` pairs.
|
258
|
+
_.findWhere = function(obj, attrs) {
|
259
|
+
return _.find(obj, _.matches(attrs));
|
260
|
+
};
|
261
|
+
|
262
|
+
// Return the maximum element (or element-based computation).
|
263
|
+
_.max = function(obj, iteratee, context) {
|
264
|
+
var result = -Infinity, lastComputed = -Infinity,
|
265
|
+
value, computed;
|
266
|
+
if (iteratee == null && obj != null) {
|
267
|
+
obj = obj.length === +obj.length ? obj : _.values(obj);
|
268
|
+
for (var i = 0, length = obj.length; i < length; i++) {
|
269
|
+
value = obj[i];
|
270
|
+
if (value > result) {
|
271
|
+
result = value;
|
272
|
+
}
|
273
|
+
}
|
274
|
+
} else {
|
275
|
+
iteratee = _.iteratee(iteratee, context);
|
276
|
+
_.each(obj, function(value, index, list) {
|
277
|
+
computed = iteratee(value, index, list);
|
278
|
+
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
|
279
|
+
result = value;
|
280
|
+
lastComputed = computed;
|
281
|
+
}
|
282
|
+
});
|
283
|
+
}
|
284
|
+
return result;
|
285
|
+
};
|
286
|
+
|
287
|
+
// Return the minimum element (or element-based computation).
|
288
|
+
_.min = function(obj, iteratee, context) {
|
289
|
+
var result = Infinity, lastComputed = Infinity,
|
290
|
+
value, computed;
|
291
|
+
if (iteratee == null && obj != null) {
|
292
|
+
obj = obj.length === +obj.length ? obj : _.values(obj);
|
293
|
+
for (var i = 0, length = obj.length; i < length; i++) {
|
294
|
+
value = obj[i];
|
295
|
+
if (value < result) {
|
296
|
+
result = value;
|
297
|
+
}
|
298
|
+
}
|
299
|
+
} else {
|
300
|
+
iteratee = _.iteratee(iteratee, context);
|
301
|
+
_.each(obj, function(value, index, list) {
|
302
|
+
computed = iteratee(value, index, list);
|
303
|
+
if (computed < lastComputed || computed === Infinity && result === Infinity) {
|
304
|
+
result = value;
|
305
|
+
lastComputed = computed;
|
306
|
+
}
|
307
|
+
});
|
308
|
+
}
|
309
|
+
return result;
|
310
|
+
};
|
311
|
+
|
312
|
+
// Shuffle a collection, using the modern version of the
|
313
|
+
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
|
314
|
+
_.shuffle = function(obj) {
|
315
|
+
var set = obj && obj.length === +obj.length ? obj : _.values(obj);
|
316
|
+
var length = set.length;
|
317
|
+
var shuffled = Array(length);
|
318
|
+
for (var index = 0, rand; index < length; index++) {
|
319
|
+
rand = _.random(0, index);
|
320
|
+
if (rand !== index) shuffled[index] = shuffled[rand];
|
321
|
+
shuffled[rand] = set[index];
|
322
|
+
}
|
323
|
+
return shuffled;
|
324
|
+
};
|
325
|
+
|
326
|
+
// Sample **n** random values from a collection.
|
327
|
+
// If **n** is not specified, returns a single random element.
|
328
|
+
// The internal `guard` argument allows it to work with `map`.
|
329
|
+
_.sample = function(obj, n, guard) {
|
330
|
+
if (n == null || guard) {
|
331
|
+
if (obj.length !== +obj.length) obj = _.values(obj);
|
332
|
+
return obj[_.random(obj.length - 1)];
|
333
|
+
}
|
334
|
+
return _.shuffle(obj).slice(0, Math.max(0, n));
|
335
|
+
};
|
336
|
+
|
337
|
+
// Sort the object's values by a criterion produced by an iteratee.
|
338
|
+
_.sortBy = function(obj, iteratee, context) {
|
339
|
+
iteratee = _.iteratee(iteratee, context);
|
340
|
+
return _.pluck(_.map(obj, function(value, index, list) {
|
341
|
+
return {
|
342
|
+
value: value,
|
343
|
+
index: index,
|
344
|
+
criteria: iteratee(value, index, list)
|
345
|
+
};
|
346
|
+
}).sort(function(left, right) {
|
347
|
+
var a = left.criteria;
|
348
|
+
var b = right.criteria;
|
349
|
+
if (a !== b) {
|
350
|
+
if (a > b || a === void 0) return 1;
|
351
|
+
if (a < b || b === void 0) return -1;
|
352
|
+
}
|
353
|
+
return left.index - right.index;
|
354
|
+
}), 'value');
|
355
|
+
};
|
356
|
+
|
357
|
+
// An internal function used for aggregate "group by" operations.
|
358
|
+
var group = function(behavior) {
|
359
|
+
return function(obj, iteratee, context) {
|
360
|
+
var result = {};
|
361
|
+
iteratee = _.iteratee(iteratee, context);
|
362
|
+
_.each(obj, function(value, index) {
|
363
|
+
var key = iteratee(value, index, obj);
|
364
|
+
behavior(result, value, key);
|
365
|
+
});
|
366
|
+
return result;
|
367
|
+
};
|
368
|
+
};
|
369
|
+
|
370
|
+
// Groups the object's values by a criterion. Pass either a string attribute
|
371
|
+
// to group by, or a function that returns the criterion.
|
372
|
+
_.groupBy = group(function(result, value, key) {
|
373
|
+
if (_.has(result, key)) result[key].push(value); else result[key] = [value];
|
374
|
+
});
|
375
|
+
|
376
|
+
// Indexes the object's values by a criterion, similar to `groupBy`, but for
|
377
|
+
// when you know that your index values will be unique.
|
378
|
+
_.indexBy = group(function(result, value, key) {
|
379
|
+
result[key] = value;
|
380
|
+
});
|
381
|
+
|
382
|
+
// Counts instances of an object that group by a certain criterion. Pass
|
383
|
+
// either a string attribute to count by, or a function that returns the
|
384
|
+
// criterion.
|
385
|
+
_.countBy = group(function(result, value, key) {
|
386
|
+
if (_.has(result, key)) result[key]++; else result[key] = 1;
|
387
|
+
});
|
388
|
+
|
389
|
+
// Use a comparator function to figure out the smallest index at which
|
390
|
+
// an object should be inserted so as to maintain order. Uses binary search.
|
391
|
+
_.sortedIndex = function(array, obj, iteratee, context) {
|
392
|
+
iteratee = _.iteratee(iteratee, context, 1);
|
393
|
+
var value = iteratee(obj);
|
394
|
+
var low = 0, high = array.length;
|
395
|
+
while (low < high) {
|
396
|
+
var mid = low + high >>> 1;
|
397
|
+
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
|
398
|
+
}
|
399
|
+
return low;
|
400
|
+
};
|
401
|
+
|
402
|
+
// Safely create a real, live array from anything iterable.
|
403
|
+
_.toArray = function(obj) {
|
404
|
+
if (!obj) return [];
|
405
|
+
if (_.isArray(obj)) return slice.call(obj);
|
406
|
+
if (obj.length === +obj.length) return _.map(obj, _.identity);
|
407
|
+
return _.values(obj);
|
408
|
+
};
|
409
|
+
|
410
|
+
// Return the number of elements in an object.
|
411
|
+
_.size = function(obj) {
|
412
|
+
if (obj == null) return 0;
|
413
|
+
return obj.length === +obj.length ? obj.length : _.keys(obj).length;
|
414
|
+
};
|
415
|
+
|
416
|
+
// Split a collection into two arrays: one whose elements all satisfy the given
|
417
|
+
// predicate, and one whose elements all do not satisfy the predicate.
|
418
|
+
_.partition = function(obj, predicate, context) {
|
419
|
+
predicate = _.iteratee(predicate, context);
|
420
|
+
var pass = [], fail = [];
|
421
|
+
_.each(obj, function(value, key, obj) {
|
422
|
+
(predicate(value, key, obj) ? pass : fail).push(value);
|
423
|
+
});
|
424
|
+
return [pass, fail];
|
425
|
+
};
|
426
|
+
|
427
|
+
// Array Functions
|
428
|
+
// ---------------
|
429
|
+
|
430
|
+
// Get the first element of an array. Passing **n** will return the first N
|
431
|
+
// values in the array. Aliased as `head` and `take`. The **guard** check
|
432
|
+
// allows it to work with `_.map`.
|
433
|
+
_.first = _.head = _.take = function(array, n, guard) {
|
434
|
+
if (array == null) return void 0;
|
435
|
+
if (n == null || guard) return array[0];
|
436
|
+
if (n < 0) return [];
|
437
|
+
return slice.call(array, 0, n);
|
438
|
+
};
|
439
|
+
|
440
|
+
// Returns everything but the last entry of the array. Especially useful on
|
441
|
+
// the arguments object. Passing **n** will return all the values in
|
442
|
+
// the array, excluding the last N. The **guard** check allows it to work with
|
443
|
+
// `_.map`.
|
444
|
+
_.initial = function(array, n, guard) {
|
445
|
+
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
|
446
|
+
};
|
447
|
+
|
448
|
+
// Get the last element of an array. Passing **n** will return the last N
|
449
|
+
// values in the array. The **guard** check allows it to work with `_.map`.
|
450
|
+
_.last = function(array, n, guard) {
|
451
|
+
if (array == null) return void 0;
|
452
|
+
if (n == null || guard) return array[array.length - 1];
|
453
|
+
return slice.call(array, Math.max(array.length - n, 0));
|
454
|
+
};
|
455
|
+
|
456
|
+
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
|
457
|
+
// Especially useful on the arguments object. Passing an **n** will return
|
458
|
+
// the rest N values in the array. The **guard**
|
459
|
+
// check allows it to work with `_.map`.
|
460
|
+
_.rest = _.tail = _.drop = function(array, n, guard) {
|
461
|
+
return slice.call(array, n == null || guard ? 1 : n);
|
462
|
+
};
|
463
|
+
|
464
|
+
// Trim out all falsy values from an array.
|
465
|
+
_.compact = function(array) {
|
466
|
+
return _.filter(array, _.identity);
|
467
|
+
};
|
468
|
+
|
469
|
+
// Internal implementation of a recursive `flatten` function.
|
470
|
+
var flatten = function(input, shallow, strict, output) {
|
471
|
+
if (shallow && _.every(input, _.isArray)) {
|
472
|
+
return concat.apply(output, input);
|
473
|
+
}
|
474
|
+
for (var i = 0, length = input.length; i < length; i++) {
|
475
|
+
var value = input[i];
|
476
|
+
if (!_.isArray(value) && !_.isArguments(value)) {
|
477
|
+
if (!strict) output.push(value);
|
478
|
+
} else if (shallow) {
|
479
|
+
push.apply(output, value);
|
480
|
+
} else {
|
481
|
+
flatten(value, shallow, strict, output);
|
482
|
+
}
|
483
|
+
}
|
484
|
+
return output;
|
485
|
+
};
|
486
|
+
|
487
|
+
// Flatten out an array, either recursively (by default), or just one level.
|
488
|
+
_.flatten = function(array, shallow) {
|
489
|
+
return flatten(array, shallow, false, []);
|
490
|
+
};
|
491
|
+
|
492
|
+
// Return a version of the array that does not contain the specified value(s).
|
493
|
+
_.without = function(array) {
|
494
|
+
return _.difference(array, slice.call(arguments, 1));
|
495
|
+
};
|
496
|
+
|
497
|
+
// Produce a duplicate-free version of the array. If the array has already
|
498
|
+
// been sorted, you have the option of using a faster algorithm.
|
499
|
+
// Aliased as `unique`.
|
500
|
+
_.uniq = _.unique = function(array, isSorted, iteratee, context) {
|
501
|
+
if (array == null) return [];
|
502
|
+
if (!_.isBoolean(isSorted)) {
|
503
|
+
context = iteratee;
|
504
|
+
iteratee = isSorted;
|
505
|
+
isSorted = false;
|
506
|
+
}
|
507
|
+
if (iteratee != null) iteratee = _.iteratee(iteratee, context);
|
508
|
+
var result = [];
|
509
|
+
var seen = [];
|
510
|
+
for (var i = 0, length = array.length; i < length; i++) {
|
511
|
+
var value = array[i];
|
512
|
+
if (isSorted) {
|
513
|
+
if (!i || seen !== value) result.push(value);
|
514
|
+
seen = value;
|
515
|
+
} else if (iteratee) {
|
516
|
+
var computed = iteratee(value, i, array);
|
517
|
+
if (_.indexOf(seen, computed) < 0) {
|
518
|
+
seen.push(computed);
|
519
|
+
result.push(value);
|
520
|
+
}
|
521
|
+
} else if (_.indexOf(result, value) < 0) {
|
522
|
+
result.push(value);
|
523
|
+
}
|
524
|
+
}
|
525
|
+
return result;
|
526
|
+
};
|
527
|
+
|
528
|
+
// Produce an array that contains the union: each distinct element from all of
|
529
|
+
// the passed-in arrays.
|
530
|
+
_.union = function() {
|
531
|
+
return _.uniq(flatten(arguments, true, true, []));
|
532
|
+
};
|
533
|
+
|
534
|
+
// Produce an array that contains every item shared between all the
|
535
|
+
// passed-in arrays.
|
536
|
+
_.intersection = function(array) {
|
537
|
+
if (array == null) return [];
|
538
|
+
var result = [];
|
539
|
+
var argsLength = arguments.length;
|
540
|
+
for (var i = 0, length = array.length; i < length; i++) {
|
541
|
+
var item = array[i];
|
542
|
+
if (_.contains(result, item)) continue;
|
543
|
+
for (var j = 1; j < argsLength; j++) {
|
544
|
+
if (!_.contains(arguments[j], item)) break;
|
545
|
+
}
|
546
|
+
if (j === argsLength) result.push(item);
|
547
|
+
}
|
548
|
+
return result;
|
549
|
+
};
|
550
|
+
|
551
|
+
// Take the difference between one array and a number of other arrays.
|
552
|
+
// Only the elements present in just the first array will remain.
|
553
|
+
_.difference = function(array) {
|
554
|
+
var rest = flatten(slice.call(arguments, 1), true, true, []);
|
555
|
+
return _.filter(array, function(value){
|
556
|
+
return !_.contains(rest, value);
|
557
|
+
});
|
558
|
+
};
|
559
|
+
|
560
|
+
// Zip together multiple lists into a single array -- elements that share
|
561
|
+
// an index go together.
|
562
|
+
_.zip = function(array) {
|
563
|
+
if (array == null) return [];
|
564
|
+
var length = _.max(arguments, 'length').length;
|
565
|
+
var results = Array(length);
|
566
|
+
while (length-- > 0) {
|
567
|
+
results[length] = _.pluck(arguments, length);
|
568
|
+
}
|
569
|
+
return results;
|
570
|
+
};
|
571
|
+
|
572
|
+
// Complement of _.zip. Unzip accepts an array of arrays and groups
|
573
|
+
// each array's elements on shared indices
|
574
|
+
_.unzip = function(array) {
|
575
|
+
return _.zip.apply(null, array);
|
576
|
+
};
|
577
|
+
|
578
|
+
// Converts lists into objects. Pass either a single array of `[key, value]`
|
579
|
+
// pairs, or two parallel arrays of the same length -- one of keys, and one of
|
580
|
+
// the corresponding values.
|
581
|
+
_.object = function(list, values) {
|
582
|
+
if (list == null) return {};
|
583
|
+
var result = {};
|
584
|
+
for (var i = 0, length = list.length; i < length; i++) {
|
585
|
+
if (values) {
|
586
|
+
result[list[i]] = values[i];
|
587
|
+
} else {
|
588
|
+
result[list[i][0]] = list[i][1];
|
589
|
+
}
|
590
|
+
}
|
591
|
+
return result;
|
592
|
+
};
|
593
|
+
|
594
|
+
// Return the position of the first occurrence of an item in an array,
|
595
|
+
// or -1 if the item is not included in the array.
|
596
|
+
// If the array is large and already in sort order, pass `true`
|
597
|
+
// for **isSorted** to use binary search.
|
598
|
+
_.indexOf = function(array, item, isSorted) {
|
599
|
+
if (array == null) return -1;
|
600
|
+
var i = 0, length = array.length;
|
601
|
+
if (isSorted) {
|
602
|
+
if (typeof isSorted == 'number') {
|
603
|
+
i = isSorted < 0 ? Math.max(0, length + isSorted) : isSorted;
|
604
|
+
} else {
|
605
|
+
i = _.sortedIndex(array, item);
|
606
|
+
return array[i] === item ? i : -1;
|
607
|
+
}
|
608
|
+
}
|
609
|
+
for (; i < length; i++) if (array[i] === item) return i;
|
610
|
+
return -1;
|
611
|
+
};
|
612
|
+
|
613
|
+
_.lastIndexOf = function(array, item, from) {
|
614
|
+
if (array == null) return -1;
|
615
|
+
var idx = array.length;
|
616
|
+
if (typeof from == 'number') {
|
617
|
+
idx = from < 0 ? idx + from + 1 : Math.min(idx, from + 1);
|
618
|
+
}
|
619
|
+
while (--idx >= 0) if (array[idx] === item) return idx;
|
620
|
+
return -1;
|
621
|
+
};
|
622
|
+
|
623
|
+
// Generate an integer Array containing an arithmetic progression. A port of
|
624
|
+
// the native Python `range()` function. See
|
625
|
+
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
626
|
+
_.range = function(start, stop, step) {
|
627
|
+
if (arguments.length <= 1) {
|
628
|
+
stop = start || 0;
|
629
|
+
start = 0;
|
630
|
+
}
|
631
|
+
step = step || 1;
|
632
|
+
|
633
|
+
var length = Math.max(Math.ceil((stop - start) / step), 0);
|
634
|
+
var range = Array(length);
|
635
|
+
|
636
|
+
for (var idx = 0; idx < length; idx++, start += step) {
|
637
|
+
range[idx] = start;
|
638
|
+
}
|
639
|
+
|
640
|
+
return range;
|
641
|
+
};
|
642
|
+
|
643
|
+
// Function (ahem) Functions
|
644
|
+
// ------------------
|
645
|
+
|
646
|
+
// Reusable constructor function for prototype setting.
|
647
|
+
var Ctor = function(){};
|
648
|
+
|
649
|
+
// Create a function bound to a given object (assigning `this`, and arguments,
|
650
|
+
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
|
651
|
+
// available.
|
652
|
+
_.bind = function(func, context) {
|
653
|
+
var args, bound;
|
654
|
+
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
|
655
|
+
if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
|
656
|
+
args = slice.call(arguments, 2);
|
657
|
+
bound = function() {
|
658
|
+
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
|
659
|
+
Ctor.prototype = func.prototype;
|
660
|
+
var self = new Ctor;
|
661
|
+
Ctor.prototype = null;
|
662
|
+
var result = func.apply(self, args.concat(slice.call(arguments)));
|
663
|
+
if (_.isObject(result)) return result;
|
664
|
+
return self;
|
665
|
+
};
|
666
|
+
return bound;
|
667
|
+
};
|
668
|
+
|
669
|
+
// Partially apply a function by creating a version that has had some of its
|
670
|
+
// arguments pre-filled, without changing its dynamic `this` context. _ acts
|
671
|
+
// as a placeholder, allowing any combination of arguments to be pre-filled.
|
672
|
+
_.partial = function(func) {
|
673
|
+
var boundArgs = slice.call(arguments, 1);
|
674
|
+
return function() {
|
675
|
+
var position = 0;
|
676
|
+
var args = boundArgs.slice();
|
677
|
+
for (var i = 0, length = args.length; i < length; i++) {
|
678
|
+
if (args[i] === _) args[i] = arguments[position++];
|
679
|
+
}
|
680
|
+
while (position < arguments.length) args.push(arguments[position++]);
|
681
|
+
return func.apply(this, args);
|
682
|
+
};
|
683
|
+
};
|
684
|
+
|
685
|
+
// Bind a number of an object's methods to that object. Remaining arguments
|
686
|
+
// are the method names to be bound. Useful for ensuring that all callbacks
|
687
|
+
// defined on an object belong to it.
|
688
|
+
_.bindAll = function(obj) {
|
689
|
+
var i, length = arguments.length, key;
|
690
|
+
if (length <= 1) throw new Error('bindAll must be passed function names');
|
691
|
+
for (i = 1; i < length; i++) {
|
692
|
+
key = arguments[i];
|
693
|
+
obj[key] = _.bind(obj[key], obj);
|
694
|
+
}
|
695
|
+
return obj;
|
696
|
+
};
|
697
|
+
|
698
|
+
// Memoize an expensive function by storing its results.
|
699
|
+
_.memoize = function(func, hasher) {
|
700
|
+
var memoize = function(key) {
|
701
|
+
var cache = memoize.cache;
|
702
|
+
var address = hasher ? hasher.apply(this, arguments) : key;
|
703
|
+
if (!_.has(cache, address)) cache[address] = func.apply(this, arguments);
|
704
|
+
return cache[address];
|
705
|
+
};
|
706
|
+
memoize.cache = {};
|
707
|
+
return memoize;
|
708
|
+
};
|
709
|
+
|
710
|
+
// Delays a function for the given number of milliseconds, and then calls
|
711
|
+
// it with the arguments supplied.
|
712
|
+
_.delay = function(func, wait) {
|
713
|
+
var args = slice.call(arguments, 2);
|
714
|
+
return setTimeout(function(){
|
715
|
+
return func.apply(null, args);
|
716
|
+
}, wait);
|
717
|
+
};
|
718
|
+
|
719
|
+
// Defers a function, scheduling it to run after the current call stack has
|
720
|
+
// cleared.
|
721
|
+
_.defer = function(func) {
|
722
|
+
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
723
|
+
};
|
724
|
+
|
725
|
+
// Returns a function, that, when invoked, will only be triggered at most once
|
726
|
+
// during a given window of time. Normally, the throttled function will run
|
727
|
+
// as much as it can, without ever going more than once per `wait` duration;
|
728
|
+
// but if you'd like to disable the execution on the leading edge, pass
|
729
|
+
// `{leading: false}`. To disable execution on the trailing edge, ditto.
|
730
|
+
_.throttle = function(func, wait, options) {
|
731
|
+
var context, args, result;
|
732
|
+
var timeout = null;
|
733
|
+
var previous = 0;
|
734
|
+
if (!options) options = {};
|
735
|
+
var later = function() {
|
736
|
+
previous = options.leading === false ? 0 : _.now();
|
737
|
+
timeout = null;
|
738
|
+
result = func.apply(context, args);
|
739
|
+
if (!timeout) context = args = null;
|
740
|
+
};
|
741
|
+
return function() {
|
742
|
+
var now = _.now();
|
743
|
+
if (!previous && options.leading === false) previous = now;
|
744
|
+
var remaining = wait - (now - previous);
|
745
|
+
context = this;
|
746
|
+
args = arguments;
|
747
|
+
if (remaining <= 0 || remaining > wait) {
|
748
|
+
clearTimeout(timeout);
|
749
|
+
timeout = null;
|
750
|
+
previous = now;
|
751
|
+
result = func.apply(context, args);
|
752
|
+
if (!timeout) context = args = null;
|
753
|
+
} else if (!timeout && options.trailing !== false) {
|
754
|
+
timeout = setTimeout(later, remaining);
|
755
|
+
}
|
756
|
+
return result;
|
757
|
+
};
|
758
|
+
};
|
759
|
+
|
760
|
+
// Returns a function, that, as long as it continues to be invoked, will not
|
761
|
+
// be triggered. The function will be called after it stops being called for
|
762
|
+
// N milliseconds. If `immediate` is passed, trigger the function on the
|
763
|
+
// leading edge, instead of the trailing.
|
764
|
+
_.debounce = function(func, wait, immediate) {
|
765
|
+
var timeout, args, context, timestamp, result;
|
766
|
+
|
767
|
+
var later = function() {
|
768
|
+
var last = _.now() - timestamp;
|
769
|
+
|
770
|
+
if (last < wait && last > 0) {
|
771
|
+
timeout = setTimeout(later, wait - last);
|
772
|
+
} else {
|
773
|
+
timeout = null;
|
774
|
+
if (!immediate) {
|
775
|
+
result = func.apply(context, args);
|
776
|
+
if (!timeout) context = args = null;
|
777
|
+
}
|
778
|
+
}
|
779
|
+
};
|
780
|
+
|
781
|
+
return function() {
|
782
|
+
context = this;
|
783
|
+
args = arguments;
|
784
|
+
timestamp = _.now();
|
785
|
+
var callNow = immediate && !timeout;
|
786
|
+
if (!timeout) timeout = setTimeout(later, wait);
|
787
|
+
if (callNow) {
|
788
|
+
result = func.apply(context, args);
|
789
|
+
context = args = null;
|
790
|
+
}
|
791
|
+
|
792
|
+
return result;
|
793
|
+
};
|
794
|
+
};
|
795
|
+
|
796
|
+
// Returns the first function passed as an argument to the second,
|
797
|
+
// allowing you to adjust arguments, run code before and after, and
|
798
|
+
// conditionally execute the original function.
|
799
|
+
_.wrap = function(func, wrapper) {
|
800
|
+
return _.partial(wrapper, func);
|
801
|
+
};
|
802
|
+
|
803
|
+
// Returns a negated version of the passed-in predicate.
|
804
|
+
_.negate = function(predicate) {
|
805
|
+
return function() {
|
806
|
+
return !predicate.apply(this, arguments);
|
807
|
+
};
|
808
|
+
};
|
809
|
+
|
810
|
+
// Returns a function that is the composition of a list of functions, each
|
811
|
+
// consuming the return value of the function that follows.
|
812
|
+
_.compose = function() {
|
813
|
+
var args = arguments;
|
814
|
+
var start = args.length - 1;
|
815
|
+
return function() {
|
816
|
+
var i = start;
|
817
|
+
var result = args[start].apply(this, arguments);
|
818
|
+
while (i--) result = args[i].call(this, result);
|
819
|
+
return result;
|
820
|
+
};
|
821
|
+
};
|
822
|
+
|
823
|
+
// Returns a function that will only be executed after being called N times.
|
824
|
+
_.after = function(times, func) {
|
825
|
+
return function() {
|
826
|
+
if (--times < 1) {
|
827
|
+
return func.apply(this, arguments);
|
828
|
+
}
|
829
|
+
};
|
830
|
+
};
|
831
|
+
|
832
|
+
// Returns a function that will only be executed before being called N times.
|
833
|
+
_.before = function(times, func) {
|
834
|
+
var memo;
|
835
|
+
return function() {
|
836
|
+
if (--times > 0) {
|
837
|
+
memo = func.apply(this, arguments);
|
838
|
+
} else {
|
839
|
+
func = null;
|
840
|
+
}
|
841
|
+
return memo;
|
842
|
+
};
|
843
|
+
};
|
844
|
+
|
845
|
+
// Returns a function that will be executed at most one time, no matter how
|
846
|
+
// often you call it. Useful for lazy initialization.
|
847
|
+
_.once = _.partial(_.before, 2);
|
848
|
+
|
849
|
+
// Object Functions
|
850
|
+
// ----------------
|
851
|
+
|
852
|
+
// Retrieve the names of an object's properties.
|
853
|
+
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
854
|
+
_.keys = function(obj) {
|
855
|
+
if (!_.isObject(obj)) return [];
|
856
|
+
if (nativeKeys) return nativeKeys(obj);
|
857
|
+
var keys = [];
|
858
|
+
for (var key in obj) if (_.has(obj, key)) keys.push(key);
|
859
|
+
return keys;
|
860
|
+
};
|
861
|
+
|
862
|
+
// Retrieve the values of an object's properties.
|
863
|
+
_.values = function(obj) {
|
864
|
+
var keys = _.keys(obj);
|
865
|
+
var length = keys.length;
|
866
|
+
var values = Array(length);
|
867
|
+
for (var i = 0; i < length; i++) {
|
868
|
+
values[i] = obj[keys[i]];
|
869
|
+
}
|
870
|
+
return values;
|
871
|
+
};
|
872
|
+
|
873
|
+
// Convert an object into a list of `[key, value]` pairs.
|
874
|
+
_.pairs = function(obj) {
|
875
|
+
var keys = _.keys(obj);
|
876
|
+
var length = keys.length;
|
877
|
+
var pairs = Array(length);
|
878
|
+
for (var i = 0; i < length; i++) {
|
879
|
+
pairs[i] = [keys[i], obj[keys[i]]];
|
880
|
+
}
|
881
|
+
return pairs;
|
882
|
+
};
|
883
|
+
|
884
|
+
// Invert the keys and values of an object. The values must be serializable.
|
885
|
+
_.invert = function(obj) {
|
886
|
+
var result = {};
|
887
|
+
var keys = _.keys(obj);
|
888
|
+
for (var i = 0, length = keys.length; i < length; i++) {
|
889
|
+
result[obj[keys[i]]] = keys[i];
|
890
|
+
}
|
891
|
+
return result;
|
892
|
+
};
|
893
|
+
|
894
|
+
// Return a sorted list of the function names available on the object.
|
895
|
+
// Aliased as `methods`
|
896
|
+
_.functions = _.methods = function(obj) {
|
897
|
+
var names = [];
|
898
|
+
for (var key in obj) {
|
899
|
+
if (_.isFunction(obj[key])) names.push(key);
|
900
|
+
}
|
901
|
+
return names.sort();
|
902
|
+
};
|
903
|
+
|
904
|
+
// Extend a given object with all the properties in passed-in object(s).
|
905
|
+
_.extend = function(obj) {
|
906
|
+
if (!_.isObject(obj)) return obj;
|
907
|
+
var source, prop;
|
908
|
+
for (var i = 1, length = arguments.length; i < length; i++) {
|
909
|
+
source = arguments[i];
|
910
|
+
for (prop in source) {
|
911
|
+
if (hasOwnProperty.call(source, prop)) {
|
912
|
+
obj[prop] = source[prop];
|
913
|
+
}
|
914
|
+
}
|
915
|
+
}
|
916
|
+
return obj;
|
917
|
+
};
|
918
|
+
|
919
|
+
// Return a copy of the object only containing the whitelisted properties.
|
920
|
+
_.pick = function(obj, iteratee, context) {
|
921
|
+
var result = {}, key;
|
922
|
+
if (obj == null) return result;
|
923
|
+
if (_.isFunction(iteratee)) {
|
924
|
+
iteratee = createCallback(iteratee, context);
|
925
|
+
for (key in obj) {
|
926
|
+
var value = obj[key];
|
927
|
+
if (iteratee(value, key, obj)) result[key] = value;
|
928
|
+
}
|
929
|
+
} else {
|
930
|
+
var keys = concat.apply([], slice.call(arguments, 1));
|
931
|
+
obj = new Object(obj);
|
932
|
+
for (var i = 0, length = keys.length; i < length; i++) {
|
933
|
+
key = keys[i];
|
934
|
+
if (key in obj) result[key] = obj[key];
|
935
|
+
}
|
936
|
+
}
|
937
|
+
return result;
|
938
|
+
};
|
939
|
+
|
940
|
+
// Return a copy of the object without the blacklisted properties.
|
941
|
+
_.omit = function(obj, iteratee, context) {
|
942
|
+
if (_.isFunction(iteratee)) {
|
943
|
+
iteratee = _.negate(iteratee);
|
944
|
+
} else {
|
945
|
+
var keys = _.map(concat.apply([], slice.call(arguments, 1)), String);
|
946
|
+
iteratee = function(value, key) {
|
947
|
+
return !_.contains(keys, key);
|
948
|
+
};
|
949
|
+
}
|
950
|
+
return _.pick(obj, iteratee, context);
|
951
|
+
};
|
952
|
+
|
953
|
+
// Fill in a given object with default properties.
|
954
|
+
_.defaults = function(obj) {
|
955
|
+
if (!_.isObject(obj)) return obj;
|
956
|
+
for (var i = 1, length = arguments.length; i < length; i++) {
|
957
|
+
var source = arguments[i];
|
958
|
+
for (var prop in source) {
|
959
|
+
if (obj[prop] === void 0) obj[prop] = source[prop];
|
960
|
+
}
|
961
|
+
}
|
962
|
+
return obj;
|
963
|
+
};
|
964
|
+
|
965
|
+
// Create a (shallow-cloned) duplicate of an object.
|
966
|
+
_.clone = function(obj) {
|
967
|
+
if (!_.isObject(obj)) return obj;
|
968
|
+
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
|
969
|
+
};
|
970
|
+
|
971
|
+
// Invokes interceptor with the obj, and then returns obj.
|
972
|
+
// The primary purpose of this method is to "tap into" a method chain, in
|
973
|
+
// order to perform operations on intermediate results within the chain.
|
974
|
+
_.tap = function(obj, interceptor) {
|
975
|
+
interceptor(obj);
|
976
|
+
return obj;
|
977
|
+
};
|
978
|
+
|
979
|
+
// Internal recursive comparison function for `isEqual`.
|
980
|
+
var eq = function(a, b, aStack, bStack) {
|
981
|
+
// Identical objects are equal. `0 === -0`, but they aren't identical.
|
982
|
+
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
|
983
|
+
if (a === b) return a !== 0 || 1 / a === 1 / b;
|
984
|
+
// A strict comparison is necessary because `null == undefined`.
|
985
|
+
if (a == null || b == null) return a === b;
|
986
|
+
// Unwrap any wrapped objects.
|
987
|
+
if (a instanceof _) a = a._wrapped;
|
988
|
+
if (b instanceof _) b = b._wrapped;
|
989
|
+
// Compare `[[Class]]` names.
|
990
|
+
var className = toString.call(a);
|
991
|
+
if (className !== toString.call(b)) return false;
|
992
|
+
switch (className) {
|
993
|
+
// Strings, numbers, regular expressions, dates, and booleans are compared by value.
|
994
|
+
case '[object RegExp]':
|
995
|
+
// RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
|
996
|
+
case '[object String]':
|
997
|
+
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
|
998
|
+
// equivalent to `new String("5")`.
|
999
|
+
return '' + a === '' + b;
|
1000
|
+
case '[object Number]':
|
1001
|
+
// `NaN`s are equivalent, but non-reflexive.
|
1002
|
+
// Object(NaN) is equivalent to NaN
|
1003
|
+
if (+a !== +a) return +b !== +b;
|
1004
|
+
// An `egal` comparison is performed for other numeric values.
|
1005
|
+
return +a === 0 ? 1 / +a === 1 / b : +a === +b;
|
1006
|
+
case '[object Date]':
|
1007
|
+
case '[object Boolean]':
|
1008
|
+
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
|
1009
|
+
// millisecond representations. Note that invalid dates with millisecond representations
|
1010
|
+
// of `NaN` are not equivalent.
|
1011
|
+
return +a === +b;
|
1012
|
+
}
|
1013
|
+
|
1014
|
+
var areArrays = className === '[object Array]';
|
1015
|
+
if (!areArrays) {
|
1016
|
+
if (typeof a != 'object' || typeof b != 'object') return false;
|
1017
|
+
|
1018
|
+
// Objects with different constructors are not equivalent, but `Object`s or `Array`s
|
1019
|
+
// from different frames are.
|
1020
|
+
var aCtor = a.constructor, bCtor = b.constructor;
|
1021
|
+
if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor &&
|
1022
|
+
_.isFunction(bCtor) && bCtor instanceof bCtor)
|
1023
|
+
&& ('constructor' in a && 'constructor' in b)) {
|
1024
|
+
return false;
|
1025
|
+
}
|
1026
|
+
}
|
1027
|
+
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
1028
|
+
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
1029
|
+
var length = aStack.length;
|
1030
|
+
while (length--) {
|
1031
|
+
// Linear search. Performance is inversely proportional to the number of
|
1032
|
+
// unique nested structures.
|
1033
|
+
if (aStack[length] === a) return bStack[length] === b;
|
1034
|
+
}
|
1035
|
+
|
1036
|
+
// Add the first object to the stack of traversed objects.
|
1037
|
+
aStack.push(a);
|
1038
|
+
bStack.push(b);
|
1039
|
+
var size, result;
|
1040
|
+
// Recursively compare objects and arrays.
|
1041
|
+
if (areArrays) {
|
1042
|
+
// Compare array lengths to determine if a deep comparison is necessary.
|
1043
|
+
size = a.length;
|
1044
|
+
result = size === b.length;
|
1045
|
+
if (result) {
|
1046
|
+
// Deep compare the contents, ignoring non-numeric properties.
|
1047
|
+
while (size--) {
|
1048
|
+
if (!(result = eq(a[size], b[size], aStack, bStack))) break;
|
1049
|
+
}
|
1050
|
+
}
|
1051
|
+
} else {
|
1052
|
+
// Deep compare objects.
|
1053
|
+
var keys = _.keys(a), key;
|
1054
|
+
size = keys.length;
|
1055
|
+
// Ensure that both objects contain the same number of properties before comparing deep equality.
|
1056
|
+
result = _.keys(b).length === size;
|
1057
|
+
if (result) {
|
1058
|
+
while (size--) {
|
1059
|
+
// Deep compare each member
|
1060
|
+
key = keys[size];
|
1061
|
+
if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
|
1062
|
+
}
|
1063
|
+
}
|
1064
|
+
}
|
1065
|
+
// Remove the first object from the stack of traversed objects.
|
1066
|
+
aStack.pop();
|
1067
|
+
bStack.pop();
|
1068
|
+
return result;
|
1069
|
+
};
|
1070
|
+
|
1071
|
+
// Perform a deep comparison to check if two objects are equal.
|
1072
|
+
_.isEqual = function(a, b) {
|
1073
|
+
return eq(a, b, [], []);
|
1074
|
+
};
|
1075
|
+
|
1076
|
+
// Is a given array, string, or object empty?
|
1077
|
+
// An "empty" object has no enumerable own-properties.
|
1078
|
+
_.isEmpty = function(obj) {
|
1079
|
+
if (obj == null) return true;
|
1080
|
+
if (_.isArray(obj) || _.isString(obj) || _.isArguments(obj)) return obj.length === 0;
|
1081
|
+
for (var key in obj) if (_.has(obj, key)) return false;
|
1082
|
+
return true;
|
1083
|
+
};
|
1084
|
+
|
1085
|
+
// Is a given value a DOM element?
|
1086
|
+
_.isElement = function(obj) {
|
1087
|
+
return !!(obj && obj.nodeType === 1);
|
1088
|
+
};
|
1089
|
+
|
1090
|
+
// Is a given value an array?
|
1091
|
+
// Delegates to ECMA5's native Array.isArray
|
1092
|
+
_.isArray = nativeIsArray || function(obj) {
|
1093
|
+
return toString.call(obj) === '[object Array]';
|
1094
|
+
};
|
1095
|
+
|
1096
|
+
// Is a given variable an object?
|
1097
|
+
_.isObject = function(obj) {
|
1098
|
+
var type = typeof obj;
|
1099
|
+
return type === 'function' || type === 'object' && !!obj;
|
1100
|
+
};
|
1101
|
+
|
1102
|
+
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
|
1103
|
+
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
|
1104
|
+
_['is' + name] = function(obj) {
|
1105
|
+
return toString.call(obj) === '[object ' + name + ']';
|
1106
|
+
};
|
1107
|
+
});
|
1108
|
+
|
1109
|
+
// Define a fallback version of the method in browsers (ahem, IE), where
|
1110
|
+
// there isn't any inspectable "Arguments" type.
|
1111
|
+
if (!_.isArguments(arguments)) {
|
1112
|
+
_.isArguments = function(obj) {
|
1113
|
+
return _.has(obj, 'callee');
|
1114
|
+
};
|
1115
|
+
}
|
1116
|
+
|
1117
|
+
// Optimize `isFunction` if appropriate. Work around an IE 11 bug.
|
1118
|
+
if (typeof /./ !== 'function') {
|
1119
|
+
_.isFunction = function(obj) {
|
1120
|
+
return typeof obj == 'function' || false;
|
1121
|
+
};
|
1122
|
+
}
|
1123
|
+
|
1124
|
+
// Is a given object a finite number?
|
1125
|
+
_.isFinite = function(obj) {
|
1126
|
+
return isFinite(obj) && !isNaN(parseFloat(obj));
|
1127
|
+
};
|
1128
|
+
|
1129
|
+
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
|
1130
|
+
_.isNaN = function(obj) {
|
1131
|
+
return _.isNumber(obj) && obj !== +obj;
|
1132
|
+
};
|
1133
|
+
|
1134
|
+
// Is a given value a boolean?
|
1135
|
+
_.isBoolean = function(obj) {
|
1136
|
+
return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
|
1137
|
+
};
|
1138
|
+
|
1139
|
+
// Is a given value equal to null?
|
1140
|
+
_.isNull = function(obj) {
|
1141
|
+
return obj === null;
|
1142
|
+
};
|
1143
|
+
|
1144
|
+
// Is a given variable undefined?
|
1145
|
+
_.isUndefined = function(obj) {
|
1146
|
+
return obj === void 0;
|
1147
|
+
};
|
1148
|
+
|
1149
|
+
// Shortcut function for checking if an object has a given property directly
|
1150
|
+
// on itself (in other words, not on a prototype).
|
1151
|
+
_.has = function(obj, key) {
|
1152
|
+
return obj != null && hasOwnProperty.call(obj, key);
|
1153
|
+
};
|
1154
|
+
|
1155
|
+
// Utility Functions
|
1156
|
+
// -----------------
|
1157
|
+
|
1158
|
+
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
|
1159
|
+
// previous owner. Returns a reference to the Underscore object.
|
1160
|
+
_.noConflict = function() {
|
1161
|
+
root._ = previousUnderscore;
|
1162
|
+
return this;
|
1163
|
+
};
|
1164
|
+
|
1165
|
+
// Keep the identity function around for default iteratees.
|
1166
|
+
_.identity = function(value) {
|
1167
|
+
return value;
|
1168
|
+
};
|
1169
|
+
|
1170
|
+
// Predicate-generating functions. Often useful outside of Underscore.
|
1171
|
+
_.constant = function(value) {
|
1172
|
+
return function() {
|
1173
|
+
return value;
|
1174
|
+
};
|
1175
|
+
};
|
1176
|
+
|
1177
|
+
_.noop = function(){};
|
1178
|
+
|
1179
|
+
_.property = function(key) {
|
1180
|
+
return function(obj) {
|
1181
|
+
return obj[key];
|
1182
|
+
};
|
1183
|
+
};
|
1184
|
+
|
1185
|
+
// Returns a predicate for checking whether an object has a given set of `key:value` pairs.
|
1186
|
+
_.matches = function(attrs) {
|
1187
|
+
var pairs = _.pairs(attrs), length = pairs.length;
|
1188
|
+
return function(obj) {
|
1189
|
+
if (obj == null) return !length;
|
1190
|
+
obj = new Object(obj);
|
1191
|
+
for (var i = 0; i < length; i++) {
|
1192
|
+
var pair = pairs[i], key = pair[0];
|
1193
|
+
if (pair[1] !== obj[key] || !(key in obj)) return false;
|
1194
|
+
}
|
1195
|
+
return true;
|
1196
|
+
};
|
1197
|
+
};
|
1198
|
+
|
1199
|
+
// Run a function **n** times.
|
1200
|
+
_.times = function(n, iteratee, context) {
|
1201
|
+
var accum = Array(Math.max(0, n));
|
1202
|
+
iteratee = createCallback(iteratee, context, 1);
|
1203
|
+
for (var i = 0; i < n; i++) accum[i] = iteratee(i);
|
1204
|
+
return accum;
|
1205
|
+
};
|
1206
|
+
|
1207
|
+
// Return a random integer between min and max (inclusive).
|
1208
|
+
_.random = function(min, max) {
|
1209
|
+
if (max == null) {
|
1210
|
+
max = min;
|
1211
|
+
min = 0;
|
1212
|
+
}
|
1213
|
+
return min + Math.floor(Math.random() * (max - min + 1));
|
1214
|
+
};
|
1215
|
+
|
1216
|
+
// A (possibly faster) way to get the current timestamp as an integer.
|
1217
|
+
_.now = Date.now || function() {
|
1218
|
+
return new Date().getTime();
|
1219
|
+
};
|
1220
|
+
|
1221
|
+
// List of HTML entities for escaping.
|
1222
|
+
var escapeMap = {
|
1223
|
+
'&': '&',
|
1224
|
+
'<': '<',
|
1225
|
+
'>': '>',
|
1226
|
+
'"': '"',
|
1227
|
+
"'": ''',
|
1228
|
+
'`': '`'
|
1229
|
+
};
|
1230
|
+
var unescapeMap = _.invert(escapeMap);
|
1231
|
+
|
1232
|
+
// Functions for escaping and unescaping strings to/from HTML interpolation.
|
1233
|
+
var createEscaper = function(map) {
|
1234
|
+
var escaper = function(match) {
|
1235
|
+
return map[match];
|
1236
|
+
};
|
1237
|
+
// Regexes for identifying a key that needs to be escaped
|
1238
|
+
var source = '(?:' + _.keys(map).join('|') + ')';
|
1239
|
+
var testRegexp = RegExp(source);
|
1240
|
+
var replaceRegexp = RegExp(source, 'g');
|
1241
|
+
return function(string) {
|
1242
|
+
string = string == null ? '' : '' + string;
|
1243
|
+
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
|
1244
|
+
};
|
1245
|
+
};
|
1246
|
+
_.escape = createEscaper(escapeMap);
|
1247
|
+
_.unescape = createEscaper(unescapeMap);
|
1248
|
+
|
1249
|
+
// If the value of the named `property` is a function then invoke it with the
|
1250
|
+
// `object` as context; otherwise, return it.
|
1251
|
+
_.result = function(object, property) {
|
1252
|
+
if (object == null) return void 0;
|
1253
|
+
var value = object[property];
|
1254
|
+
return _.isFunction(value) ? object[property]() : value;
|
1255
|
+
};
|
1256
|
+
|
1257
|
+
// Generate a unique integer id (unique within the entire client session).
|
1258
|
+
// Useful for temporary DOM ids.
|
1259
|
+
var idCounter = 0;
|
1260
|
+
_.uniqueId = function(prefix) {
|
1261
|
+
var id = ++idCounter + '';
|
1262
|
+
return prefix ? prefix + id : id;
|
1263
|
+
};
|
1264
|
+
|
1265
|
+
// By default, Underscore uses ERB-style template delimiters, change the
|
1266
|
+
// following template settings to use alternative delimiters.
|
1267
|
+
_.templateSettings = {
|
1268
|
+
evaluate : /<%([\s\S]+?)%>/g,
|
1269
|
+
interpolate : /<%=([\s\S]+?)%>/g,
|
1270
|
+
escape : /<%-([\s\S]+?)%>/g
|
1271
|
+
};
|
1272
|
+
|
1273
|
+
// When customizing `templateSettings`, if you don't want to define an
|
1274
|
+
// interpolation, evaluation or escaping regex, we need one that is
|
1275
|
+
// guaranteed not to match.
|
1276
|
+
var noMatch = /(.)^/;
|
1277
|
+
|
1278
|
+
// Certain characters need to be escaped so that they can be put into a
|
1279
|
+
// string literal.
|
1280
|
+
var escapes = {
|
1281
|
+
"'": "'",
|
1282
|
+
'\\': '\\',
|
1283
|
+
'\r': 'r',
|
1284
|
+
'\n': 'n',
|
1285
|
+
'\u2028': 'u2028',
|
1286
|
+
'\u2029': 'u2029'
|
1287
|
+
};
|
1288
|
+
|
1289
|
+
var escaper = /\\|'|\r|\n|\u2028|\u2029/g;
|
1290
|
+
|
1291
|
+
var escapeChar = function(match) {
|
1292
|
+
return '\\' + escapes[match];
|
1293
|
+
};
|
1294
|
+
|
1295
|
+
// JavaScript micro-templating, similar to John Resig's implementation.
|
1296
|
+
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
1297
|
+
// and correctly escapes quotes within interpolated code.
|
1298
|
+
// NB: `oldSettings` only exists for backwards compatibility.
|
1299
|
+
_.template = function(text, settings, oldSettings) {
|
1300
|
+
if (!settings && oldSettings) settings = oldSettings;
|
1301
|
+
settings = _.defaults({}, settings, _.templateSettings);
|
1302
|
+
|
1303
|
+
// Combine delimiters into one regular expression via alternation.
|
1304
|
+
var matcher = RegExp([
|
1305
|
+
(settings.escape || noMatch).source,
|
1306
|
+
(settings.interpolate || noMatch).source,
|
1307
|
+
(settings.evaluate || noMatch).source
|
1308
|
+
].join('|') + '|$', 'g');
|
1309
|
+
|
1310
|
+
// Compile the template source, escaping string literals appropriately.
|
1311
|
+
var index = 0;
|
1312
|
+
var source = "__p+='";
|
1313
|
+
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
|
1314
|
+
source += text.slice(index, offset).replace(escaper, escapeChar);
|
1315
|
+
index = offset + match.length;
|
1316
|
+
|
1317
|
+
if (escape) {
|
1318
|
+
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
|
1319
|
+
} else if (interpolate) {
|
1320
|
+
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
|
1321
|
+
} else if (evaluate) {
|
1322
|
+
source += "';\n" + evaluate + "\n__p+='";
|
1323
|
+
}
|
1324
|
+
|
1325
|
+
// Adobe VMs need the match returned to produce the correct offest.
|
1326
|
+
return match;
|
1327
|
+
});
|
1328
|
+
source += "';\n";
|
1329
|
+
|
1330
|
+
// If a variable is not specified, place data values in local scope.
|
1331
|
+
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
|
1332
|
+
|
1333
|
+
source = "var __t,__p='',__j=Array.prototype.join," +
|
1334
|
+
"print=function(){__p+=__j.call(arguments,'');};\n" +
|
1335
|
+
source + 'return __p;\n';
|
1336
|
+
|
1337
|
+
try {
|
1338
|
+
var render = new Function(settings.variable || 'obj', '_', source);
|
1339
|
+
} catch (e) {
|
1340
|
+
e.source = source;
|
1341
|
+
throw e;
|
1342
|
+
}
|
1343
|
+
|
1344
|
+
var template = function(data) {
|
1345
|
+
return render.call(this, data, _);
|
1346
|
+
};
|
1347
|
+
|
1348
|
+
// Provide the compiled source as a convenience for precompilation.
|
1349
|
+
var argument = settings.variable || 'obj';
|
1350
|
+
template.source = 'function(' + argument + '){\n' + source + '}';
|
1351
|
+
|
1352
|
+
return template;
|
1353
|
+
};
|
1354
|
+
|
1355
|
+
// Add a "chain" function. Start chaining a wrapped Underscore object.
|
1356
|
+
_.chain = function(obj) {
|
1357
|
+
var instance = _(obj);
|
1358
|
+
instance._chain = true;
|
1359
|
+
return instance;
|
1360
|
+
};
|
1361
|
+
|
1362
|
+
// OOP
|
1363
|
+
// ---------------
|
1364
|
+
// If Underscore is called as a function, it returns a wrapped object that
|
1365
|
+
// can be used OO-style. This wrapper holds altered versions of all the
|
1366
|
+
// underscore functions. Wrapped objects may be chained.
|
1367
|
+
|
1368
|
+
// Helper function to continue chaining intermediate results.
|
1369
|
+
var result = function(instance, obj) {
|
1370
|
+
return instance._chain ? _(obj).chain() : obj;
|
1371
|
+
};
|
1372
|
+
|
1373
|
+
// Add your own custom functions to the Underscore object.
|
1374
|
+
_.mixin = function(obj) {
|
1375
|
+
_.each(_.functions(obj), function(name) {
|
1376
|
+
var func = _[name] = obj[name];
|
1377
|
+
_.prototype[name] = function() {
|
1378
|
+
var args = [this._wrapped];
|
1379
|
+
push.apply(args, arguments);
|
1380
|
+
return result(this, func.apply(_, args));
|
1381
|
+
};
|
1382
|
+
});
|
1383
|
+
};
|
1384
|
+
|
1385
|
+
// Add all of the Underscore functions to the wrapper object.
|
1386
|
+
_.mixin(_);
|
1387
|
+
|
1388
|
+
// Add all mutator Array functions to the wrapper.
|
1389
|
+
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
1390
|
+
var method = ArrayProto[name];
|
1391
|
+
_.prototype[name] = function() {
|
1392
|
+
var obj = this._wrapped;
|
1393
|
+
method.apply(obj, arguments);
|
1394
|
+
if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
|
1395
|
+
return result(this, obj);
|
1396
|
+
};
|
1397
|
+
});
|
1398
|
+
|
1399
|
+
// Add all accessor Array functions to the wrapper.
|
1400
|
+
_.each(['concat', 'join', 'slice'], function(name) {
|
1401
|
+
var method = ArrayProto[name];
|
1402
|
+
_.prototype[name] = function() {
|
1403
|
+
return result(this, method.apply(this._wrapped, arguments));
|
1404
|
+
};
|
1405
|
+
});
|
1406
|
+
|
1407
|
+
// Extracts the result from a wrapped and chained object.
|
1408
|
+
_.prototype.value = function() {
|
1409
|
+
return this._wrapped;
|
1410
|
+
};
|
1411
|
+
|
1412
|
+
// AMD registration happens at the end for compatibility with AMD loaders
|
1413
|
+
// that may not enforce next-turn semantics on modules. Even though general
|
1414
|
+
// practice for AMD registration is to be anonymous, underscore registers
|
1415
|
+
// as a named module because, like jQuery, it is a base library that is
|
1416
|
+
// popular enough to be bundled in a third party lib, but not be part of
|
1417
|
+
// an AMD load request. Those cases could generate an error when an
|
1418
|
+
// anonymous define() is called outside of a loader request.
|
1419
|
+
if (typeof define === 'function' && define.amd) {
|
1420
|
+
define('underscore', [], function() {
|
1421
|
+
return _;
|
1422
|
+
});
|
1423
|
+
}
|
1424
|
+
}.call(this));
|