underscore-source 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
|
|
1
|
-
// Underscore.js 1.1.
|
1
|
+
// Underscore.js 1.1.5
|
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,
|
@@ -21,7 +21,7 @@
|
|
21
21
|
var breaker = {};
|
22
22
|
|
23
23
|
// Save bytes in the minified (but not gzipped) version:
|
24
|
-
var ArrayProto = Array.prototype, ObjProto = Object.prototype;
|
24
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
|
25
25
|
|
26
26
|
// Create quick reference variables for speed access to core prototypes.
|
27
27
|
var slice = ArrayProto.slice,
|
@@ -42,7 +42,8 @@
|
|
42
42
|
nativeIndexOf = ArrayProto.indexOf,
|
43
43
|
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
44
44
|
nativeIsArray = Array.isArray,
|
45
|
-
nativeKeys = Object.keys
|
45
|
+
nativeKeys = Object.keys,
|
46
|
+
nativeBind = FuncProto.bind;
|
46
47
|
|
47
48
|
// Create a safe reference to the Underscore object for use below.
|
48
49
|
var _ = function(obj) { return new wrapper(obj); };
|
@@ -58,7 +59,7 @@
|
|
58
59
|
}
|
59
60
|
|
60
61
|
// Current version.
|
61
|
-
_.VERSION = '1.1.
|
62
|
+
_.VERSION = '1.1.5';
|
62
63
|
|
63
64
|
// Collection Functions
|
64
65
|
// --------------------
|
@@ -67,7 +68,6 @@
|
|
67
68
|
// Handles objects implementing `forEach`, arrays, and raw objects.
|
68
69
|
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
69
70
|
var each = _.each = _.forEach = function(obj, iterator, context) {
|
70
|
-
var value;
|
71
71
|
if (obj == null) return;
|
72
72
|
if (nativeForEach && obj.forEach === nativeForEach) {
|
73
73
|
obj.forEach(iterator, context);
|
@@ -285,7 +285,7 @@
|
|
285
285
|
// values in the array. Aliased as `head`. The **guard** check allows it to work
|
286
286
|
// with `_.map`.
|
287
287
|
_.first = _.head = function(array, n, guard) {
|
288
|
-
return n && !guard ? slice.call(array, 0, n) : array[0];
|
288
|
+
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
|
289
289
|
};
|
290
290
|
|
291
291
|
// Returns everything but the first entry of the array. Aliased as `tail`.
|
@@ -293,7 +293,7 @@
|
|
293
293
|
// the rest of the values in the array from that index onward. The **guard**
|
294
294
|
// check allows it to work with `_.map`.
|
295
295
|
_.rest = _.tail = function(array, index, guard) {
|
296
|
-
return slice.call(array,
|
296
|
+
return slice.call(array, (index == null) || guard ? 1 : index);
|
297
297
|
};
|
298
298
|
|
299
299
|
// Get the last element of an array.
|
@@ -360,12 +360,13 @@
|
|
360
360
|
// for **isSorted** to use binary search.
|
361
361
|
_.indexOf = function(array, item, isSorted) {
|
362
362
|
if (array == null) return -1;
|
363
|
+
var i, l;
|
363
364
|
if (isSorted) {
|
364
|
-
|
365
|
+
i = _.sortedIndex(array, item);
|
365
366
|
return array[i] === item ? i : -1;
|
366
367
|
}
|
367
368
|
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
368
|
-
for (
|
369
|
+
for (i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
|
369
370
|
return -1;
|
370
371
|
};
|
371
372
|
|
@@ -383,18 +384,21 @@
|
|
383
384
|
// the native Python `range()` function. See
|
384
385
|
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
385
386
|
_.range = function(start, stop, step) {
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
387
|
+
if (arguments.length <= 1) {
|
388
|
+
stop = start || 0;
|
389
|
+
start = 0;
|
390
|
+
}
|
391
|
+
step = arguments[2] || 1;
|
392
|
+
|
393
|
+
var len = Math.max(Math.ceil((stop - start) / step), 0);
|
394
|
+
var idx = 0;
|
395
|
+
var range = new Array(len);
|
396
|
+
|
397
|
+
while(idx < len) {
|
395
398
|
range[idx++] = start;
|
396
399
|
start += step;
|
397
400
|
}
|
401
|
+
|
398
402
|
return range;
|
399
403
|
};
|
400
404
|
|
@@ -403,10 +407,12 @@
|
|
403
407
|
|
404
408
|
// Create a function bound to a given object (assigning `this`, and arguments,
|
405
409
|
// optionally). Binding with arguments is also known as `curry`.
|
410
|
+
// Delegates to **ECMAScript 5**'s native `Function.bind` if available.
|
406
411
|
_.bind = function(func, obj) {
|
412
|
+
if (nativeBind && func.bind === nativeBind) return func.bind.apply(func, slice.call(arguments, 1));
|
407
413
|
var args = slice.call(arguments, 2);
|
408
414
|
return function() {
|
409
|
-
return func.apply(obj
|
415
|
+
return func.apply(obj, args.concat(slice.call(arguments)));
|
410
416
|
};
|
411
417
|
};
|
412
418
|
|
@@ -425,7 +431,7 @@
|
|
425
431
|
hasher = hasher || _.identity;
|
426
432
|
return function() {
|
427
433
|
var key = hasher.apply(this, arguments);
|
428
|
-
return key
|
434
|
+
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
|
429
435
|
};
|
430
436
|
};
|
431
437
|
|
@@ -469,6 +475,17 @@
|
|
469
475
|
return limit(func, wait, true);
|
470
476
|
};
|
471
477
|
|
478
|
+
// Returns a function that will be executed at most one time, no matter how
|
479
|
+
// often you call it. Useful for lazy initialization.
|
480
|
+
_.once = function(func) {
|
481
|
+
var ran = false, memo;
|
482
|
+
return function() {
|
483
|
+
if (ran) return memo;
|
484
|
+
ran = true;
|
485
|
+
return memo = func.apply(this, arguments);
|
486
|
+
};
|
487
|
+
};
|
488
|
+
|
472
489
|
// Returns the first function passed as an argument to the second,
|
473
490
|
// allowing you to adjust arguments, run code before and after, and
|
474
491
|
// conditionally execute the original function.
|
@@ -498,7 +515,7 @@
|
|
498
515
|
// Retrieve the names of an object's properties.
|
499
516
|
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
500
517
|
_.keys = nativeKeys || function(obj) {
|
501
|
-
if (
|
518
|
+
if (obj !== Object(obj)) throw new TypeError('Invalid object');
|
502
519
|
var keys = [];
|
503
520
|
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
|
504
521
|
return keys;
|
@@ -523,6 +540,14 @@
|
|
523
540
|
return obj;
|
524
541
|
};
|
525
542
|
|
543
|
+
// Fill in a given object with default properties.
|
544
|
+
_.defaults = function(obj) {
|
545
|
+
each(slice.call(arguments, 1), function(source) {
|
546
|
+
for (var prop in source) if (obj[prop] == null) obj[prop] = source[prop];
|
547
|
+
});
|
548
|
+
return obj;
|
549
|
+
};
|
550
|
+
|
526
551
|
// Create a (shallow-cloned) duplicate of an object.
|
527
552
|
_.clone = function(obj) {
|
528
553
|
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
|