underscore-source 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -23,7 +23,7 @@
|
|
23
23
|
if (typeof exports !== 'undefined') _ = exports;
|
24
24
|
|
25
25
|
// Current version.
|
26
|
-
_.VERSION = '0.3.
|
26
|
+
_.VERSION = '0.3.3';
|
27
27
|
|
28
28
|
/*------------------------ Collection Functions: ---------------------------*/
|
29
29
|
|
@@ -61,14 +61,26 @@
|
|
61
61
|
};
|
62
62
|
|
63
63
|
// Reduce builds up a single result from a list of values. Also known as
|
64
|
-
// inject, or foldl.
|
64
|
+
// inject, or foldl. Uses JavaScript 1.8's version of reduce, if possible.
|
65
65
|
_.reduce = function(obj, memo, iterator, context) {
|
66
|
+
if (obj && obj.reduce) return obj.reduce(_.bind(iterator, context), memo);
|
66
67
|
_.each(obj, function(value, index, list) {
|
67
68
|
memo = iterator.call(context, memo, value, index, list);
|
68
69
|
});
|
69
70
|
return memo;
|
70
71
|
};
|
71
72
|
|
73
|
+
// The right-associative version of reduce, also known as foldr. Uses
|
74
|
+
// JavaScript 1.8's version of reduceRight, if available.
|
75
|
+
_.reduceRight = function(obj, memo, iterator, context) {
|
76
|
+
if (obj && obj.reduceRight) return obj.reduceRight(_.bind(iterator, context), memo);
|
77
|
+
var reversed = _.clone(_.toArray(obj)).reverse();
|
78
|
+
_.each(reversed, function(value, index) {
|
79
|
+
memo = iterator.call(context, memo, value, index, obj);
|
80
|
+
});
|
81
|
+
return memo;
|
82
|
+
};
|
83
|
+
|
72
84
|
// Return the first value which passes a truth test.
|
73
85
|
_.detect = function(obj, iterator, context) {
|
74
86
|
var result;
|
@@ -368,6 +380,7 @@
|
|
368
380
|
|
369
381
|
// Create a (shallow-cloned) duplicate of an object.
|
370
382
|
_.clone = function(obj) {
|
383
|
+
if (_.isArray(obj)) return obj.slice(0);
|
371
384
|
return _.extend({}, obj);
|
372
385
|
};
|
373
386
|
|
@@ -455,7 +468,8 @@
|
|
455
468
|
/*------------------------------- Aliases ----------------------------------*/
|
456
469
|
|
457
470
|
_.forEach = _.each;
|
458
|
-
_.inject
|
471
|
+
_.foldl = _.inject = _.reduce;
|
472
|
+
_.foldr = _.reduceRight;
|
459
473
|
_.filter = _.select;
|
460
474
|
_.every = _.all;
|
461
475
|
_.some = _.any;
|