underscore-source 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
@@ -26,7 +26,7 @@
|
|
26
26
|
commonJS ? _ = exports : window._ = _;
|
27
27
|
|
28
28
|
// Current version.
|
29
|
-
_.VERSION = '0.3.
|
29
|
+
_.VERSION = '0.3.1';
|
30
30
|
|
31
31
|
/*------------------------ Collection Functions: ---------------------------*/
|
32
32
|
|
@@ -38,16 +38,12 @@
|
|
38
38
|
if (obj.forEach) {
|
39
39
|
obj.forEach(iterator, context);
|
40
40
|
} else if (obj.length) {
|
41
|
-
for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i);
|
41
|
+
for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
|
42
42
|
} else if (obj.each) {
|
43
|
-
obj.each(function(value) { iterator.call(context, value, index
|
43
|
+
obj.each(function(value) { iterator.call(context, value, index++, obj); });
|
44
44
|
} else {
|
45
|
-
var i = 0;
|
46
45
|
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
47
|
-
|
48
|
-
pair.key = key;
|
49
|
-
pair.value = value;
|
50
|
-
iterator.call(context, pair, i++);
|
46
|
+
iterator.call(context, obj[key], key, obj);
|
51
47
|
}
|
52
48
|
}
|
53
49
|
} catch(e) {
|
@@ -61,8 +57,8 @@
|
|
61
57
|
_.map = function(obj, iterator, context) {
|
62
58
|
if (obj && obj.map) return obj.map(iterator, context);
|
63
59
|
var results = [];
|
64
|
-
_.each(obj, function(value, index) {
|
65
|
-
results.push(iterator.call(context, value, index));
|
60
|
+
_.each(obj, function(value, index, list) {
|
61
|
+
results.push(iterator.call(context, value, index, list));
|
66
62
|
});
|
67
63
|
return results;
|
68
64
|
};
|
@@ -70,8 +66,8 @@
|
|
70
66
|
// Reduce builds up a single result from a list of values. Also known as
|
71
67
|
// inject, or foldl.
|
72
68
|
_.reduce = function(obj, memo, iterator, context) {
|
73
|
-
_.each(obj, function(value, index) {
|
74
|
-
memo = iterator.call(context, memo, value, index);
|
69
|
+
_.each(obj, function(value, index, list) {
|
70
|
+
memo = iterator.call(context, memo, value, index, list);
|
75
71
|
});
|
76
72
|
return memo;
|
77
73
|
};
|
@@ -79,8 +75,8 @@
|
|
79
75
|
// Return the first value which passes a truth test.
|
80
76
|
_.detect = function(obj, iterator, context) {
|
81
77
|
var result;
|
82
|
-
_.each(obj, function(value, index) {
|
83
|
-
if (iterator.call(context, value, index)) {
|
78
|
+
_.each(obj, function(value, index, list) {
|
79
|
+
if (iterator.call(context, value, index, list)) {
|
84
80
|
result = value;
|
85
81
|
throw '__break__';
|
86
82
|
}
|
@@ -93,8 +89,8 @@
|
|
93
89
|
_.select = function(obj, iterator, context) {
|
94
90
|
if (obj.filter) return obj.filter(iterator, context);
|
95
91
|
var results = [];
|
96
|
-
_.each(obj, function(value, index) {
|
97
|
-
iterator.call(context, value, index) && results.push(value);
|
92
|
+
_.each(obj, function(value, index, list) {
|
93
|
+
iterator.call(context, value, index, list) && results.push(value);
|
98
94
|
});
|
99
95
|
return results;
|
100
96
|
};
|
@@ -102,8 +98,8 @@
|
|
102
98
|
// Return all the elements for which a truth test fails.
|
103
99
|
_.reject = function(obj, iterator, context) {
|
104
100
|
var results = [];
|
105
|
-
_.each(obj, function(value, index) {
|
106
|
-
!iterator.call(context, value, index) && results.push(value);
|
101
|
+
_.each(obj, function(value, index, list) {
|
102
|
+
!iterator.call(context, value, index, list) && results.push(value);
|
107
103
|
});
|
108
104
|
return results;
|
109
105
|
};
|
@@ -114,8 +110,8 @@
|
|
114
110
|
iterator = iterator || identity;
|
115
111
|
if (obj.every) return obj.every(iterator, context);
|
116
112
|
var result = true;
|
117
|
-
_.each(obj, function(value, index) {
|
118
|
-
if (!(result = result && iterator.call(context, value, index))) throw '__break__';
|
113
|
+
_.each(obj, function(value, index, list) {
|
114
|
+
if (!(result = result && iterator.call(context, value, index, list))) throw '__break__';
|
119
115
|
});
|
120
116
|
return result;
|
121
117
|
};
|
@@ -126,8 +122,8 @@
|
|
126
122
|
iterator = iterator || identity;
|
127
123
|
if (obj.some) return obj.some(iterator, context);
|
128
124
|
var result = false;
|
129
|
-
_.each(obj, function(value, index) {
|
130
|
-
if (result = iterator.call(context, value, index)) throw '__break__';
|
125
|
+
_.each(obj, function(value, index, list) {
|
126
|
+
if (result = iterator.call(context, value, index, list)) throw '__break__';
|
131
127
|
});
|
132
128
|
return result;
|
133
129
|
};
|
@@ -137,8 +133,8 @@
|
|
137
133
|
_.include = function(obj, target) {
|
138
134
|
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
139
135
|
var found = false;
|
140
|
-
_.each(obj, function(
|
141
|
-
if (found =
|
136
|
+
_.each(obj, function(value) {
|
137
|
+
if (found = value === target) {
|
142
138
|
throw '__break__';
|
143
139
|
}
|
144
140
|
});
|
@@ -153,19 +149,17 @@
|
|
153
149
|
});
|
154
150
|
};
|
155
151
|
|
156
|
-
//
|
152
|
+
// Convenience version of a common use case of map: fetching a property.
|
157
153
|
_.pluck = function(obj, key) {
|
158
|
-
|
159
|
-
_.each(obj, function(value){ results.push(value[key]); });
|
160
|
-
return results;
|
154
|
+
return _.map(obj, function(value){ return value[key]; });
|
161
155
|
};
|
162
156
|
|
163
157
|
// Return the maximum item or (item-based computation).
|
164
158
|
_.max = function(obj, iterator, context) {
|
165
159
|
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
166
160
|
var result = {computed : -Infinity};
|
167
|
-
_.each(obj, function(value, index) {
|
168
|
-
var computed = iterator ? iterator.call(context, value, index) : value;
|
161
|
+
_.each(obj, function(value, index, list) {
|
162
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
169
163
|
computed >= result.computed && (result = {value : value, computed : computed});
|
170
164
|
});
|
171
165
|
return result.value;
|
@@ -175,8 +169,8 @@
|
|
175
169
|
_.min = function(obj, iterator, context) {
|
176
170
|
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
177
171
|
var result = {computed : Infinity};
|
178
|
-
_.each(obj, function(value, index) {
|
179
|
-
var computed = iterator ? iterator.call(context, value, index) : value;
|
172
|
+
_.each(obj, function(value, index, list) {
|
173
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
180
174
|
computed < result.computed && (result = {value : value, computed : computed});
|
181
175
|
});
|
182
176
|
return result.value;
|
@@ -184,10 +178,10 @@
|
|
184
178
|
|
185
179
|
// Sort the object's values by a criteria produced by an iterator.
|
186
180
|
_.sortBy = function(obj, iterator, context) {
|
187
|
-
return _.pluck(_.map(obj, function(value, index) {
|
181
|
+
return _.pluck(_.map(obj, function(value, index, list) {
|
188
182
|
return {
|
189
183
|
value : value,
|
190
|
-
criteria : iterator.call(context, value, index)
|
184
|
+
criteria : iterator.call(context, value, index, list)
|
191
185
|
};
|
192
186
|
}).sort(function(left, right) {
|
193
187
|
var a = left.criteria, b = right.criteria;
|
@@ -361,12 +355,12 @@
|
|
361
355
|
|
362
356
|
// Retrieve the names of an object's properties.
|
363
357
|
_.keys = function(obj) {
|
364
|
-
return _.
|
358
|
+
return _.map(obj, function(value, key){ return key; });
|
365
359
|
};
|
366
360
|
|
367
361
|
// Retrieve the values of an object's properties.
|
368
362
|
_.values = function(obj) {
|
369
|
-
return _.
|
363
|
+
return _.map(obj, identity);
|
370
364
|
};
|
371
365
|
|
372
366
|
// Extend a given object with all of the properties in a source object.
|