underscore-source 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
|
|
1
|
-
// Underscore.js 1.1.
|
2
|
-
// (c)
|
1
|
+
// Underscore.js 1.1.4
|
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,
|
5
5
|
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
@@ -58,7 +58,7 @@
|
|
58
58
|
}
|
59
59
|
|
60
60
|
// Current version.
|
61
|
-
_.VERSION = '1.1.
|
61
|
+
_.VERSION = '1.1.4';
|
62
62
|
|
63
63
|
// Collection Functions
|
64
64
|
// --------------------
|
@@ -68,6 +68,7 @@
|
|
68
68
|
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
69
69
|
var each = _.each = _.forEach = function(obj, iterator, context) {
|
70
70
|
var value;
|
71
|
+
if (obj == null) return;
|
71
72
|
if (nativeForEach && obj.forEach === nativeForEach) {
|
72
73
|
obj.forEach(iterator, context);
|
73
74
|
} else if (_.isNumber(obj.length)) {
|
@@ -86,8 +87,9 @@
|
|
86
87
|
// Return the results of applying the iterator to each element.
|
87
88
|
// Delegates to **ECMAScript 5**'s native `map` if available.
|
88
89
|
_.map = function(obj, iterator, context) {
|
89
|
-
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
90
90
|
var results = [];
|
91
|
+
if (obj == null) return results;
|
92
|
+
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
91
93
|
each(obj, function(value, index, list) {
|
92
94
|
results[results.length] = iterator.call(context, value, index, list);
|
93
95
|
});
|
@@ -98,6 +100,7 @@
|
|
98
100
|
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
99
101
|
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
100
102
|
var initial = memo !== void 0;
|
103
|
+
if (obj == null) obj = [];
|
101
104
|
if (nativeReduce && obj.reduce === nativeReduce) {
|
102
105
|
if (context) iterator = _.bind(iterator, context);
|
103
106
|
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
@@ -105,16 +108,19 @@
|
|
105
108
|
each(obj, function(value, index, list) {
|
106
109
|
if (!initial && index === 0) {
|
107
110
|
memo = value;
|
111
|
+
initial = true;
|
108
112
|
} else {
|
109
113
|
memo = iterator.call(context, memo, value, index, list);
|
110
114
|
}
|
111
115
|
});
|
116
|
+
if (!initial) throw new TypeError("Reduce of empty array with no initial value");
|
112
117
|
return memo;
|
113
118
|
};
|
114
119
|
|
115
120
|
// The right-associative version of reduce, also known as `foldr`.
|
116
121
|
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
|
117
122
|
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
123
|
+
if (obj == null) obj = [];
|
118
124
|
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
119
125
|
if (context) iterator = _.bind(iterator, context);
|
120
126
|
return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
@@ -139,8 +145,9 @@
|
|
139
145
|
// Delegates to **ECMAScript 5**'s native `filter` if available.
|
140
146
|
// Aliased as `select`.
|
141
147
|
_.filter = _.select = function(obj, iterator, context) {
|
142
|
-
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
143
148
|
var results = [];
|
149
|
+
if (obj == null) return results;
|
150
|
+
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
144
151
|
each(obj, function(value, index, list) {
|
145
152
|
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
146
153
|
});
|
@@ -150,6 +157,7 @@
|
|
150
157
|
// Return all the elements for which a truth test fails.
|
151
158
|
_.reject = function(obj, iterator, context) {
|
152
159
|
var results = [];
|
160
|
+
if (obj == null) return results;
|
153
161
|
each(obj, function(value, index, list) {
|
154
162
|
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
155
163
|
});
|
@@ -161,8 +169,9 @@
|
|
161
169
|
// Aliased as `all`.
|
162
170
|
_.every = _.all = function(obj, iterator, context) {
|
163
171
|
iterator = iterator || _.identity;
|
164
|
-
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
165
172
|
var result = true;
|
173
|
+
if (obj == null) return result;
|
174
|
+
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
166
175
|
each(obj, function(value, index, list) {
|
167
176
|
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
|
168
177
|
});
|
@@ -174,8 +183,9 @@
|
|
174
183
|
// Aliased as `any`.
|
175
184
|
var any = _.some = _.any = function(obj, iterator, context) {
|
176
185
|
iterator = iterator || _.identity;
|
177
|
-
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
178
186
|
var result = false;
|
187
|
+
if (obj == null) return result;
|
188
|
+
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
179
189
|
each(obj, function(value, index, list) {
|
180
190
|
if (result = iterator.call(context, value, index, list)) return breaker;
|
181
191
|
});
|
@@ -185,8 +195,9 @@
|
|
185
195
|
// Determine if a given value is included in the array or object using `===`.
|
186
196
|
// Aliased as `contains`.
|
187
197
|
_.include = _.contains = function(obj, target) {
|
188
|
-
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
189
198
|
var found = false;
|
199
|
+
if (obj == null) return found;
|
200
|
+
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
190
201
|
any(obj, function(value) {
|
191
202
|
if (found = value === target) return true;
|
192
203
|
});
|
@@ -345,7 +356,14 @@
|
|
345
356
|
// we need this function. Return the position of the first occurrence of an
|
346
357
|
// item in an array, or -1 if the item is not included in the array.
|
347
358
|
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
|
348
|
-
|
359
|
+
// If the array is large and already in sort order, pass `true`
|
360
|
+
// for **isSorted** to use binary search.
|
361
|
+
_.indexOf = function(array, item, isSorted) {
|
362
|
+
if (array == null) return -1;
|
363
|
+
if (isSorted) {
|
364
|
+
var i = _.sortedIndex(array, item);
|
365
|
+
return array[i] === item ? i : -1;
|
366
|
+
}
|
349
367
|
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
350
368
|
for (var i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
|
351
369
|
return -1;
|
@@ -354,6 +372,7 @@
|
|
354
372
|
|
355
373
|
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
356
374
|
_.lastIndexOf = function(array, item) {
|
375
|
+
if (array == null) return -1;
|
357
376
|
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
|
358
377
|
var i = array.length;
|
359
378
|
while (i--) if (array[i] === item) return i;
|
@@ -456,7 +475,7 @@
|
|
456
475
|
_.wrap = function(func, wrapper) {
|
457
476
|
return function() {
|
458
477
|
var args = [func].concat(slice.call(arguments));
|
459
|
-
return wrapper.apply(
|
478
|
+
return wrapper.apply(this, args);
|
460
479
|
};
|
461
480
|
};
|
462
481
|
|
@@ -528,6 +547,9 @@
|
|
528
547
|
if (a == b) return true;
|
529
548
|
// One is falsy and the other truthy.
|
530
549
|
if ((!a && b) || (a && !b)) return false;
|
550
|
+
// Unwrap any wrapped objects.
|
551
|
+
if (a._chain) a = a._wrapped;
|
552
|
+
if (b._chain) b = b._wrapped;
|
531
553
|
// One of them implements an isEqual()?
|
532
554
|
if (a.isEqual) return a.isEqual(b);
|
533
555
|
// Check dates' integer values.
|
@@ -568,12 +590,12 @@
|
|
568
590
|
// Is a given value an array?
|
569
591
|
// Delegates to ECMA5's native Array.isArray
|
570
592
|
_.isArray = nativeIsArray || function(obj) {
|
571
|
-
return
|
593
|
+
return toString.call(obj) === '[object Array]';
|
572
594
|
};
|
573
595
|
|
574
596
|
// Is a given variable an arguments object?
|
575
597
|
_.isArguments = function(obj) {
|
576
|
-
return !!(obj && obj
|
598
|
+
return !!(obj && hasOwnProperty.call(obj, 'callee'));
|
577
599
|
};
|
578
600
|
|
579
601
|
// Is a given value a function?
|
@@ -591,10 +613,10 @@
|
|
591
613
|
return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
|
592
614
|
};
|
593
615
|
|
594
|
-
// Is the given value NaN
|
595
|
-
//
|
616
|
+
// Is the given value `NaN`? `NaN` happens to be the only value in JavaScript
|
617
|
+
// that does not equal itself.
|
596
618
|
_.isNaN = function(obj) {
|
597
|
-
return
|
619
|
+
return obj !== obj;
|
598
620
|
};
|
599
621
|
|
600
622
|
// Is a given value a boolean?
|