underscore-source 1.1.6 → 1.1.7
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
|
|
1
|
-
// Underscore.js 1.1.
|
1
|
+
// Underscore.js 1.1.7
|
2
2
|
// (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.
|
3
3
|
// Underscore is freely distributable under the MIT license.
|
4
4
|
// Portions of Underscore are inspired or borrowed from Prototype,
|
@@ -55,25 +55,26 @@
|
|
55
55
|
module.exports = _;
|
56
56
|
_._ = _;
|
57
57
|
} else {
|
58
|
-
|
58
|
+
// Exported as a string, for Closure Compiler "advanced" mode.
|
59
|
+
root['_'] = _;
|
59
60
|
}
|
60
61
|
|
61
62
|
// Current version.
|
62
|
-
_.VERSION = '1.1.
|
63
|
+
_.VERSION = '1.1.7';
|
63
64
|
|
64
65
|
// Collection Functions
|
65
66
|
// --------------------
|
66
67
|
|
67
68
|
// The cornerstone, an `each` implementation, aka `forEach`.
|
68
|
-
// Handles objects
|
69
|
+
// Handles objects with the built-in `forEach`, arrays, and raw objects.
|
69
70
|
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
70
71
|
var each = _.each = _.forEach = function(obj, iterator, context) {
|
71
72
|
if (obj == null) return;
|
72
73
|
if (nativeForEach && obj.forEach === nativeForEach) {
|
73
74
|
obj.forEach(iterator, context);
|
74
|
-
} else if (
|
75
|
+
} else if (obj.length === +obj.length) {
|
75
76
|
for (var i = 0, l = obj.length; i < l; i++) {
|
76
|
-
if (iterator.call(context, obj[i], i, obj) === breaker) return;
|
77
|
+
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
|
77
78
|
}
|
78
79
|
} else {
|
79
80
|
for (var key in obj) {
|
@@ -106,7 +107,7 @@
|
|
106
107
|
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
107
108
|
}
|
108
109
|
each(obj, function(value, index, list) {
|
109
|
-
if (!initial
|
110
|
+
if (!initial) {
|
110
111
|
memo = value;
|
111
112
|
initial = true;
|
112
113
|
} else {
|
@@ -181,14 +182,14 @@
|
|
181
182
|
// Delegates to **ECMAScript 5**'s native `some` if available.
|
182
183
|
// Aliased as `any`.
|
183
184
|
var any = _.some = _.any = function(obj, iterator, context) {
|
184
|
-
iterator
|
185
|
+
iterator = iterator || _.identity;
|
185
186
|
var result = false;
|
186
187
|
if (obj == null) return result;
|
187
188
|
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
188
189
|
each(obj, function(value, index, list) {
|
189
|
-
if (result
|
190
|
+
if (result |= iterator.call(context, value, index, list)) return breaker;
|
190
191
|
});
|
191
|
-
return result;
|
192
|
+
return !!result;
|
192
193
|
};
|
193
194
|
|
194
195
|
// Determine if a given value is included in the array or object using `===`.
|
@@ -251,6 +252,16 @@
|
|
251
252
|
}), 'value');
|
252
253
|
};
|
253
254
|
|
255
|
+
// Groups the object's values by a criterion produced by an iterator
|
256
|
+
_.groupBy = function(obj, iterator) {
|
257
|
+
var result = {};
|
258
|
+
each(obj, function(value, index) {
|
259
|
+
var key = iterator(value, index);
|
260
|
+
(result[key] || (result[key] = [])).push(value);
|
261
|
+
});
|
262
|
+
return result;
|
263
|
+
};
|
264
|
+
|
254
265
|
// Use a comparator function to figure out at what index an object should
|
255
266
|
// be inserted so as to maintain order. Uses binary search.
|
256
267
|
_.sortedIndex = function(array, obj, iterator) {
|
@@ -267,7 +278,7 @@
|
|
267
278
|
_.toArray = function(iterable) {
|
268
279
|
if (!iterable) return [];
|
269
280
|
if (iterable.toArray) return iterable.toArray();
|
270
|
-
if (_.isArray(iterable)) return iterable;
|
281
|
+
if (_.isArray(iterable)) return slice.call(iterable);
|
271
282
|
if (_.isArguments(iterable)) return slice.call(iterable);
|
272
283
|
return _.values(iterable);
|
273
284
|
};
|
@@ -316,8 +327,7 @@
|
|
316
327
|
|
317
328
|
// Return a version of the array that does not contain the specified value(s).
|
318
329
|
_.without = function(array) {
|
319
|
-
|
320
|
-
return _.filter(array, function(value){ return !_.include(values, value); });
|
330
|
+
return _.difference(array, slice.call(arguments, 1));
|
321
331
|
};
|
322
332
|
|
323
333
|
// Produce a duplicate-free version of the array. If the array has already
|
@@ -330,9 +340,15 @@
|
|
330
340
|
}, []);
|
331
341
|
};
|
332
342
|
|
343
|
+
// Produce an array that contains the union: each distinct element from all of
|
344
|
+
// the passed-in arrays.
|
345
|
+
_.union = function() {
|
346
|
+
return _.uniq(_.flatten(arguments));
|
347
|
+
};
|
348
|
+
|
333
349
|
// Produce an array that contains every item shared between all the
|
334
|
-
// passed-in arrays.
|
335
|
-
_.intersect = function(array) {
|
350
|
+
// passed-in arrays. (Aliased as "intersect" for back-compat.)
|
351
|
+
_.intersection = _.intersect = function(array) {
|
336
352
|
var rest = slice.call(arguments, 1);
|
337
353
|
return _.filter(_.uniq(array), function(item) {
|
338
354
|
return _.every(rest, function(other) {
|
@@ -341,6 +357,12 @@
|
|
341
357
|
});
|
342
358
|
};
|
343
359
|
|
360
|
+
// Take the difference between one array and another.
|
361
|
+
// Only the elements present in just the first array will remain.
|
362
|
+
_.difference = function(array, other) {
|
363
|
+
return _.filter(array, function(value){ return !_.include(other, value); });
|
364
|
+
};
|
365
|
+
|
344
366
|
// Zip together multiple lists into a single array -- elements that share
|
345
367
|
// an index go together.
|
346
368
|
_.zip = function() {
|
@@ -502,7 +524,7 @@
|
|
502
524
|
var funcs = slice.call(arguments);
|
503
525
|
return function() {
|
504
526
|
var args = slice.call(arguments);
|
505
|
-
for (var i=funcs.length-1; i >= 0; i--) {
|
527
|
+
for (var i = funcs.length - 1; i >= 0; i--) {
|
506
528
|
args = [funcs[i].apply(this, args)];
|
507
529
|
}
|
508
530
|
return args[0];
|
@@ -537,7 +559,11 @@
|
|
537
559
|
// Return a sorted list of the function names available on the object.
|
538
560
|
// Aliased as `methods`
|
539
561
|
_.functions = _.methods = function(obj) {
|
540
|
-
|
562
|
+
var names = [];
|
563
|
+
for (var key in obj) {
|
564
|
+
if (_.isFunction(obj[key])) names.push(key);
|
565
|
+
}
|
566
|
+
return names.sort();
|
541
567
|
};
|
542
568
|
|
543
569
|
// Extend a given object with all the properties in passed-in object(s).
|
@@ -589,6 +615,7 @@
|
|
589
615
|
if (b._chain) b = b._wrapped;
|
590
616
|
// One of them implements an isEqual()?
|
591
617
|
if (a.isEqual) return a.isEqual(b);
|
618
|
+
if (b.isEqual) return b.isEqual(a);
|
592
619
|
// Check dates' integer values.
|
593
620
|
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
594
621
|
// Both are NaN?
|
@@ -630,6 +657,11 @@
|
|
630
657
|
return toString.call(obj) === '[object Array]';
|
631
658
|
};
|
632
659
|
|
660
|
+
// Is a given variable an object?
|
661
|
+
_.isObject = function(obj) {
|
662
|
+
return obj === Object(obj);
|
663
|
+
};
|
664
|
+
|
633
665
|
// Is a given variable an arguments object?
|
634
666
|
_.isArguments = function(obj) {
|
635
667
|
return !!(obj && hasOwnProperty.call(obj, 'callee'));
|