underscore-source 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -55,7 +55,7 @@
|
|
55
55
|
root._ = _;
|
56
56
|
|
57
57
|
// Current version.
|
58
|
-
_.VERSION = '1.0
|
58
|
+
_.VERSION = '1.1.0';
|
59
59
|
|
60
60
|
// ------------------------ Collection Functions: ---------------------------
|
61
61
|
|
@@ -92,8 +92,11 @@
|
|
92
92
|
|
93
93
|
// Reduce builds up a single result from a list of values, aka inject, or foldl.
|
94
94
|
// Delegates to JavaScript 1.8's native reduce if available.
|
95
|
-
_.reduce = function(obj,
|
96
|
-
if (nativeReduce && obj.reduce === nativeReduce)
|
95
|
+
_.reduce = function(obj, iterator, memo, context) {
|
96
|
+
if (nativeReduce && obj.reduce === nativeReduce) {
|
97
|
+
if (context) iterator = _.bind(iterator, context);
|
98
|
+
return obj.reduce(iterator, memo);
|
99
|
+
}
|
97
100
|
each(obj, function(value, index, list) {
|
98
101
|
memo = iterator.call(context, memo, value, index, list);
|
99
102
|
});
|
@@ -102,10 +105,13 @@
|
|
102
105
|
|
103
106
|
// The right-associative version of reduce, also known as foldr. Uses
|
104
107
|
// Delegates to JavaScript 1.8's native reduceRight if available.
|
105
|
-
_.reduceRight = function(obj,
|
106
|
-
if (nativeReduceRight && obj.reduceRight === nativeReduceRight)
|
108
|
+
_.reduceRight = function(obj, iterator, memo, context) {
|
109
|
+
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
110
|
+
if (context) iterator = _.bind(iterator, context);
|
111
|
+
return obj.reduceRight(iterator, memo);
|
112
|
+
}
|
107
113
|
var reversed = _.clone(_.toArray(obj)).reverse();
|
108
|
-
return _.reduce(reversed,
|
114
|
+
return _.reduce(reversed, iterator, memo, context);
|
109
115
|
};
|
110
116
|
|
111
117
|
// Return the first value which passes a truth test.
|
@@ -277,11 +283,11 @@
|
|
277
283
|
|
278
284
|
// Return a completely flattened version of an array.
|
279
285
|
_.flatten = function(array) {
|
280
|
-
return _.reduce(array,
|
286
|
+
return _.reduce(array, function(memo, value) {
|
281
287
|
if (_.isArray(value)) return memo.concat(_.flatten(value));
|
282
288
|
memo.push(value);
|
283
289
|
return memo;
|
284
|
-
});
|
290
|
+
}, []);
|
285
291
|
};
|
286
292
|
|
287
293
|
// Return a version of the array that does not contain the specified value(s).
|
@@ -293,10 +299,10 @@
|
|
293
299
|
// Produce a duplicate-free version of the array. If the array has already
|
294
300
|
// been sorted, you have the option of using a faster algorithm.
|
295
301
|
_.uniq = function(array, isSorted) {
|
296
|
-
return _.reduce(array,
|
302
|
+
return _.reduce(array, function(memo, el, i) {
|
297
303
|
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo.push(el);
|
298
304
|
return memo;
|
299
|
-
});
|
305
|
+
}, []);
|
300
306
|
};
|
301
307
|
|
302
308
|
// Produce an array that contains every item shared between all the
|
@@ -619,17 +625,19 @@
|
|
619
625
|
// JavaScript templating a-la ERB, pilfered from John Resig's
|
620
626
|
// "Secrets of the JavaScript Ninja", page 83.
|
621
627
|
// Single-quote fix from Rick Strahl's version.
|
622
|
-
// With alterations for arbitrary delimiters.
|
628
|
+
// With alterations for arbitrary delimiters, and to preserve whitespace.
|
623
629
|
_.template = function(str, data) {
|
624
630
|
var c = _.templateSettings;
|
625
631
|
var endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g");
|
626
632
|
var fn = new Function('obj',
|
627
633
|
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
628
|
-
'with(obj){p.push(\'' +
|
629
|
-
str.replace(
|
630
|
-
.replace(
|
634
|
+
'with(obj||{}){p.push(\'' +
|
635
|
+
str.replace(/\r/g, '\\r')
|
636
|
+
.replace(/\n/g, '\\n')
|
637
|
+
.replace(/\t/g, '\\t')
|
638
|
+
.replace(endMatch,"✄")
|
631
639
|
.split("'").join("\\'")
|
632
|
-
.split("
|
640
|
+
.split("✄").join("'")
|
633
641
|
.replace(c.interpolate, "',$1,'")
|
634
642
|
.split(c.start).join("');")
|
635
643
|
.split(c.end).join("p.push('")
|
@@ -645,6 +653,7 @@
|
|
645
653
|
_.select = _.filter;
|
646
654
|
_.all = _.every;
|
647
655
|
_.any = _.some;
|
656
|
+
_.contains = _.include;
|
648
657
|
_.head = _.first;
|
649
658
|
_.tail = _.rest;
|
650
659
|
_.methods = _.functions;
|