pubba 0.8.0 → 0.8.1
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,194 +1,300 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
1
|
+
// Underscore.js 1.2.4
|
|
2
|
+
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
|
3
|
+
// Underscore is freely distributable under the MIT license.
|
|
4
|
+
// Portions of Underscore are inspired or borrowed from Prototype,
|
|
5
|
+
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
|
6
|
+
// For all details and documentation:
|
|
7
|
+
// http://documentcloud.github.com/underscore
|
|
8
8
|
|
|
9
9
|
(function() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
|
|
11
|
+
// Baseline setup
|
|
12
|
+
// --------------
|
|
13
|
+
|
|
14
|
+
// Establish the root object, `window` in the browser, or `global` on the server.
|
|
15
|
+
var root = this;
|
|
16
|
+
|
|
17
|
+
// Save the previous value of the `_` variable.
|
|
13
18
|
var previousUnderscore = root._;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
19
|
+
|
|
20
|
+
// Establish the object that gets returned to break out of a loop iteration.
|
|
21
|
+
var breaker = {};
|
|
22
|
+
|
|
23
|
+
// Save bytes in the minified (but not gzipped) version:
|
|
24
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
|
|
25
|
+
|
|
26
|
+
// Create quick reference variables for speed access to core prototypes.
|
|
27
|
+
var slice = ArrayProto.slice,
|
|
28
|
+
unshift = ArrayProto.unshift,
|
|
29
|
+
toString = ObjProto.toString,
|
|
30
|
+
hasOwnProperty = ObjProto.hasOwnProperty;
|
|
31
|
+
|
|
32
|
+
// All **ECMAScript 5** native function implementations that we hope to use
|
|
33
|
+
// are declared here.
|
|
34
|
+
var
|
|
35
|
+
nativeForEach = ArrayProto.forEach,
|
|
36
|
+
nativeMap = ArrayProto.map,
|
|
37
|
+
nativeReduce = ArrayProto.reduce,
|
|
38
|
+
nativeReduceRight = ArrayProto.reduceRight,
|
|
39
|
+
nativeFilter = ArrayProto.filter,
|
|
40
|
+
nativeEvery = ArrayProto.every,
|
|
41
|
+
nativeSome = ArrayProto.some,
|
|
42
|
+
nativeIndexOf = ArrayProto.indexOf,
|
|
43
|
+
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
|
44
|
+
nativeIsArray = Array.isArray,
|
|
45
|
+
nativeKeys = Object.keys,
|
|
46
|
+
nativeBind = FuncProto.bind;
|
|
47
|
+
|
|
48
|
+
// Create a safe reference to the Underscore object for use below.
|
|
49
|
+
var _ = function(obj) { return new wrapper(obj); };
|
|
50
|
+
|
|
51
|
+
// Export the Underscore object for **Node.js** and **"CommonJS"**, with
|
|
52
|
+
// backwards-compatibility for the old `require()` API. If we're not in
|
|
53
|
+
// CommonJS, add `_` to the global object.
|
|
54
|
+
if (typeof exports !== 'undefined') {
|
|
55
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
56
|
+
exports = module.exports = _;
|
|
57
|
+
}
|
|
58
|
+
exports._ = _;
|
|
59
|
+
} else if (typeof define === 'function' && define.amd) {
|
|
60
|
+
// Register as a named module with AMD.
|
|
61
|
+
define('underscore', function() {
|
|
62
|
+
return _;
|
|
63
|
+
});
|
|
64
|
+
} else {
|
|
65
|
+
// Exported as a string, for Closure Compiler "advanced" mode.
|
|
66
|
+
root['_'] = _;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Current version.
|
|
70
|
+
_.VERSION = '1.2.4';
|
|
71
|
+
|
|
72
|
+
// Collection Functions
|
|
73
|
+
// --------------------
|
|
74
|
+
|
|
75
|
+
// The cornerstone, an `each` implementation, aka `forEach`.
|
|
76
|
+
// Handles objects with the built-in `forEach`, arrays, and raw objects.
|
|
77
|
+
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
|
78
|
+
var each = _.each = _.forEach = function(obj, iterator, context) {
|
|
79
|
+
if (obj == null) return;
|
|
80
|
+
if (nativeForEach && obj.forEach === nativeForEach) {
|
|
81
|
+
obj.forEach(iterator, context);
|
|
82
|
+
} else if (obj.length === +obj.length) {
|
|
83
|
+
for (var i = 0, l = obj.length; i < l; i++) {
|
|
84
|
+
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
for (var key in obj) {
|
|
88
|
+
if (hasOwnProperty.call(obj, key)) {
|
|
89
|
+
if (iterator.call(context, obj[key], key, obj) === breaker) return;
|
|
39
90
|
}
|
|
40
91
|
}
|
|
41
|
-
} catch(e) {
|
|
42
|
-
if (e != '__break__') throw e;
|
|
43
92
|
}
|
|
44
|
-
return obj;
|
|
45
93
|
};
|
|
46
|
-
|
|
47
|
-
// Return the results of applying the iterator to each element.
|
|
48
|
-
//
|
|
94
|
+
|
|
95
|
+
// Return the results of applying the iterator to each element.
|
|
96
|
+
// Delegates to **ECMAScript 5**'s native `map` if available.
|
|
49
97
|
_.map = function(obj, iterator, context) {
|
|
50
|
-
if (obj && obj.map) return obj.map(iterator, context);
|
|
51
98
|
var results = [];
|
|
52
|
-
|
|
53
|
-
|
|
99
|
+
if (obj == null) return results;
|
|
100
|
+
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
|
101
|
+
each(obj, function(value, index, list) {
|
|
102
|
+
results[results.length] = iterator.call(context, value, index, list);
|
|
54
103
|
});
|
|
104
|
+
if (obj.length === +obj.length) results.length = obj.length;
|
|
55
105
|
return results;
|
|
56
106
|
};
|
|
57
|
-
|
|
58
|
-
// Reduce builds up a single result from a list of values
|
|
59
|
-
//
|
|
60
|
-
_.reduce = function(obj,
|
|
61
|
-
|
|
62
|
-
|
|
107
|
+
|
|
108
|
+
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
|
109
|
+
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
|
110
|
+
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
|
111
|
+
var initial = arguments.length > 2;
|
|
112
|
+
if (obj == null) obj = [];
|
|
113
|
+
if (nativeReduce && obj.reduce === nativeReduce) {
|
|
114
|
+
if (context) iterator = _.bind(iterator, context);
|
|
115
|
+
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
|
116
|
+
}
|
|
117
|
+
each(obj, function(value, index, list) {
|
|
118
|
+
if (!initial) {
|
|
119
|
+
memo = value;
|
|
120
|
+
initial = true;
|
|
121
|
+
} else {
|
|
122
|
+
memo = iterator.call(context, memo, value, index, list);
|
|
123
|
+
}
|
|
63
124
|
});
|
|
125
|
+
if (!initial) throw new TypeError('Reduce of empty array with no initial value');
|
|
64
126
|
return memo;
|
|
65
127
|
};
|
|
66
|
-
|
|
67
|
-
//
|
|
68
|
-
|
|
128
|
+
|
|
129
|
+
// The right-associative version of reduce, also known as `foldr`.
|
|
130
|
+
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
|
|
131
|
+
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
|
132
|
+
var initial = arguments.length > 2;
|
|
133
|
+
if (obj == null) obj = [];
|
|
134
|
+
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
|
135
|
+
if (context) iterator = _.bind(iterator, context);
|
|
136
|
+
return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
|
137
|
+
}
|
|
138
|
+
var reversed = _.toArray(obj).reverse();
|
|
139
|
+
if (context && !initial) iterator = _.bind(iterator, context);
|
|
140
|
+
return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Return the first value which passes a truth test. Aliased as `detect`.
|
|
144
|
+
_.find = _.detect = function(obj, iterator, context) {
|
|
69
145
|
var result;
|
|
70
|
-
|
|
71
|
-
if (iterator.call(context, value, index)) {
|
|
146
|
+
any(obj, function(value, index, list) {
|
|
147
|
+
if (iterator.call(context, value, index, list)) {
|
|
72
148
|
result = value;
|
|
73
|
-
|
|
149
|
+
return true;
|
|
74
150
|
}
|
|
75
151
|
});
|
|
76
152
|
return result;
|
|
77
153
|
};
|
|
78
|
-
|
|
79
|
-
// Return all the elements that pass a truth test.
|
|
80
|
-
// filter
|
|
81
|
-
|
|
82
|
-
|
|
154
|
+
|
|
155
|
+
// Return all the elements that pass a truth test.
|
|
156
|
+
// Delegates to **ECMAScript 5**'s native `filter` if available.
|
|
157
|
+
// Aliased as `select`.
|
|
158
|
+
_.filter = _.select = function(obj, iterator, context) {
|
|
83
159
|
var results = [];
|
|
84
|
-
|
|
85
|
-
|
|
160
|
+
if (obj == null) return results;
|
|
161
|
+
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
|
162
|
+
each(obj, function(value, index, list) {
|
|
163
|
+
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
|
86
164
|
});
|
|
87
165
|
return results;
|
|
88
166
|
};
|
|
89
|
-
|
|
167
|
+
|
|
90
168
|
// Return all the elements for which a truth test fails.
|
|
91
169
|
_.reject = function(obj, iterator, context) {
|
|
92
170
|
var results = [];
|
|
93
|
-
|
|
94
|
-
|
|
171
|
+
if (obj == null) return results;
|
|
172
|
+
each(obj, function(value, index, list) {
|
|
173
|
+
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
|
95
174
|
});
|
|
96
175
|
return results;
|
|
97
176
|
};
|
|
98
|
-
|
|
99
|
-
// Determine whether all of the elements match a truth test.
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (obj.every) return obj.every(iterator, context);
|
|
177
|
+
|
|
178
|
+
// Determine whether all of the elements match a truth test.
|
|
179
|
+
// Delegates to **ECMAScript 5**'s native `every` if available.
|
|
180
|
+
// Aliased as `all`.
|
|
181
|
+
_.every = _.all = function(obj, iterator, context) {
|
|
104
182
|
var result = true;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
183
|
+
if (obj == null) return result;
|
|
184
|
+
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
|
185
|
+
each(obj, function(value, index, list) {
|
|
186
|
+
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
|
|
108
187
|
});
|
|
109
188
|
return result;
|
|
110
189
|
};
|
|
111
|
-
|
|
112
|
-
// Determine if at least one element in the object matches a truth test.
|
|
113
|
-
//
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
190
|
+
|
|
191
|
+
// Determine if at least one element in the object matches a truth test.
|
|
192
|
+
// Delegates to **ECMAScript 5**'s native `some` if available.
|
|
193
|
+
// Aliased as `any`.
|
|
194
|
+
var any = _.some = _.any = function(obj, iterator, context) {
|
|
195
|
+
iterator || (iterator = _.identity);
|
|
117
196
|
var result = false;
|
|
118
|
-
|
|
119
|
-
|
|
197
|
+
if (obj == null) return result;
|
|
198
|
+
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
|
199
|
+
each(obj, function(value, index, list) {
|
|
200
|
+
if (result || (result = iterator.call(context, value, index, list))) return breaker;
|
|
120
201
|
});
|
|
121
|
-
return result;
|
|
202
|
+
return !!result;
|
|
122
203
|
};
|
|
123
|
-
|
|
124
|
-
// Determine if a given value is included in the array or object
|
|
125
|
-
//
|
|
126
|
-
_.include = function(obj, target) {
|
|
127
|
-
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
|
204
|
+
|
|
205
|
+
// Determine if a given value is included in the array or object using `===`.
|
|
206
|
+
// Aliased as `contains`.
|
|
207
|
+
_.include = _.contains = function(obj, target) {
|
|
128
208
|
var found = false;
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
209
|
+
if (obj == null) return found;
|
|
210
|
+
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
|
211
|
+
found = any(obj, function(value) {
|
|
212
|
+
return value === target;
|
|
134
213
|
});
|
|
135
214
|
return found;
|
|
136
215
|
};
|
|
137
|
-
|
|
138
|
-
// Invoke a method with arguments on every item in a collection.
|
|
216
|
+
|
|
217
|
+
// Invoke a method (with arguments) on every item in a collection.
|
|
139
218
|
_.invoke = function(obj, method) {
|
|
140
|
-
var args =
|
|
219
|
+
var args = slice.call(arguments, 2);
|
|
141
220
|
return _.map(obj, function(value) {
|
|
142
|
-
return (method ?
|
|
221
|
+
return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
|
|
143
222
|
});
|
|
144
223
|
};
|
|
145
|
-
|
|
146
|
-
//
|
|
224
|
+
|
|
225
|
+
// Convenience version of a common use case of `map`: fetching a property.
|
|
147
226
|
_.pluck = function(obj, key) {
|
|
148
|
-
|
|
149
|
-
_.each(obj, function(value){ results.push(value[key]); });
|
|
150
|
-
return results;
|
|
227
|
+
return _.map(obj, function(value){ return value[key]; });
|
|
151
228
|
};
|
|
152
|
-
|
|
153
|
-
// Return the maximum
|
|
229
|
+
|
|
230
|
+
// Return the maximum element or (element-based computation).
|
|
154
231
|
_.max = function(obj, iterator, context) {
|
|
155
232
|
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
233
|
+
if (!iterator && _.isEmpty(obj)) return -Infinity;
|
|
234
|
+
var result = {computed : -Infinity};
|
|
235
|
+
each(obj, function(value, index, list) {
|
|
236
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
|
237
|
+
computed >= result.computed && (result = {value : value, computed : computed});
|
|
160
238
|
});
|
|
161
239
|
return result.value;
|
|
162
240
|
};
|
|
163
|
-
|
|
241
|
+
|
|
164
242
|
// Return the minimum element (or element-based computation).
|
|
165
243
|
_.min = function(obj, iterator, context) {
|
|
166
244
|
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
245
|
+
if (!iterator && _.isEmpty(obj)) return Infinity;
|
|
246
|
+
var result = {computed : Infinity};
|
|
247
|
+
each(obj, function(value, index, list) {
|
|
248
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
|
249
|
+
computed < result.computed && (result = {value : value, computed : computed});
|
|
171
250
|
});
|
|
172
251
|
return result.value;
|
|
173
252
|
};
|
|
174
|
-
|
|
175
|
-
//
|
|
253
|
+
|
|
254
|
+
// Shuffle an array.
|
|
255
|
+
_.shuffle = function(obj) {
|
|
256
|
+
var shuffled = [], rand;
|
|
257
|
+
each(obj, function(value, index, list) {
|
|
258
|
+
if (index == 0) {
|
|
259
|
+
shuffled[0] = value;
|
|
260
|
+
} else {
|
|
261
|
+
rand = Math.floor(Math.random() * (index + 1));
|
|
262
|
+
shuffled[index] = shuffled[rand];
|
|
263
|
+
shuffled[rand] = value;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
return shuffled;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// Sort the object's values by a criterion produced by an iterator.
|
|
176
270
|
_.sortBy = function(obj, iterator, context) {
|
|
177
|
-
return _.pluck(_.map(obj, function(value, index) {
|
|
271
|
+
return _.pluck(_.map(obj, function(value, index, list) {
|
|
178
272
|
return {
|
|
179
273
|
value : value,
|
|
180
|
-
criteria : iterator.call(context, value, index)
|
|
274
|
+
criteria : iterator.call(context, value, index, list)
|
|
181
275
|
};
|
|
182
276
|
}).sort(function(left, right) {
|
|
183
277
|
var a = left.criteria, b = right.criteria;
|
|
184
278
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
185
279
|
}), 'value');
|
|
186
280
|
};
|
|
187
|
-
|
|
281
|
+
|
|
282
|
+
// Groups the object's values by a criterion. Pass either a string attribute
|
|
283
|
+
// to group by, or a function that returns the criterion.
|
|
284
|
+
_.groupBy = function(obj, val) {
|
|
285
|
+
var result = {};
|
|
286
|
+
var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
|
|
287
|
+
each(obj, function(value, index) {
|
|
288
|
+
var key = iterator(value, index);
|
|
289
|
+
(result[key] || (result[key] = [])).push(value);
|
|
290
|
+
});
|
|
291
|
+
return result;
|
|
292
|
+
};
|
|
293
|
+
|
|
188
294
|
// Use a comparator function to figure out at what index an object should
|
|
189
295
|
// be inserted so as to maintain order. Uses binary search.
|
|
190
296
|
_.sortedIndex = function(array, obj, iterator) {
|
|
191
|
-
iterator
|
|
297
|
+
iterator || (iterator = _.identity);
|
|
192
298
|
var low = 0, high = array.length;
|
|
193
299
|
while (low < high) {
|
|
194
300
|
var mid = (low + high) >> 1;
|
|
@@ -196,265 +302,694 @@
|
|
|
196
302
|
}
|
|
197
303
|
return low;
|
|
198
304
|
};
|
|
199
|
-
|
|
200
|
-
//
|
|
305
|
+
|
|
306
|
+
// Safely convert anything iterable into a real, live array.
|
|
201
307
|
_.toArray = function(iterable) {
|
|
202
|
-
if (!iterable)
|
|
203
|
-
if (
|
|
204
|
-
|
|
308
|
+
if (!iterable) return [];
|
|
309
|
+
if (iterable.toArray) return iterable.toArray();
|
|
310
|
+
if (_.isArray(iterable)) return slice.call(iterable);
|
|
311
|
+
if (_.isArguments(iterable)) return slice.call(iterable);
|
|
312
|
+
return _.values(iterable);
|
|
205
313
|
};
|
|
206
|
-
|
|
314
|
+
|
|
207
315
|
// Return the number of elements in an object.
|
|
208
316
|
_.size = function(obj) {
|
|
209
317
|
return _.toArray(obj).length;
|
|
210
318
|
};
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
319
|
+
|
|
320
|
+
// Array Functions
|
|
321
|
+
// ---------------
|
|
322
|
+
|
|
323
|
+
// Get the first element of an array. Passing **n** will return the first N
|
|
324
|
+
// values in the array. Aliased as `head`. The **guard** check allows it to work
|
|
325
|
+
// with `_.map`.
|
|
326
|
+
_.first = _.head = function(array, n, guard) {
|
|
327
|
+
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// Returns everything but the last entry of the array. Especcialy useful on
|
|
331
|
+
// the arguments object. Passing **n** will return all the values in
|
|
332
|
+
// the array, excluding the last N. The **guard** check allows it to work with
|
|
333
|
+
// `_.map`.
|
|
334
|
+
_.initial = function(array, n, guard) {
|
|
335
|
+
return slice.call(array, 0, array.length - ((n == null) || guard ? 1 : n));
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// Get the last element of an array. Passing **n** will return the last N
|
|
339
|
+
// values in the array. The **guard** check allows it to work with `_.map`.
|
|
340
|
+
_.last = function(array, n, guard) {
|
|
341
|
+
if ((n != null) && !guard) {
|
|
342
|
+
return slice.call(array, Math.max(array.length - n, 0));
|
|
343
|
+
} else {
|
|
344
|
+
return array[array.length - 1];
|
|
345
|
+
}
|
|
217
346
|
};
|
|
218
|
-
|
|
219
|
-
//
|
|
220
|
-
|
|
221
|
-
|
|
347
|
+
|
|
348
|
+
// Returns everything but the first entry of the array. Aliased as `tail`.
|
|
349
|
+
// Especially useful on the arguments object. Passing an **index** will return
|
|
350
|
+
// the rest of the values in the array from that index onward. The **guard**
|
|
351
|
+
// check allows it to work with `_.map`.
|
|
352
|
+
_.rest = _.tail = function(array, index, guard) {
|
|
353
|
+
return slice.call(array, (index == null) || guard ? 1 : index);
|
|
222
354
|
};
|
|
223
|
-
|
|
355
|
+
|
|
224
356
|
// Trim out all falsy values from an array.
|
|
225
357
|
_.compact = function(array) {
|
|
226
|
-
return _.
|
|
358
|
+
return _.filter(array, function(value){ return !!value; });
|
|
227
359
|
};
|
|
228
|
-
|
|
360
|
+
|
|
229
361
|
// Return a completely flattened version of an array.
|
|
230
|
-
_.flatten = function(array) {
|
|
231
|
-
return _.reduce(array,
|
|
232
|
-
if (_.isArray(value)) return memo.concat(_.flatten(value));
|
|
233
|
-
memo.
|
|
362
|
+
_.flatten = function(array, shallow) {
|
|
363
|
+
return _.reduce(array, function(memo, value) {
|
|
364
|
+
if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
|
|
365
|
+
memo[memo.length] = value;
|
|
234
366
|
return memo;
|
|
235
|
-
});
|
|
367
|
+
}, []);
|
|
236
368
|
};
|
|
237
|
-
|
|
369
|
+
|
|
238
370
|
// Return a version of the array that does not contain the specified value(s).
|
|
239
371
|
_.without = function(array) {
|
|
240
|
-
|
|
241
|
-
return _.select(array, function(value){ return !_.include(values, value); });
|
|
372
|
+
return _.difference(array, slice.call(arguments, 1));
|
|
242
373
|
};
|
|
243
|
-
|
|
374
|
+
|
|
244
375
|
// Produce a duplicate-free version of the array. If the array has already
|
|
245
376
|
// been sorted, you have the option of using a faster algorithm.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
377
|
+
// Aliased as `unique`.
|
|
378
|
+
_.uniq = _.unique = function(array, isSorted, iterator) {
|
|
379
|
+
var initial = iterator ? _.map(array, iterator) : array;
|
|
380
|
+
var result = [];
|
|
381
|
+
_.reduce(initial, function(memo, el, i) {
|
|
382
|
+
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
|
|
383
|
+
memo[memo.length] = el;
|
|
384
|
+
result[result.length] = array[i];
|
|
385
|
+
}
|
|
249
386
|
return memo;
|
|
250
|
-
});
|
|
387
|
+
}, []);
|
|
388
|
+
return result;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
// Produce an array that contains the union: each distinct element from all of
|
|
392
|
+
// the passed-in arrays.
|
|
393
|
+
_.union = function() {
|
|
394
|
+
return _.uniq(_.flatten(arguments, true));
|
|
251
395
|
};
|
|
252
|
-
|
|
253
|
-
// Produce an array that contains every item shared between all the
|
|
254
|
-
// passed-in arrays.
|
|
255
|
-
_.intersect = function(array) {
|
|
256
|
-
var rest =
|
|
257
|
-
return _.
|
|
258
|
-
return _.
|
|
396
|
+
|
|
397
|
+
// Produce an array that contains every item shared between all the
|
|
398
|
+
// passed-in arrays. (Aliased as "intersect" for back-compat.)
|
|
399
|
+
_.intersection = _.intersect = function(array) {
|
|
400
|
+
var rest = slice.call(arguments, 1);
|
|
401
|
+
return _.filter(_.uniq(array), function(item) {
|
|
402
|
+
return _.every(rest, function(other) {
|
|
259
403
|
return _.indexOf(other, item) >= 0;
|
|
260
404
|
});
|
|
261
405
|
});
|
|
262
406
|
};
|
|
263
|
-
|
|
407
|
+
|
|
408
|
+
// Take the difference between one array and a number of other arrays.
|
|
409
|
+
// Only the elements present in just the first array will remain.
|
|
410
|
+
_.difference = function(array) {
|
|
411
|
+
var rest = _.flatten(slice.call(arguments, 1));
|
|
412
|
+
return _.filter(array, function(value){ return !_.include(rest, value); });
|
|
413
|
+
};
|
|
414
|
+
|
|
264
415
|
// Zip together multiple lists into a single array -- elements that share
|
|
265
416
|
// an index go together.
|
|
266
417
|
_.zip = function() {
|
|
267
|
-
var args =
|
|
418
|
+
var args = slice.call(arguments);
|
|
268
419
|
var length = _.max(_.pluck(args, 'length'));
|
|
269
420
|
var results = new Array(length);
|
|
270
|
-
for (var i=0; i<length; i++) results[i] = _.pluck(args,
|
|
421
|
+
for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
|
|
271
422
|
return results;
|
|
272
423
|
};
|
|
273
|
-
|
|
274
|
-
// If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
|
|
275
|
-
// we need this function. Return the position of the first
|
|
424
|
+
|
|
425
|
+
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
|
|
426
|
+
// we need this function. Return the position of the first occurrence of an
|
|
276
427
|
// item in an array, or -1 if the item is not included in the array.
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
428
|
+
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
|
|
429
|
+
// If the array is large and already in sort order, pass `true`
|
|
430
|
+
// for **isSorted** to use binary search.
|
|
431
|
+
_.indexOf = function(array, item, isSorted) {
|
|
432
|
+
if (array == null) return -1;
|
|
433
|
+
var i, l;
|
|
434
|
+
if (isSorted) {
|
|
435
|
+
i = _.sortedIndex(array, item);
|
|
436
|
+
return array[i] === item ? i : -1;
|
|
437
|
+
}
|
|
438
|
+
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
|
439
|
+
for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
|
|
280
440
|
return -1;
|
|
281
441
|
};
|
|
282
|
-
|
|
283
|
-
//
|
|
284
|
-
// if possible.
|
|
442
|
+
|
|
443
|
+
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
|
285
444
|
_.lastIndexOf = function(array, item) {
|
|
286
|
-
if (array
|
|
287
|
-
|
|
445
|
+
if (array == null) return -1;
|
|
446
|
+
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
|
|
447
|
+
var i = array.length;
|
|
448
|
+
while (i--) if (i in array && array[i] === item) return i;
|
|
288
449
|
return -1;
|
|
289
450
|
};
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
//
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
451
|
+
|
|
452
|
+
// Generate an integer Array containing an arithmetic progression. A port of
|
|
453
|
+
// the native Python `range()` function. See
|
|
454
|
+
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
|
455
|
+
_.range = function(start, stop, step) {
|
|
456
|
+
if (arguments.length <= 1) {
|
|
457
|
+
stop = start || 0;
|
|
458
|
+
start = 0;
|
|
459
|
+
}
|
|
460
|
+
step = arguments[2] || 1;
|
|
461
|
+
|
|
462
|
+
var len = Math.max(Math.ceil((stop - start) / step), 0);
|
|
463
|
+
var idx = 0;
|
|
464
|
+
var range = new Array(len);
|
|
465
|
+
|
|
466
|
+
while(idx < len) {
|
|
467
|
+
range[idx++] = start;
|
|
468
|
+
start += step;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return range;
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// Function (ahem) Functions
|
|
475
|
+
// ------------------
|
|
476
|
+
|
|
477
|
+
// Reusable constructor function for prototype setting.
|
|
478
|
+
var ctor = function(){};
|
|
479
|
+
|
|
480
|
+
// Create a function bound to a given object (assigning `this`, and arguments,
|
|
481
|
+
// optionally). Binding with arguments is also known as `curry`.
|
|
482
|
+
// Delegates to **ECMAScript 5**'s native `Function.bind` if available.
|
|
483
|
+
// We check for `func.bind` first, to fail fast when `func` is undefined.
|
|
484
|
+
_.bind = function bind(func, context) {
|
|
485
|
+
var bound, args;
|
|
486
|
+
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
|
|
487
|
+
if (!_.isFunction(func)) throw new TypeError;
|
|
488
|
+
args = slice.call(arguments, 2);
|
|
489
|
+
return bound = function() {
|
|
490
|
+
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
|
|
491
|
+
ctor.prototype = func.prototype;
|
|
492
|
+
var self = new ctor;
|
|
493
|
+
var result = func.apply(self, args.concat(slice.call(arguments)));
|
|
494
|
+
if (Object(result) === result) return result;
|
|
495
|
+
return self;
|
|
301
496
|
};
|
|
302
497
|
};
|
|
303
|
-
|
|
304
|
-
// Bind all of an object's methods to that object. Useful for ensuring that
|
|
498
|
+
|
|
499
|
+
// Bind all of an object's methods to that object. Useful for ensuring that
|
|
305
500
|
// all callbacks defined on an object belong to it.
|
|
306
|
-
_.bindAll = function() {
|
|
307
|
-
var
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
});
|
|
501
|
+
_.bindAll = function(obj) {
|
|
502
|
+
var funcs = slice.call(arguments, 1);
|
|
503
|
+
if (funcs.length == 0) funcs = _.functions(obj);
|
|
504
|
+
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
|
|
505
|
+
return obj;
|
|
312
506
|
};
|
|
313
|
-
|
|
507
|
+
|
|
508
|
+
// Memoize an expensive function by storing its results.
|
|
509
|
+
_.memoize = function(func, hasher) {
|
|
510
|
+
var memo = {};
|
|
511
|
+
hasher || (hasher = _.identity);
|
|
512
|
+
return function() {
|
|
513
|
+
var key = hasher.apply(this, arguments);
|
|
514
|
+
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
|
|
515
|
+
};
|
|
516
|
+
};
|
|
517
|
+
|
|
314
518
|
// Delays a function for the given number of milliseconds, and then calls
|
|
315
519
|
// it with the arguments supplied.
|
|
316
520
|
_.delay = function(func, wait) {
|
|
317
|
-
var args =
|
|
521
|
+
var args = slice.call(arguments, 2);
|
|
318
522
|
return setTimeout(function(){ return func.apply(func, args); }, wait);
|
|
319
523
|
};
|
|
320
|
-
|
|
321
|
-
// Defers a function, scheduling it to run after the current call stack has
|
|
524
|
+
|
|
525
|
+
// Defers a function, scheduling it to run after the current call stack has
|
|
322
526
|
// cleared.
|
|
323
527
|
_.defer = function(func) {
|
|
324
|
-
return _.delay.apply(_, [func, 1].concat(
|
|
528
|
+
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
// Returns a function, that, when invoked, will only be triggered at most once
|
|
532
|
+
// during a given window of time.
|
|
533
|
+
_.throttle = function(func, wait) {
|
|
534
|
+
var context, args, timeout, throttling, more;
|
|
535
|
+
var whenDone = _.debounce(function(){ more = throttling = false; }, wait);
|
|
536
|
+
return function() {
|
|
537
|
+
context = this; args = arguments;
|
|
538
|
+
var later = function() {
|
|
539
|
+
timeout = null;
|
|
540
|
+
if (more) func.apply(context, args);
|
|
541
|
+
whenDone();
|
|
542
|
+
};
|
|
543
|
+
if (!timeout) timeout = setTimeout(later, wait);
|
|
544
|
+
if (throttling) {
|
|
545
|
+
more = true;
|
|
546
|
+
} else {
|
|
547
|
+
func.apply(context, args);
|
|
548
|
+
}
|
|
549
|
+
whenDone();
|
|
550
|
+
throttling = true;
|
|
551
|
+
};
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
// Returns a function, that, as long as it continues to be invoked, will not
|
|
555
|
+
// be triggered. The function will be called after it stops being called for
|
|
556
|
+
// N milliseconds.
|
|
557
|
+
_.debounce = function(func, wait) {
|
|
558
|
+
var timeout;
|
|
559
|
+
return function() {
|
|
560
|
+
var context = this, args = arguments;
|
|
561
|
+
var later = function() {
|
|
562
|
+
timeout = null;
|
|
563
|
+
func.apply(context, args);
|
|
564
|
+
};
|
|
565
|
+
clearTimeout(timeout);
|
|
566
|
+
timeout = setTimeout(later, wait);
|
|
567
|
+
};
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
// Returns a function that will be executed at most one time, no matter how
|
|
571
|
+
// often you call it. Useful for lazy initialization.
|
|
572
|
+
_.once = function(func) {
|
|
573
|
+
var ran = false, memo;
|
|
574
|
+
return function() {
|
|
575
|
+
if (ran) return memo;
|
|
576
|
+
ran = true;
|
|
577
|
+
return memo = func.apply(this, arguments);
|
|
578
|
+
};
|
|
325
579
|
};
|
|
326
|
-
|
|
327
|
-
// Returns the first function passed as an argument to the second,
|
|
328
|
-
// allowing you to adjust arguments, run code before and after, and
|
|
580
|
+
|
|
581
|
+
// Returns the first function passed as an argument to the second,
|
|
582
|
+
// allowing you to adjust arguments, run code before and after, and
|
|
329
583
|
// conditionally execute the original function.
|
|
330
584
|
_.wrap = function(func, wrapper) {
|
|
331
585
|
return function() {
|
|
332
|
-
var args = [func].concat(
|
|
333
|
-
return wrapper.apply(
|
|
586
|
+
var args = [func].concat(slice.call(arguments, 0));
|
|
587
|
+
return wrapper.apply(this, args);
|
|
334
588
|
};
|
|
335
589
|
};
|
|
336
|
-
|
|
590
|
+
|
|
337
591
|
// Returns a function that is the composition of a list of functions, each
|
|
338
592
|
// consuming the return value of the function that follows.
|
|
339
593
|
_.compose = function() {
|
|
340
|
-
var funcs =
|
|
594
|
+
var funcs = arguments;
|
|
341
595
|
return function() {
|
|
342
|
-
|
|
343
|
-
|
|
596
|
+
var args = arguments;
|
|
597
|
+
for (var i = funcs.length - 1; i >= 0; i--) {
|
|
598
|
+
args = [funcs[i].apply(this, args)];
|
|
344
599
|
}
|
|
345
|
-
return
|
|
600
|
+
return args[0];
|
|
346
601
|
};
|
|
347
602
|
};
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
603
|
+
|
|
604
|
+
// Returns a function that will only be executed after being called N times.
|
|
605
|
+
_.after = function(times, func) {
|
|
606
|
+
if (times <= 0) return func();
|
|
607
|
+
return function() {
|
|
608
|
+
if (--times < 1) { return func.apply(this, arguments); }
|
|
609
|
+
};
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
// Object Functions
|
|
613
|
+
// ----------------
|
|
614
|
+
|
|
351
615
|
// Retrieve the names of an object's properties.
|
|
352
|
-
|
|
353
|
-
|
|
616
|
+
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
|
617
|
+
_.keys = nativeKeys || function(obj) {
|
|
618
|
+
if (obj !== Object(obj)) throw new TypeError('Invalid object');
|
|
619
|
+
var keys = [];
|
|
620
|
+
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
|
|
621
|
+
return keys;
|
|
354
622
|
};
|
|
355
|
-
|
|
623
|
+
|
|
356
624
|
// Retrieve the values of an object's properties.
|
|
357
625
|
_.values = function(obj) {
|
|
358
|
-
return _.
|
|
626
|
+
return _.map(obj, _.identity);
|
|
359
627
|
};
|
|
360
|
-
|
|
361
|
-
//
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
628
|
+
|
|
629
|
+
// Return a sorted list of the function names available on the object.
|
|
630
|
+
// Aliased as `methods`
|
|
631
|
+
_.functions = _.methods = function(obj) {
|
|
632
|
+
var names = [];
|
|
633
|
+
for (var key in obj) {
|
|
634
|
+
if (_.isFunction(obj[key])) names.push(key);
|
|
635
|
+
}
|
|
636
|
+
return names.sort();
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
// Extend a given object with all the properties in passed-in object(s).
|
|
640
|
+
_.extend = function(obj) {
|
|
641
|
+
each(slice.call(arguments, 1), function(source) {
|
|
642
|
+
for (var prop in source) {
|
|
643
|
+
if (source[prop] !== void 0) obj[prop] = source[prop];
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
return obj;
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
// Fill in a given object with default properties.
|
|
650
|
+
_.defaults = function(obj) {
|
|
651
|
+
each(slice.call(arguments, 1), function(source) {
|
|
652
|
+
for (var prop in source) {
|
|
653
|
+
if (obj[prop] == null) obj[prop] = source[prop];
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
return obj;
|
|
365
657
|
};
|
|
366
|
-
|
|
658
|
+
|
|
367
659
|
// Create a (shallow-cloned) duplicate of an object.
|
|
368
660
|
_.clone = function(obj) {
|
|
369
|
-
|
|
661
|
+
if (!_.isObject(obj)) return obj;
|
|
662
|
+
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
|
|
370
663
|
};
|
|
371
|
-
|
|
664
|
+
|
|
665
|
+
// Invokes interceptor with the obj, and then returns obj.
|
|
666
|
+
// The primary purpose of this method is to "tap into" a method chain, in
|
|
667
|
+
// order to perform operations on intermediate results within the chain.
|
|
668
|
+
_.tap = function(obj, interceptor) {
|
|
669
|
+
interceptor(obj);
|
|
670
|
+
return obj;
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
// Internal recursive comparison function.
|
|
674
|
+
function eq(a, b, stack) {
|
|
675
|
+
// Identical objects are equal. `0 === -0`, but they aren't identical.
|
|
676
|
+
// See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
|
|
677
|
+
if (a === b) return a !== 0 || 1 / a == 1 / b;
|
|
678
|
+
// A strict comparison is necessary because `null == undefined`.
|
|
679
|
+
if (a == null || b == null) return a === b;
|
|
680
|
+
// Unwrap any wrapped objects.
|
|
681
|
+
if (a._chain) a = a._wrapped;
|
|
682
|
+
if (b._chain) b = b._wrapped;
|
|
683
|
+
// Invoke a custom `isEqual` method if one is provided.
|
|
684
|
+
if (a.isEqual && _.isFunction(a.isEqual)) return a.isEqual(b);
|
|
685
|
+
if (b.isEqual && _.isFunction(b.isEqual)) return b.isEqual(a);
|
|
686
|
+
// Compare `[[Class]]` names.
|
|
687
|
+
var className = toString.call(a);
|
|
688
|
+
if (className != toString.call(b)) return false;
|
|
689
|
+
switch (className) {
|
|
690
|
+
// Strings, numbers, dates, and booleans are compared by value.
|
|
691
|
+
case '[object String]':
|
|
692
|
+
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
|
|
693
|
+
// equivalent to `new String("5")`.
|
|
694
|
+
return a == String(b);
|
|
695
|
+
case '[object Number]':
|
|
696
|
+
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
|
|
697
|
+
// other numeric values.
|
|
698
|
+
return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
|
|
699
|
+
case '[object Date]':
|
|
700
|
+
case '[object Boolean]':
|
|
701
|
+
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
|
|
702
|
+
// millisecond representations. Note that invalid dates with millisecond representations
|
|
703
|
+
// of `NaN` are not equivalent.
|
|
704
|
+
return +a == +b;
|
|
705
|
+
// RegExps are compared by their source patterns and flags.
|
|
706
|
+
case '[object RegExp]':
|
|
707
|
+
return a.source == b.source &&
|
|
708
|
+
a.global == b.global &&
|
|
709
|
+
a.multiline == b.multiline &&
|
|
710
|
+
a.ignoreCase == b.ignoreCase;
|
|
711
|
+
}
|
|
712
|
+
if (typeof a != 'object' || typeof b != 'object') return false;
|
|
713
|
+
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
|
714
|
+
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
|
715
|
+
var length = stack.length;
|
|
716
|
+
while (length--) {
|
|
717
|
+
// Linear search. Performance is inversely proportional to the number of
|
|
718
|
+
// unique nested structures.
|
|
719
|
+
if (stack[length] == a) return true;
|
|
720
|
+
}
|
|
721
|
+
// Add the first object to the stack of traversed objects.
|
|
722
|
+
stack.push(a);
|
|
723
|
+
var size = 0, result = true;
|
|
724
|
+
// Recursively compare objects and arrays.
|
|
725
|
+
if (className == '[object Array]') {
|
|
726
|
+
// Compare array lengths to determine if a deep comparison is necessary.
|
|
727
|
+
size = a.length;
|
|
728
|
+
result = size == b.length;
|
|
729
|
+
if (result) {
|
|
730
|
+
// Deep compare the contents, ignoring non-numeric properties.
|
|
731
|
+
while (size--) {
|
|
732
|
+
// Ensure commutative equality for sparse arrays.
|
|
733
|
+
if (!(result = size in a == size in b && eq(a[size], b[size], stack))) break;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
} else {
|
|
737
|
+
// Objects with different constructors are not equivalent.
|
|
738
|
+
if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
|
|
739
|
+
// Deep compare objects.
|
|
740
|
+
for (var key in a) {
|
|
741
|
+
if (hasOwnProperty.call(a, key)) {
|
|
742
|
+
// Count the expected number of properties.
|
|
743
|
+
size++;
|
|
744
|
+
// Deep compare each member.
|
|
745
|
+
if (!(result = hasOwnProperty.call(b, key) && eq(a[key], b[key], stack))) break;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
// Ensure that both objects contain the same number of properties.
|
|
749
|
+
if (result) {
|
|
750
|
+
for (key in b) {
|
|
751
|
+
if (hasOwnProperty.call(b, key) && !(size--)) break;
|
|
752
|
+
}
|
|
753
|
+
result = !size;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
// Remove the first object from the stack of traversed objects.
|
|
757
|
+
stack.pop();
|
|
758
|
+
return result;
|
|
759
|
+
}
|
|
760
|
+
|
|
372
761
|
// Perform a deep comparison to check if two objects are equal.
|
|
373
762
|
_.isEqual = function(a, b) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
if (
|
|
381
|
-
|
|
382
|
-
if (a.isEqual) return a.isEqual(b);
|
|
383
|
-
// If a is not an object by this point, we can't handle it.
|
|
384
|
-
if (atype !== 'object') return false;
|
|
385
|
-
// Nothing else worked, deep compare the contents.
|
|
386
|
-
var aKeys = _.keys(a), bKeys = _.keys(b);
|
|
387
|
-
// Different object sizes?
|
|
388
|
-
if (aKeys.length != bKeys.length) return false;
|
|
389
|
-
// Recursive comparison of contents.
|
|
390
|
-
for (var key in a) if (!_.isEqual(a[key], b[key])) return false;
|
|
763
|
+
return eq(a, b, []);
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
// Is a given array, string, or object empty?
|
|
767
|
+
// An "empty" object has no enumerable own-properties.
|
|
768
|
+
_.isEmpty = function(obj) {
|
|
769
|
+
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
|
|
770
|
+
for (var key in obj) if (hasOwnProperty.call(obj, key)) return false;
|
|
391
771
|
return true;
|
|
392
772
|
};
|
|
393
|
-
|
|
773
|
+
|
|
394
774
|
// Is a given value a DOM element?
|
|
395
775
|
_.isElement = function(obj) {
|
|
396
776
|
return !!(obj && obj.nodeType == 1);
|
|
397
777
|
};
|
|
398
|
-
|
|
399
|
-
// Is a given value
|
|
400
|
-
|
|
401
|
-
|
|
778
|
+
|
|
779
|
+
// Is a given value an array?
|
|
780
|
+
// Delegates to ECMA5's native Array.isArray
|
|
781
|
+
_.isArray = nativeIsArray || function(obj) {
|
|
782
|
+
return toString.call(obj) == '[object Array]';
|
|
402
783
|
};
|
|
403
|
-
|
|
404
|
-
// Is a given
|
|
784
|
+
|
|
785
|
+
// Is a given variable an object?
|
|
786
|
+
_.isObject = function(obj) {
|
|
787
|
+
return obj === Object(obj);
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
// Is a given variable an arguments object?
|
|
791
|
+
_.isArguments = function(obj) {
|
|
792
|
+
return toString.call(obj) == '[object Arguments]';
|
|
793
|
+
};
|
|
794
|
+
if (!_.isArguments(arguments)) {
|
|
795
|
+
_.isArguments = function(obj) {
|
|
796
|
+
return !!(obj && hasOwnProperty.call(obj, 'callee'));
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// Is a given value a function?
|
|
405
801
|
_.isFunction = function(obj) {
|
|
406
|
-
return
|
|
802
|
+
return toString.call(obj) == '[object Function]';
|
|
407
803
|
};
|
|
408
|
-
|
|
804
|
+
|
|
805
|
+
// Is a given value a string?
|
|
806
|
+
_.isString = function(obj) {
|
|
807
|
+
return toString.call(obj) == '[object String]';
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
// Is a given value a number?
|
|
811
|
+
_.isNumber = function(obj) {
|
|
812
|
+
return toString.call(obj) == '[object Number]';
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
// Is the given value `NaN`?
|
|
816
|
+
_.isNaN = function(obj) {
|
|
817
|
+
// `NaN` is the only value for which `===` is not reflexive.
|
|
818
|
+
return obj !== obj;
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
// Is a given value a boolean?
|
|
822
|
+
_.isBoolean = function(obj) {
|
|
823
|
+
return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
// Is a given value a date?
|
|
827
|
+
_.isDate = function(obj) {
|
|
828
|
+
return toString.call(obj) == '[object Date]';
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
// Is the given value a regular expression?
|
|
832
|
+
_.isRegExp = function(obj) {
|
|
833
|
+
return toString.call(obj) == '[object RegExp]';
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
// Is a given value equal to null?
|
|
837
|
+
_.isNull = function(obj) {
|
|
838
|
+
return obj === null;
|
|
839
|
+
};
|
|
840
|
+
|
|
409
841
|
// Is a given variable undefined?
|
|
410
842
|
_.isUndefined = function(obj) {
|
|
411
|
-
return
|
|
843
|
+
return obj === void 0;
|
|
412
844
|
};
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
845
|
+
|
|
846
|
+
// Utility Functions
|
|
847
|
+
// -----------------
|
|
848
|
+
|
|
849
|
+
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
|
|
417
850
|
// previous owner. Returns a reference to the Underscore object.
|
|
418
851
|
_.noConflict = function() {
|
|
419
852
|
root._ = previousUnderscore;
|
|
420
853
|
return this;
|
|
421
854
|
};
|
|
422
|
-
|
|
855
|
+
|
|
856
|
+
// Keep the identity function around for default iterators.
|
|
857
|
+
_.identity = function(value) {
|
|
858
|
+
return value;
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
// Run a function **n** times.
|
|
862
|
+
_.times = function (n, iterator, context) {
|
|
863
|
+
for (var i = 0; i < n; i++) iterator.call(context, i);
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
// Escape a string for HTML interpolation.
|
|
867
|
+
_.escape = function(string) {
|
|
868
|
+
return (''+string).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
|
|
869
|
+
};
|
|
870
|
+
|
|
871
|
+
// Add your own custom functions to the Underscore object, ensuring that
|
|
872
|
+
// they're correctly added to the OOP wrapper as well.
|
|
873
|
+
_.mixin = function(obj) {
|
|
874
|
+
each(_.functions(obj), function(name){
|
|
875
|
+
addToWrapper(name, _[name] = obj[name]);
|
|
876
|
+
});
|
|
877
|
+
};
|
|
878
|
+
|
|
423
879
|
// Generate a unique integer id (unique within the entire client session).
|
|
424
880
|
// Useful for temporary DOM ids.
|
|
881
|
+
var idCounter = 0;
|
|
425
882
|
_.uniqueId = function(prefix) {
|
|
426
|
-
var id =
|
|
883
|
+
var id = idCounter++;
|
|
427
884
|
return prefix ? prefix + id : id;
|
|
428
885
|
};
|
|
429
|
-
|
|
430
|
-
//
|
|
431
|
-
//
|
|
886
|
+
|
|
887
|
+
// By default, Underscore uses ERB-style template delimiters, change the
|
|
888
|
+
// following template settings to use alternative delimiters.
|
|
889
|
+
_.templateSettings = {
|
|
890
|
+
evaluate : /<%([\s\S]+?)%>/g,
|
|
891
|
+
interpolate : /<%=([\s\S]+?)%>/g,
|
|
892
|
+
escape : /<%-([\s\S]+?)%>/g
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
// When customizing `templateSettings`, if you don't want to define an
|
|
896
|
+
// interpolation, evaluation or escaping regex, we need one that is
|
|
897
|
+
// guaranteed not to match.
|
|
898
|
+
var noMatch = /.^/;
|
|
899
|
+
|
|
900
|
+
// JavaScript micro-templating, similar to John Resig's implementation.
|
|
901
|
+
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
|
902
|
+
// and correctly escapes quotes within interpolated code.
|
|
432
903
|
_.template = function(str, data) {
|
|
433
|
-
var
|
|
434
|
-
|
|
435
|
-
'with(obj){
|
|
436
|
-
str
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
904
|
+
var c = _.templateSettings;
|
|
905
|
+
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
|
906
|
+
'with(obj||{}){__p.push(\'' +
|
|
907
|
+
str.replace(/\\/g, '\\\\')
|
|
908
|
+
.replace(/'/g, "\\'")
|
|
909
|
+
.replace(c.escape || noMatch, function(match, code) {
|
|
910
|
+
return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
|
|
911
|
+
})
|
|
912
|
+
.replace(c.interpolate || noMatch, function(match, code) {
|
|
913
|
+
return "'," + code.replace(/\\'/g, "'") + ",'";
|
|
914
|
+
})
|
|
915
|
+
.replace(c.evaluate || noMatch, function(match, code) {
|
|
916
|
+
return "');" + code.replace(/\\'/g, "'")
|
|
917
|
+
.replace(/[\r\n\t]/g, ' ')
|
|
918
|
+
.replace(/\\\\/g, '\\') + ";__p.push('";
|
|
919
|
+
})
|
|
920
|
+
.replace(/\r/g, '\\r')
|
|
921
|
+
.replace(/\n/g, '\\n')
|
|
922
|
+
.replace(/\t/g, '\\t')
|
|
923
|
+
+ "');}return __p.join('');";
|
|
924
|
+
var func = new Function('obj', '_', tmpl);
|
|
925
|
+
if (data) return func(data, _);
|
|
926
|
+
return function(data) {
|
|
927
|
+
return func.call(this, data, _);
|
|
928
|
+
};
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
// Add a "chain" function, which will delegate to the wrapper.
|
|
932
|
+
_.chain = function(obj) {
|
|
933
|
+
return _(obj).chain();
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
// The OOP Wrapper
|
|
937
|
+
// ---------------
|
|
938
|
+
|
|
939
|
+
// If Underscore is called as a function, it returns a wrapped object that
|
|
940
|
+
// can be used OO-style. This wrapper holds altered versions of all the
|
|
941
|
+
// underscore functions. Wrapped objects may be chained.
|
|
942
|
+
var wrapper = function(obj) { this._wrapped = obj; };
|
|
943
|
+
|
|
944
|
+
// Expose `wrapper.prototype` as `_.prototype`
|
|
945
|
+
_.prototype = wrapper.prototype;
|
|
946
|
+
|
|
947
|
+
// Helper function to continue chaining intermediate results.
|
|
948
|
+
var result = function(obj, chain) {
|
|
949
|
+
return chain ? _(obj).chain() : obj;
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
// A method to easily add functions to the OOP wrapper.
|
|
953
|
+
var addToWrapper = function(name, func) {
|
|
954
|
+
wrapper.prototype[name] = function() {
|
|
955
|
+
var args = slice.call(arguments);
|
|
956
|
+
unshift.call(args, this._wrapped);
|
|
957
|
+
return result(func.apply(_, args), this._chain);
|
|
958
|
+
};
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
// Add all of the Underscore functions to the wrapper object.
|
|
962
|
+
_.mixin(_);
|
|
963
|
+
|
|
964
|
+
// Add all mutator Array functions to the wrapper.
|
|
965
|
+
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
|
966
|
+
var method = ArrayProto[name];
|
|
967
|
+
wrapper.prototype[name] = function() {
|
|
968
|
+
var wrapped = this._wrapped;
|
|
969
|
+
method.apply(wrapped, arguments);
|
|
970
|
+
var length = wrapped.length;
|
|
971
|
+
if ((name == 'shift' || name == 'splice') && length === 0) delete wrapped[0];
|
|
972
|
+
return result(wrapped, this._chain);
|
|
973
|
+
};
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
// Add all accessor Array functions to the wrapper.
|
|
977
|
+
each(['concat', 'join', 'slice'], function(name) {
|
|
978
|
+
var method = ArrayProto[name];
|
|
979
|
+
wrapper.prototype[name] = function() {
|
|
980
|
+
return result(method.apply(this._wrapped, arguments), this._chain);
|
|
981
|
+
};
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
// Start chaining a wrapped Underscore object.
|
|
985
|
+
wrapper.prototype.chain = function() {
|
|
986
|
+
this._chain = true;
|
|
987
|
+
return this;
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
// Extracts the result from a wrapped and chained object.
|
|
991
|
+
wrapper.prototype.value = function() {
|
|
992
|
+
return this._wrapped;
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
}).call(this);
|