backbone-rails 0.9.2 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- data/vendor/assets/javascripts/backbone.js +543 -441
- data/vendor/assets/javascripts/json2.js +3 -4
- data/vendor/assets/javascripts/underscore.js +478 -256
- metadata +10 -5
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
|
-
|
3
|
-
|
2
|
+
json2.js
|
3
|
+
2012-10-08
|
4
4
|
|
5
5
|
Public Domain.
|
6
6
|
|
@@ -159,8 +159,7 @@
|
|
159
159
|
// Create a JSON object only if one does not already exist. We create the
|
160
160
|
// methods in a closure to avoid creating global variables.
|
161
161
|
|
162
|
-
|
163
|
-
if (!JSON) {
|
162
|
+
if (typeof JSON !== 'object') {
|
164
163
|
JSON = {};
|
165
164
|
}
|
166
165
|
|
@@ -1,10 +1,7 @@
|
|
1
|
-
// Underscore.js 1.3
|
1
|
+
// Underscore.js 1.4.3
|
2
|
+
// http://underscorejs.org
|
2
3
|
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
3
|
-
// Underscore
|
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
|
4
|
+
// Underscore may be freely distributed under the MIT license.
|
8
5
|
|
9
6
|
(function() {
|
10
7
|
|
@@ -24,8 +21,9 @@
|
|
24
21
|
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
|
25
22
|
|
26
23
|
// Create quick reference variables for speed access to core prototypes.
|
27
|
-
var
|
28
|
-
|
24
|
+
var push = ArrayProto.push,
|
25
|
+
slice = ArrayProto.slice,
|
26
|
+
concat = ArrayProto.concat,
|
29
27
|
toString = ObjProto.toString,
|
30
28
|
hasOwnProperty = ObjProto.hasOwnProperty;
|
31
29
|
|
@@ -46,7 +44,11 @@
|
|
46
44
|
nativeBind = FuncProto.bind;
|
47
45
|
|
48
46
|
// Create a safe reference to the Underscore object for use below.
|
49
|
-
var _ = function(obj) {
|
47
|
+
var _ = function(obj) {
|
48
|
+
if (obj instanceof _) return obj;
|
49
|
+
if (!(this instanceof _)) return new _(obj);
|
50
|
+
this._wrapped = obj;
|
51
|
+
};
|
50
52
|
|
51
53
|
// Export the Underscore object for **Node.js**, with
|
52
54
|
// backwards-compatibility for the old `require()` API. If we're in
|
@@ -58,11 +60,11 @@
|
|
58
60
|
}
|
59
61
|
exports._ = _;
|
60
62
|
} else {
|
61
|
-
root
|
63
|
+
root._ = _;
|
62
64
|
}
|
63
65
|
|
64
66
|
// Current version.
|
65
|
-
_.VERSION = '1.3
|
67
|
+
_.VERSION = '1.4.3';
|
66
68
|
|
67
69
|
// Collection Functions
|
68
70
|
// --------------------
|
@@ -76,7 +78,7 @@
|
|
76
78
|
obj.forEach(iterator, context);
|
77
79
|
} else if (obj.length === +obj.length) {
|
78
80
|
for (var i = 0, l = obj.length; i < l; i++) {
|
79
|
-
if (
|
81
|
+
if (iterator.call(context, obj[i], i, obj) === breaker) return;
|
80
82
|
}
|
81
83
|
} else {
|
82
84
|
for (var key in obj) {
|
@@ -96,10 +98,11 @@
|
|
96
98
|
each(obj, function(value, index, list) {
|
97
99
|
results[results.length] = iterator.call(context, value, index, list);
|
98
100
|
});
|
99
|
-
if (obj.length === +obj.length) results.length = obj.length;
|
100
101
|
return results;
|
101
102
|
};
|
102
103
|
|
104
|
+
var reduceError = 'Reduce of empty array with no initial value';
|
105
|
+
|
103
106
|
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
104
107
|
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
105
108
|
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
@@ -117,7 +120,7 @@
|
|
117
120
|
memo = iterator.call(context, memo, value, index, list);
|
118
121
|
}
|
119
122
|
});
|
120
|
-
if (!initial) throw new TypeError(
|
123
|
+
if (!initial) throw new TypeError(reduceError);
|
121
124
|
return memo;
|
122
125
|
};
|
123
126
|
|
@@ -130,9 +133,22 @@
|
|
130
133
|
if (context) iterator = _.bind(iterator, context);
|
131
134
|
return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
132
135
|
}
|
133
|
-
var
|
134
|
-
if (
|
135
|
-
|
136
|
+
var length = obj.length;
|
137
|
+
if (length !== +length) {
|
138
|
+
var keys = _.keys(obj);
|
139
|
+
length = keys.length;
|
140
|
+
}
|
141
|
+
each(obj, function(value, index, list) {
|
142
|
+
index = keys ? keys[--length] : --length;
|
143
|
+
if (!initial) {
|
144
|
+
memo = obj[index];
|
145
|
+
initial = true;
|
146
|
+
} else {
|
147
|
+
memo = iterator.call(context, memo, obj[index], index, list);
|
148
|
+
}
|
149
|
+
});
|
150
|
+
if (!initial) throw new TypeError(reduceError);
|
151
|
+
return memo;
|
136
152
|
};
|
137
153
|
|
138
154
|
// Return the first value which passes a truth test. Aliased as `detect`.
|
@@ -162,25 +178,23 @@
|
|
162
178
|
|
163
179
|
// Return all the elements for which a truth test fails.
|
164
180
|
_.reject = function(obj, iterator, context) {
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
169
|
-
});
|
170
|
-
return results;
|
181
|
+
return _.filter(obj, function(value, index, list) {
|
182
|
+
return !iterator.call(context, value, index, list);
|
183
|
+
}, context);
|
171
184
|
};
|
172
185
|
|
173
186
|
// Determine whether all of the elements match a truth test.
|
174
187
|
// Delegates to **ECMAScript 5**'s native `every` if available.
|
175
188
|
// Aliased as `all`.
|
176
189
|
_.every = _.all = function(obj, iterator, context) {
|
190
|
+
iterator || (iterator = _.identity);
|
177
191
|
var result = true;
|
178
192
|
if (obj == null) return result;
|
179
193
|
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
180
194
|
each(obj, function(value, index, list) {
|
181
195
|
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
|
182
196
|
});
|
183
|
-
return result;
|
197
|
+
return !!result;
|
184
198
|
};
|
185
199
|
|
186
200
|
// Determine if at least one element in the object matches a truth test.
|
@@ -197,23 +211,21 @@
|
|
197
211
|
return !!result;
|
198
212
|
};
|
199
213
|
|
200
|
-
// Determine if
|
201
|
-
// Aliased as `
|
202
|
-
_.
|
203
|
-
|
204
|
-
if (obj == null) return found;
|
214
|
+
// Determine if the array or object contains a given value (using `===`).
|
215
|
+
// Aliased as `include`.
|
216
|
+
_.contains = _.include = function(obj, target) {
|
217
|
+
if (obj == null) return false;
|
205
218
|
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
206
|
-
|
219
|
+
return any(obj, function(value) {
|
207
220
|
return value === target;
|
208
221
|
});
|
209
|
-
return found;
|
210
222
|
};
|
211
223
|
|
212
224
|
// Invoke a method (with arguments) on every item in a collection.
|
213
225
|
_.invoke = function(obj, method) {
|
214
226
|
var args = slice.call(arguments, 2);
|
215
227
|
return _.map(obj, function(value) {
|
216
|
-
return (_.isFunction(method) ? method
|
228
|
+
return (_.isFunction(method) ? method : value[method]).apply(value, args);
|
217
229
|
});
|
218
230
|
};
|
219
231
|
|
@@ -222,11 +234,27 @@
|
|
222
234
|
return _.map(obj, function(value){ return value[key]; });
|
223
235
|
};
|
224
236
|
|
237
|
+
// Convenience version of a common use case of `filter`: selecting only objects
|
238
|
+
// with specific `key:value` pairs.
|
239
|
+
_.where = function(obj, attrs) {
|
240
|
+
if (_.isEmpty(attrs)) return [];
|
241
|
+
return _.filter(obj, function(value) {
|
242
|
+
for (var key in attrs) {
|
243
|
+
if (attrs[key] !== value[key]) return false;
|
244
|
+
}
|
245
|
+
return true;
|
246
|
+
});
|
247
|
+
};
|
248
|
+
|
225
249
|
// Return the maximum element or (element-based computation).
|
250
|
+
// Can't optimize arrays of integers longer than 65,535 elements.
|
251
|
+
// See: https://bugs.webkit.org/show_bug.cgi?id=80797
|
226
252
|
_.max = function(obj, iterator, context) {
|
227
|
-
if (!iterator && _.isArray(obj)
|
253
|
+
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
|
254
|
+
return Math.max.apply(Math, obj);
|
255
|
+
}
|
228
256
|
if (!iterator && _.isEmpty(obj)) return -Infinity;
|
229
|
-
var result = {computed : -Infinity};
|
257
|
+
var result = {computed : -Infinity, value: -Infinity};
|
230
258
|
each(obj, function(value, index, list) {
|
231
259
|
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
232
260
|
computed >= result.computed && (result = {value : value, computed : computed});
|
@@ -236,9 +264,11 @@
|
|
236
264
|
|
237
265
|
// Return the minimum element (or element-based computation).
|
238
266
|
_.min = function(obj, iterator, context) {
|
239
|
-
if (!iterator && _.isArray(obj)
|
267
|
+
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
|
268
|
+
return Math.min.apply(Math, obj);
|
269
|
+
}
|
240
270
|
if (!iterator && _.isEmpty(obj)) return Infinity;
|
241
|
-
var result = {computed : Infinity};
|
271
|
+
var result = {computed : Infinity, value: Infinity};
|
242
272
|
each(obj, function(value, index, list) {
|
243
273
|
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
244
274
|
computed < result.computed && (result = {value : value, computed : computed});
|
@@ -248,81 +278,110 @@
|
|
248
278
|
|
249
279
|
// Shuffle an array.
|
250
280
|
_.shuffle = function(obj) {
|
251
|
-
var
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
shuffled[rand] = value;
|
259
|
-
}
|
281
|
+
var rand;
|
282
|
+
var index = 0;
|
283
|
+
var shuffled = [];
|
284
|
+
each(obj, function(value) {
|
285
|
+
rand = _.random(index++);
|
286
|
+
shuffled[index - 1] = shuffled[rand];
|
287
|
+
shuffled[rand] = value;
|
260
288
|
});
|
261
289
|
return shuffled;
|
262
290
|
};
|
263
291
|
|
292
|
+
// An internal function to generate lookup iterators.
|
293
|
+
var lookupIterator = function(value) {
|
294
|
+
return _.isFunction(value) ? value : function(obj){ return obj[value]; };
|
295
|
+
};
|
296
|
+
|
264
297
|
// Sort the object's values by a criterion produced by an iterator.
|
265
|
-
_.sortBy = function(obj,
|
298
|
+
_.sortBy = function(obj, value, context) {
|
299
|
+
var iterator = lookupIterator(value);
|
266
300
|
return _.pluck(_.map(obj, function(value, index, list) {
|
267
301
|
return {
|
268
302
|
value : value,
|
303
|
+
index : index,
|
269
304
|
criteria : iterator.call(context, value, index, list)
|
270
305
|
};
|
271
306
|
}).sort(function(left, right) {
|
272
|
-
var a = left.criteria
|
273
|
-
|
307
|
+
var a = left.criteria;
|
308
|
+
var b = right.criteria;
|
309
|
+
if (a !== b) {
|
310
|
+
if (a > b || a === void 0) return 1;
|
311
|
+
if (a < b || b === void 0) return -1;
|
312
|
+
}
|
313
|
+
return left.index < right.index ? -1 : 1;
|
274
314
|
}), 'value');
|
275
315
|
};
|
276
316
|
|
277
|
-
//
|
278
|
-
|
279
|
-
_.groupBy = function(obj, val) {
|
317
|
+
// An internal function used for aggregate "group by" operations.
|
318
|
+
var group = function(obj, value, context, behavior) {
|
280
319
|
var result = {};
|
281
|
-
var iterator =
|
320
|
+
var iterator = lookupIterator(value || _.identity);
|
282
321
|
each(obj, function(value, index) {
|
283
|
-
var key = iterator(value, index);
|
284
|
-
(result
|
322
|
+
var key = iterator.call(context, value, index, obj);
|
323
|
+
behavior(result, key, value);
|
285
324
|
});
|
286
325
|
return result;
|
287
326
|
};
|
288
327
|
|
289
|
-
//
|
290
|
-
//
|
291
|
-
_.
|
292
|
-
|
328
|
+
// Groups the object's values by a criterion. Pass either a string attribute
|
329
|
+
// to group by, or a function that returns the criterion.
|
330
|
+
_.groupBy = function(obj, value, context) {
|
331
|
+
return group(obj, value, context, function(result, key, value) {
|
332
|
+
(_.has(result, key) ? result[key] : (result[key] = [])).push(value);
|
333
|
+
});
|
334
|
+
};
|
335
|
+
|
336
|
+
// Counts instances of an object that group by a certain criterion. Pass
|
337
|
+
// either a string attribute to count by, or a function that returns the
|
338
|
+
// criterion.
|
339
|
+
_.countBy = function(obj, value, context) {
|
340
|
+
return group(obj, value, context, function(result, key) {
|
341
|
+
if (!_.has(result, key)) result[key] = 0;
|
342
|
+
result[key]++;
|
343
|
+
});
|
344
|
+
};
|
345
|
+
|
346
|
+
// Use a comparator function to figure out the smallest index at which
|
347
|
+
// an object should be inserted so as to maintain order. Uses binary search.
|
348
|
+
_.sortedIndex = function(array, obj, iterator, context) {
|
349
|
+
iterator = iterator == null ? _.identity : lookupIterator(iterator);
|
350
|
+
var value = iterator.call(context, obj);
|
293
351
|
var low = 0, high = array.length;
|
294
352
|
while (low < high) {
|
295
|
-
var mid = (low + high)
|
296
|
-
iterator(array[mid]) <
|
353
|
+
var mid = (low + high) >>> 1;
|
354
|
+
iterator.call(context, array[mid]) < value ? low = mid + 1 : high = mid;
|
297
355
|
}
|
298
356
|
return low;
|
299
357
|
};
|
300
358
|
|
301
359
|
// Safely convert anything iterable into a real, live array.
|
302
|
-
_.toArray = function(
|
303
|
-
if (!
|
304
|
-
if (
|
305
|
-
if (
|
306
|
-
|
307
|
-
return _.values(iterable);
|
360
|
+
_.toArray = function(obj) {
|
361
|
+
if (!obj) return [];
|
362
|
+
if (_.isArray(obj)) return slice.call(obj);
|
363
|
+
if (obj.length === +obj.length) return _.map(obj, _.identity);
|
364
|
+
return _.values(obj);
|
308
365
|
};
|
309
366
|
|
310
367
|
// Return the number of elements in an object.
|
311
368
|
_.size = function(obj) {
|
312
|
-
|
369
|
+
if (obj == null) return 0;
|
370
|
+
return (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
|
313
371
|
};
|
314
372
|
|
315
373
|
// Array Functions
|
316
374
|
// ---------------
|
317
375
|
|
318
376
|
// Get the first element of an array. Passing **n** will return the first N
|
319
|
-
// values in the array. Aliased as `head`. The **guard** check
|
320
|
-
// with `_.map`.
|
321
|
-
_.first = _.head = function(array, n, guard) {
|
377
|
+
// values in the array. Aliased as `head` and `take`. The **guard** check
|
378
|
+
// allows it to work with `_.map`.
|
379
|
+
_.first = _.head = _.take = function(array, n, guard) {
|
380
|
+
if (array == null) return void 0;
|
322
381
|
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
|
323
382
|
};
|
324
383
|
|
325
|
-
// Returns everything but the last entry of the array.
|
384
|
+
// Returns everything but the last entry of the array. Especially useful on
|
326
385
|
// the arguments object. Passing **n** will return all the values in
|
327
386
|
// the array, excluding the last N. The **guard** check allows it to work with
|
328
387
|
// `_.map`.
|
@@ -333,6 +392,7 @@
|
|
333
392
|
// Get the last element of an array. Passing **n** will return the last N
|
334
393
|
// values in the array. The **guard** check allows it to work with `_.map`.
|
335
394
|
_.last = function(array, n, guard) {
|
395
|
+
if (array == null) return void 0;
|
336
396
|
if ((n != null) && !guard) {
|
337
397
|
return slice.call(array, Math.max(array.length - n, 0));
|
338
398
|
} else {
|
@@ -340,26 +400,34 @@
|
|
340
400
|
}
|
341
401
|
};
|
342
402
|
|
343
|
-
// Returns everything but the first entry of the array. Aliased as `tail`.
|
344
|
-
// Especially useful on the arguments object. Passing an **
|
345
|
-
// the rest
|
403
|
+
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
|
404
|
+
// Especially useful on the arguments object. Passing an **n** will return
|
405
|
+
// the rest N values in the array. The **guard**
|
346
406
|
// check allows it to work with `_.map`.
|
347
|
-
_.rest = _.tail = function(array,
|
348
|
-
return slice.call(array, (
|
407
|
+
_.rest = _.tail = _.drop = function(array, n, guard) {
|
408
|
+
return slice.call(array, (n == null) || guard ? 1 : n);
|
349
409
|
};
|
350
410
|
|
351
411
|
// Trim out all falsy values from an array.
|
352
412
|
_.compact = function(array) {
|
353
|
-
return _.filter(array,
|
413
|
+
return _.filter(array, _.identity);
|
414
|
+
};
|
415
|
+
|
416
|
+
// Internal implementation of a recursive `flatten` function.
|
417
|
+
var flatten = function(input, shallow, output) {
|
418
|
+
each(input, function(value) {
|
419
|
+
if (_.isArray(value)) {
|
420
|
+
shallow ? push.apply(output, value) : flatten(value, shallow, output);
|
421
|
+
} else {
|
422
|
+
output.push(value);
|
423
|
+
}
|
424
|
+
});
|
425
|
+
return output;
|
354
426
|
};
|
355
427
|
|
356
428
|
// Return a completely flattened version of an array.
|
357
429
|
_.flatten = function(array, shallow) {
|
358
|
-
return
|
359
|
-
if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
|
360
|
-
memo[memo.length] = value;
|
361
|
-
return memo;
|
362
|
-
}, []);
|
430
|
+
return flatten(array, shallow, []);
|
363
431
|
};
|
364
432
|
|
365
433
|
// Return a version of the array that does not contain the specified value(s).
|
@@ -370,28 +438,33 @@
|
|
370
438
|
// Produce a duplicate-free version of the array. If the array has already
|
371
439
|
// been sorted, you have the option of using a faster algorithm.
|
372
440
|
// Aliased as `unique`.
|
373
|
-
_.uniq = _.unique = function(array, isSorted, iterator) {
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
441
|
+
_.uniq = _.unique = function(array, isSorted, iterator, context) {
|
442
|
+
if (_.isFunction(isSorted)) {
|
443
|
+
context = iterator;
|
444
|
+
iterator = isSorted;
|
445
|
+
isSorted = false;
|
446
|
+
}
|
447
|
+
var initial = iterator ? _.map(array, iterator, context) : array;
|
448
|
+
var results = [];
|
449
|
+
var seen = [];
|
450
|
+
each(initial, function(value, index) {
|
451
|
+
if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.contains(seen, value)) {
|
452
|
+
seen.push(value);
|
453
|
+
results.push(array[index]);
|
380
454
|
}
|
381
|
-
|
382
|
-
|
383
|
-
return result;
|
455
|
+
});
|
456
|
+
return results;
|
384
457
|
};
|
385
458
|
|
386
459
|
// Produce an array that contains the union: each distinct element from all of
|
387
460
|
// the passed-in arrays.
|
388
461
|
_.union = function() {
|
389
|
-
return _.uniq(
|
462
|
+
return _.uniq(concat.apply(ArrayProto, arguments));
|
390
463
|
};
|
391
464
|
|
392
465
|
// Produce an array that contains every item shared between all the
|
393
|
-
// passed-in arrays.
|
394
|
-
_.intersection =
|
466
|
+
// passed-in arrays.
|
467
|
+
_.intersection = function(array) {
|
395
468
|
var rest = slice.call(arguments, 1);
|
396
469
|
return _.filter(_.uniq(array), function(item) {
|
397
470
|
return _.every(rest, function(other) {
|
@@ -403,8 +476,8 @@
|
|
403
476
|
// Take the difference between one array and a number of other arrays.
|
404
477
|
// Only the elements present in just the first array will remain.
|
405
478
|
_.difference = function(array) {
|
406
|
-
var rest =
|
407
|
-
return _.filter(array, function(value){ return !_.
|
479
|
+
var rest = concat.apply(ArrayProto, slice.call(arguments, 1));
|
480
|
+
return _.filter(array, function(value){ return !_.contains(rest, value); });
|
408
481
|
};
|
409
482
|
|
410
483
|
// Zip together multiple lists into a single array -- elements that share
|
@@ -413,10 +486,28 @@
|
|
413
486
|
var args = slice.call(arguments);
|
414
487
|
var length = _.max(_.pluck(args, 'length'));
|
415
488
|
var results = new Array(length);
|
416
|
-
for (var i = 0; i < length; i++)
|
489
|
+
for (var i = 0; i < length; i++) {
|
490
|
+
results[i] = _.pluck(args, "" + i);
|
491
|
+
}
|
417
492
|
return results;
|
418
493
|
};
|
419
494
|
|
495
|
+
// Converts lists into objects. Pass either a single array of `[key, value]`
|
496
|
+
// pairs, or two parallel arrays of the same length -- one of keys, and one of
|
497
|
+
// the corresponding values.
|
498
|
+
_.object = function(list, values) {
|
499
|
+
if (list == null) return {};
|
500
|
+
var result = {};
|
501
|
+
for (var i = 0, l = list.length; i < l; i++) {
|
502
|
+
if (values) {
|
503
|
+
result[list[i]] = values[i];
|
504
|
+
} else {
|
505
|
+
result[list[i][0]] = list[i][1];
|
506
|
+
}
|
507
|
+
}
|
508
|
+
return result;
|
509
|
+
};
|
510
|
+
|
420
511
|
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
|
421
512
|
// we need this function. Return the position of the first occurrence of an
|
422
513
|
// item in an array, or -1 if the item is not included in the array.
|
@@ -425,22 +516,29 @@
|
|
425
516
|
// for **isSorted** to use binary search.
|
426
517
|
_.indexOf = function(array, item, isSorted) {
|
427
518
|
if (array == null) return -1;
|
428
|
-
var i, l;
|
519
|
+
var i = 0, l = array.length;
|
429
520
|
if (isSorted) {
|
430
|
-
|
431
|
-
|
521
|
+
if (typeof isSorted == 'number') {
|
522
|
+
i = (isSorted < 0 ? Math.max(0, l + isSorted) : isSorted);
|
523
|
+
} else {
|
524
|
+
i = _.sortedIndex(array, item);
|
525
|
+
return array[i] === item ? i : -1;
|
526
|
+
}
|
432
527
|
}
|
433
|
-
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
434
|
-
for (
|
528
|
+
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
|
529
|
+
for (; i < l; i++) if (array[i] === item) return i;
|
435
530
|
return -1;
|
436
531
|
};
|
437
532
|
|
438
533
|
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
439
|
-
_.lastIndexOf = function(array, item) {
|
534
|
+
_.lastIndexOf = function(array, item, from) {
|
440
535
|
if (array == null) return -1;
|
441
|
-
|
442
|
-
|
443
|
-
|
536
|
+
var hasIndex = from != null;
|
537
|
+
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) {
|
538
|
+
return hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item);
|
539
|
+
}
|
540
|
+
var i = (hasIndex ? from : array.length);
|
541
|
+
while (i--) if (array[i] === item) return i;
|
444
542
|
return -1;
|
445
543
|
};
|
446
544
|
|
@@ -476,8 +574,8 @@
|
|
476
574
|
// optionally). Binding with arguments is also known as `curry`.
|
477
575
|
// Delegates to **ECMAScript 5**'s native `Function.bind` if available.
|
478
576
|
// We check for `func.bind` first, to fail fast when `func` is undefined.
|
479
|
-
_.bind = function
|
480
|
-
var
|
577
|
+
_.bind = function(func, context) {
|
578
|
+
var args, bound;
|
481
579
|
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
|
482
580
|
if (!_.isFunction(func)) throw new TypeError;
|
483
581
|
args = slice.call(arguments, 2);
|
@@ -485,6 +583,7 @@
|
|
485
583
|
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
|
486
584
|
ctor.prototype = func.prototype;
|
487
585
|
var self = new ctor;
|
586
|
+
ctor.prototype = null;
|
488
587
|
var result = func.apply(self, args.concat(slice.call(arguments)));
|
489
588
|
if (Object(result) === result) return result;
|
490
589
|
return self;
|
@@ -514,7 +613,7 @@
|
|
514
613
|
// it with the arguments supplied.
|
515
614
|
_.delay = function(func, wait) {
|
516
615
|
var args = slice.call(arguments, 2);
|
517
|
-
return setTimeout(function(){ return func.apply(
|
616
|
+
return setTimeout(function(){ return func.apply(null, args); }, wait);
|
518
617
|
};
|
519
618
|
|
520
619
|
// Defers a function, scheduling it to run after the current call stack has
|
@@ -526,39 +625,47 @@
|
|
526
625
|
// Returns a function, that, when invoked, will only be triggered at most once
|
527
626
|
// during a given window of time.
|
528
627
|
_.throttle = function(func, wait) {
|
529
|
-
var context, args, timeout,
|
530
|
-
var
|
628
|
+
var context, args, timeout, result;
|
629
|
+
var previous = 0;
|
630
|
+
var later = function() {
|
631
|
+
previous = new Date;
|
632
|
+
timeout = null;
|
633
|
+
result = func.apply(context, args);
|
634
|
+
};
|
531
635
|
return function() {
|
532
|
-
|
533
|
-
var
|
636
|
+
var now = new Date;
|
637
|
+
var remaining = wait - (now - previous);
|
638
|
+
context = this;
|
639
|
+
args = arguments;
|
640
|
+
if (remaining <= 0) {
|
641
|
+
clearTimeout(timeout);
|
534
642
|
timeout = null;
|
535
|
-
|
536
|
-
|
537
|
-
}
|
538
|
-
|
539
|
-
if (throttling) {
|
540
|
-
more = true;
|
541
|
-
} else {
|
542
|
-
func.apply(context, args);
|
643
|
+
previous = now;
|
644
|
+
result = func.apply(context, args);
|
645
|
+
} else if (!timeout) {
|
646
|
+
timeout = setTimeout(later, remaining);
|
543
647
|
}
|
544
|
-
|
545
|
-
throttling = true;
|
648
|
+
return result;
|
546
649
|
};
|
547
650
|
};
|
548
651
|
|
549
652
|
// Returns a function, that, as long as it continues to be invoked, will not
|
550
653
|
// be triggered. The function will be called after it stops being called for
|
551
|
-
// N milliseconds.
|
552
|
-
|
553
|
-
|
654
|
+
// N milliseconds. If `immediate` is passed, trigger the function on the
|
655
|
+
// leading edge, instead of the trailing.
|
656
|
+
_.debounce = function(func, wait, immediate) {
|
657
|
+
var timeout, result;
|
554
658
|
return function() {
|
555
659
|
var context = this, args = arguments;
|
556
660
|
var later = function() {
|
557
661
|
timeout = null;
|
558
|
-
func.apply(context, args);
|
662
|
+
if (!immediate) result = func.apply(context, args);
|
559
663
|
};
|
664
|
+
var callNow = immediate && !timeout;
|
560
665
|
clearTimeout(timeout);
|
561
666
|
timeout = setTimeout(later, wait);
|
667
|
+
if (callNow) result = func.apply(context, args);
|
668
|
+
return result;
|
562
669
|
};
|
563
670
|
};
|
564
671
|
|
@@ -569,7 +676,9 @@
|
|
569
676
|
return function() {
|
570
677
|
if (ran) return memo;
|
571
678
|
ran = true;
|
572
|
-
|
679
|
+
memo = func.apply(this, arguments);
|
680
|
+
func = null;
|
681
|
+
return memo;
|
573
682
|
};
|
574
683
|
};
|
575
684
|
|
@@ -578,7 +687,8 @@
|
|
578
687
|
// conditionally execute the original function.
|
579
688
|
_.wrap = function(func, wrapper) {
|
580
689
|
return function() {
|
581
|
-
var args = [func]
|
690
|
+
var args = [func];
|
691
|
+
push.apply(args, arguments);
|
582
692
|
return wrapper.apply(this, args);
|
583
693
|
};
|
584
694
|
};
|
@@ -600,7 +710,9 @@
|
|
600
710
|
_.after = function(times, func) {
|
601
711
|
if (times <= 0) return func();
|
602
712
|
return function() {
|
603
|
-
if (--times < 1) {
|
713
|
+
if (--times < 1) {
|
714
|
+
return func.apply(this, arguments);
|
715
|
+
}
|
604
716
|
};
|
605
717
|
};
|
606
718
|
|
@@ -618,7 +730,23 @@
|
|
618
730
|
|
619
731
|
// Retrieve the values of an object's properties.
|
620
732
|
_.values = function(obj) {
|
621
|
-
|
733
|
+
var values = [];
|
734
|
+
for (var key in obj) if (_.has(obj, key)) values.push(obj[key]);
|
735
|
+
return values;
|
736
|
+
};
|
737
|
+
|
738
|
+
// Convert an object into a list of `[key, value]` pairs.
|
739
|
+
_.pairs = function(obj) {
|
740
|
+
var pairs = [];
|
741
|
+
for (var key in obj) if (_.has(obj, key)) pairs.push([key, obj[key]]);
|
742
|
+
return pairs;
|
743
|
+
};
|
744
|
+
|
745
|
+
// Invert the keys and values of an object. The values must be serializable.
|
746
|
+
_.invert = function(obj) {
|
747
|
+
var result = {};
|
748
|
+
for (var key in obj) if (_.has(obj, key)) result[obj[key]] = key;
|
749
|
+
return result;
|
622
750
|
};
|
623
751
|
|
624
752
|
// Return a sorted list of the function names available on the object.
|
@@ -634,18 +762,42 @@
|
|
634
762
|
// Extend a given object with all the properties in passed-in object(s).
|
635
763
|
_.extend = function(obj) {
|
636
764
|
each(slice.call(arguments, 1), function(source) {
|
637
|
-
|
638
|
-
|
765
|
+
if (source) {
|
766
|
+
for (var prop in source) {
|
767
|
+
obj[prop] = source[prop];
|
768
|
+
}
|
639
769
|
}
|
640
770
|
});
|
641
771
|
return obj;
|
642
772
|
};
|
643
773
|
|
774
|
+
// Return a copy of the object only containing the whitelisted properties.
|
775
|
+
_.pick = function(obj) {
|
776
|
+
var copy = {};
|
777
|
+
var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
|
778
|
+
each(keys, function(key) {
|
779
|
+
if (key in obj) copy[key] = obj[key];
|
780
|
+
});
|
781
|
+
return copy;
|
782
|
+
};
|
783
|
+
|
784
|
+
// Return a copy of the object without the blacklisted properties.
|
785
|
+
_.omit = function(obj) {
|
786
|
+
var copy = {};
|
787
|
+
var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
|
788
|
+
for (var key in obj) {
|
789
|
+
if (!_.contains(keys, key)) copy[key] = obj[key];
|
790
|
+
}
|
791
|
+
return copy;
|
792
|
+
};
|
793
|
+
|
644
794
|
// Fill in a given object with default properties.
|
645
795
|
_.defaults = function(obj) {
|
646
796
|
each(slice.call(arguments, 1), function(source) {
|
647
|
-
|
648
|
-
|
797
|
+
if (source) {
|
798
|
+
for (var prop in source) {
|
799
|
+
if (obj[prop] == null) obj[prop] = source[prop];
|
800
|
+
}
|
649
801
|
}
|
650
802
|
});
|
651
803
|
return obj;
|
@@ -665,19 +817,16 @@
|
|
665
817
|
return obj;
|
666
818
|
};
|
667
819
|
|
668
|
-
// Internal recursive comparison function
|
669
|
-
|
820
|
+
// Internal recursive comparison function for `isEqual`.
|
821
|
+
var eq = function(a, b, aStack, bStack) {
|
670
822
|
// Identical objects are equal. `0 === -0`, but they aren't identical.
|
671
823
|
// See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
|
672
824
|
if (a === b) return a !== 0 || 1 / a == 1 / b;
|
673
825
|
// A strict comparison is necessary because `null == undefined`.
|
674
826
|
if (a == null || b == null) return a === b;
|
675
827
|
// Unwrap any wrapped objects.
|
676
|
-
if (a
|
677
|
-
if (b
|
678
|
-
// Invoke a custom `isEqual` method if one is provided.
|
679
|
-
if (a.isEqual && _.isFunction(a.isEqual)) return a.isEqual(b);
|
680
|
-
if (b.isEqual && _.isFunction(b.isEqual)) return b.isEqual(a);
|
828
|
+
if (a instanceof _) a = a._wrapped;
|
829
|
+
if (b instanceof _) b = b._wrapped;
|
681
830
|
// Compare `[[Class]]` names.
|
682
831
|
var className = toString.call(a);
|
683
832
|
if (className != toString.call(b)) return false;
|
@@ -707,14 +856,15 @@
|
|
707
856
|
if (typeof a != 'object' || typeof b != 'object') return false;
|
708
857
|
// Assume equality for cyclic structures. The algorithm for detecting cyclic
|
709
858
|
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
|
710
|
-
var length =
|
859
|
+
var length = aStack.length;
|
711
860
|
while (length--) {
|
712
861
|
// Linear search. Performance is inversely proportional to the number of
|
713
862
|
// unique nested structures.
|
714
|
-
if (
|
863
|
+
if (aStack[length] == a) return bStack[length] == b;
|
715
864
|
}
|
716
865
|
// Add the first object to the stack of traversed objects.
|
717
|
-
|
866
|
+
aStack.push(a);
|
867
|
+
bStack.push(b);
|
718
868
|
var size = 0, result = true;
|
719
869
|
// Recursively compare objects and arrays.
|
720
870
|
if (className == '[object Array]') {
|
@@ -724,20 +874,24 @@
|
|
724
874
|
if (result) {
|
725
875
|
// Deep compare the contents, ignoring non-numeric properties.
|
726
876
|
while (size--) {
|
727
|
-
|
728
|
-
if (!(result = size in a == size in b && eq(a[size], b[size], stack))) break;
|
877
|
+
if (!(result = eq(a[size], b[size], aStack, bStack))) break;
|
729
878
|
}
|
730
879
|
}
|
731
880
|
} else {
|
732
|
-
// Objects with different constructors are not equivalent
|
733
|
-
|
881
|
+
// Objects with different constructors are not equivalent, but `Object`s
|
882
|
+
// from different frames are.
|
883
|
+
var aCtor = a.constructor, bCtor = b.constructor;
|
884
|
+
if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
|
885
|
+
_.isFunction(bCtor) && (bCtor instanceof bCtor))) {
|
886
|
+
return false;
|
887
|
+
}
|
734
888
|
// Deep compare objects.
|
735
889
|
for (var key in a) {
|
736
890
|
if (_.has(a, key)) {
|
737
891
|
// Count the expected number of properties.
|
738
892
|
size++;
|
739
893
|
// Deep compare each member.
|
740
|
-
if (!(result = _.has(b, key) && eq(a[key], b[key],
|
894
|
+
if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
|
741
895
|
}
|
742
896
|
}
|
743
897
|
// Ensure that both objects contain the same number of properties.
|
@@ -749,18 +903,20 @@
|
|
749
903
|
}
|
750
904
|
}
|
751
905
|
// Remove the first object from the stack of traversed objects.
|
752
|
-
|
906
|
+
aStack.pop();
|
907
|
+
bStack.pop();
|
753
908
|
return result;
|
754
|
-
}
|
909
|
+
};
|
755
910
|
|
756
911
|
// Perform a deep comparison to check if two objects are equal.
|
757
912
|
_.isEqual = function(a, b) {
|
758
|
-
return eq(a, b, []);
|
913
|
+
return eq(a, b, [], []);
|
759
914
|
};
|
760
915
|
|
761
916
|
// Is a given array, string, or object empty?
|
762
917
|
// An "empty" object has no enumerable own-properties.
|
763
918
|
_.isEmpty = function(obj) {
|
919
|
+
if (obj == null) return true;
|
764
920
|
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
|
765
921
|
for (var key in obj) if (_.has(obj, key)) return false;
|
766
922
|
return true;
|
@@ -768,7 +924,7 @@
|
|
768
924
|
|
769
925
|
// Is a given value a DOM element?
|
770
926
|
_.isElement = function(obj) {
|
771
|
-
return !!(obj && obj.nodeType
|
927
|
+
return !!(obj && obj.nodeType === 1);
|
772
928
|
};
|
773
929
|
|
774
930
|
// Is a given value an array?
|
@@ -782,35 +938,36 @@
|
|
782
938
|
return obj === Object(obj);
|
783
939
|
};
|
784
940
|
|
785
|
-
//
|
786
|
-
|
787
|
-
|
788
|
-
|
941
|
+
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
|
942
|
+
each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
|
943
|
+
_['is' + name] = function(obj) {
|
944
|
+
return toString.call(obj) == '[object ' + name + ']';
|
945
|
+
};
|
946
|
+
});
|
947
|
+
|
948
|
+
// Define a fallback version of the method in browsers (ahem, IE), where
|
949
|
+
// there isn't any inspectable "Arguments" type.
|
789
950
|
if (!_.isArguments(arguments)) {
|
790
951
|
_.isArguments = function(obj) {
|
791
952
|
return !!(obj && _.has(obj, 'callee'));
|
792
953
|
};
|
793
954
|
}
|
794
955
|
|
795
|
-
//
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
_.isString = function(obj) {
|
802
|
-
return toString.call(obj) == '[object String]';
|
803
|
-
};
|
956
|
+
// Optimize `isFunction` if appropriate.
|
957
|
+
if (typeof (/./) !== 'function') {
|
958
|
+
_.isFunction = function(obj) {
|
959
|
+
return typeof obj === 'function';
|
960
|
+
};
|
961
|
+
}
|
804
962
|
|
805
|
-
// Is a given
|
806
|
-
_.
|
807
|
-
return
|
963
|
+
// Is a given object a finite number?
|
964
|
+
_.isFinite = function(obj) {
|
965
|
+
return isFinite(obj) && !isNaN(parseFloat(obj));
|
808
966
|
};
|
809
967
|
|
810
|
-
// Is the given value `NaN`?
|
968
|
+
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
|
811
969
|
_.isNaN = function(obj) {
|
812
|
-
|
813
|
-
return obj !== obj;
|
970
|
+
return _.isNumber(obj) && obj != +obj;
|
814
971
|
};
|
815
972
|
|
816
973
|
// Is a given value a boolean?
|
@@ -818,16 +975,6 @@
|
|
818
975
|
return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
|
819
976
|
};
|
820
977
|
|
821
|
-
// Is a given value a date?
|
822
|
-
_.isDate = function(obj) {
|
823
|
-
return toString.call(obj) == '[object Date]';
|
824
|
-
};
|
825
|
-
|
826
|
-
// Is the given value a regular expression?
|
827
|
-
_.isRegExp = function(obj) {
|
828
|
-
return toString.call(obj) == '[object RegExp]';
|
829
|
-
};
|
830
|
-
|
831
978
|
// Is a given value equal to null?
|
832
979
|
_.isNull = function(obj) {
|
833
980
|
return obj === null;
|
@@ -838,7 +985,8 @@
|
|
838
985
|
return obj === void 0;
|
839
986
|
};
|
840
987
|
|
841
|
-
//
|
988
|
+
// Shortcut function for checking if an object has a given property directly
|
989
|
+
// on itself (in other words, not on a prototype).
|
842
990
|
_.has = function(obj, key) {
|
843
991
|
return hasOwnProperty.call(obj, key);
|
844
992
|
};
|
@@ -859,20 +1007,67 @@
|
|
859
1007
|
};
|
860
1008
|
|
861
1009
|
// Run a function **n** times.
|
862
|
-
_.times = function
|
863
|
-
|
1010
|
+
_.times = function(n, iterator, context) {
|
1011
|
+
var accum = Array(n);
|
1012
|
+
for (var i = 0; i < n; i++) accum[i] = iterator.call(context, i);
|
1013
|
+
return accum;
|
1014
|
+
};
|
1015
|
+
|
1016
|
+
// Return a random integer between min and max (inclusive).
|
1017
|
+
_.random = function(min, max) {
|
1018
|
+
if (max == null) {
|
1019
|
+
max = min;
|
1020
|
+
min = 0;
|
1021
|
+
}
|
1022
|
+
return min + (0 | Math.random() * (max - min + 1));
|
1023
|
+
};
|
1024
|
+
|
1025
|
+
// List of HTML entities for escaping.
|
1026
|
+
var entityMap = {
|
1027
|
+
escape: {
|
1028
|
+
'&': '&',
|
1029
|
+
'<': '<',
|
1030
|
+
'>': '>',
|
1031
|
+
'"': '"',
|
1032
|
+
"'": ''',
|
1033
|
+
'/': '/'
|
1034
|
+
}
|
1035
|
+
};
|
1036
|
+
entityMap.unescape = _.invert(entityMap.escape);
|
1037
|
+
|
1038
|
+
// Regexes containing the keys and values listed immediately above.
|
1039
|
+
var entityRegexes = {
|
1040
|
+
escape: new RegExp('[' + _.keys(entityMap.escape).join('') + ']', 'g'),
|
1041
|
+
unescape: new RegExp('(' + _.keys(entityMap.unescape).join('|') + ')', 'g')
|
864
1042
|
};
|
865
1043
|
|
866
|
-
//
|
867
|
-
_.escape
|
868
|
-
|
1044
|
+
// Functions for escaping and unescaping strings to/from HTML interpolation.
|
1045
|
+
_.each(['escape', 'unescape'], function(method) {
|
1046
|
+
_[method] = function(string) {
|
1047
|
+
if (string == null) return '';
|
1048
|
+
return ('' + string).replace(entityRegexes[method], function(match) {
|
1049
|
+
return entityMap[method][match];
|
1050
|
+
});
|
1051
|
+
};
|
1052
|
+
});
|
1053
|
+
|
1054
|
+
// If the value of the named property is a function then invoke it;
|
1055
|
+
// otherwise, return it.
|
1056
|
+
_.result = function(object, property) {
|
1057
|
+
if (object == null) return null;
|
1058
|
+
var value = object[property];
|
1059
|
+
return _.isFunction(value) ? value.call(object) : value;
|
869
1060
|
};
|
870
1061
|
|
871
|
-
// Add your own custom functions to the Underscore object
|
872
|
-
// they're correctly added to the OOP wrapper as well.
|
1062
|
+
// Add your own custom functions to the Underscore object.
|
873
1063
|
_.mixin = function(obj) {
|
874
1064
|
each(_.functions(obj), function(name){
|
875
|
-
|
1065
|
+
var func = _[name] = obj[name];
|
1066
|
+
_.prototype[name] = function() {
|
1067
|
+
var args = [this._wrapped];
|
1068
|
+
push.apply(args, arguments);
|
1069
|
+
return result.call(this, func.apply(_, args));
|
1070
|
+
};
|
876
1071
|
});
|
877
1072
|
};
|
878
1073
|
|
@@ -880,7 +1075,7 @@
|
|
880
1075
|
// Useful for temporary DOM ids.
|
881
1076
|
var idCounter = 0;
|
882
1077
|
_.uniqueId = function(prefix) {
|
883
|
-
var id = idCounter
|
1078
|
+
var id = '' + ++idCounter;
|
884
1079
|
return prefix ? prefix + id : id;
|
885
1080
|
};
|
886
1081
|
|
@@ -895,41 +1090,79 @@
|
|
895
1090
|
// When customizing `templateSettings`, if you don't want to define an
|
896
1091
|
// interpolation, evaluation or escaping regex, we need one that is
|
897
1092
|
// guaranteed not to match.
|
898
|
-
var noMatch =
|
1093
|
+
var noMatch = /(.)^/;
|
899
1094
|
|
900
|
-
//
|
901
|
-
//
|
902
|
-
var
|
903
|
-
|
1095
|
+
// Certain characters need to be escaped so that they can be put into a
|
1096
|
+
// string literal.
|
1097
|
+
var escapes = {
|
1098
|
+
"'": "'",
|
1099
|
+
'\\': '\\',
|
1100
|
+
'\r': 'r',
|
1101
|
+
'\n': 'n',
|
1102
|
+
'\t': 't',
|
1103
|
+
'\u2028': 'u2028',
|
1104
|
+
'\u2029': 'u2029'
|
904
1105
|
};
|
905
1106
|
|
1107
|
+
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
|
1108
|
+
|
906
1109
|
// JavaScript micro-templating, similar to John Resig's implementation.
|
907
1110
|
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
908
1111
|
// and correctly escapes quotes within interpolated code.
|
909
|
-
_.template = function(
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
1112
|
+
_.template = function(text, data, settings) {
|
1113
|
+
settings = _.defaults({}, settings, _.templateSettings);
|
1114
|
+
|
1115
|
+
// Combine delimiters into one regular expression via alternation.
|
1116
|
+
var matcher = new RegExp([
|
1117
|
+
(settings.escape || noMatch).source,
|
1118
|
+
(settings.interpolate || noMatch).source,
|
1119
|
+
(settings.evaluate || noMatch).source
|
1120
|
+
].join('|') + '|$', 'g');
|
1121
|
+
|
1122
|
+
// Compile the template source, escaping string literals appropriately.
|
1123
|
+
var index = 0;
|
1124
|
+
var source = "__p+='";
|
1125
|
+
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
|
1126
|
+
source += text.slice(index, offset)
|
1127
|
+
.replace(escaper, function(match) { return '\\' + escapes[match]; });
|
1128
|
+
|
1129
|
+
if (escape) {
|
1130
|
+
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
|
1131
|
+
}
|
1132
|
+
if (interpolate) {
|
1133
|
+
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
|
1134
|
+
}
|
1135
|
+
if (evaluate) {
|
1136
|
+
source += "';\n" + evaluate + "\n__p+='";
|
1137
|
+
}
|
1138
|
+
index = offset + match.length;
|
1139
|
+
return match;
|
1140
|
+
});
|
1141
|
+
source += "';\n";
|
1142
|
+
|
1143
|
+
// If a variable is not specified, place data values in local scope.
|
1144
|
+
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
|
1145
|
+
|
1146
|
+
source = "var __t,__p='',__j=Array.prototype.join," +
|
1147
|
+
"print=function(){__p+=__j.call(arguments,'');};\n" +
|
1148
|
+
source + "return __p;\n";
|
1149
|
+
|
1150
|
+
try {
|
1151
|
+
var render = new Function(settings.variable || 'obj', '_', source);
|
1152
|
+
} catch (e) {
|
1153
|
+
e.source = source;
|
1154
|
+
throw e;
|
1155
|
+
}
|
1156
|
+
|
1157
|
+
if (data) return render(data, _);
|
1158
|
+
var template = function(data) {
|
1159
|
+
return render.call(this, data, _);
|
932
1160
|
};
|
1161
|
+
|
1162
|
+
// Provide the compiled function source as a convenience for precompilation.
|
1163
|
+
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
|
1164
|
+
|
1165
|
+
return template;
|
933
1166
|
};
|
934
1167
|
|
935
1168
|
// Add a "chain" function, which will delegate to the wrapper.
|
@@ -937,29 +1170,15 @@
|
|
937
1170
|
return _(obj).chain();
|
938
1171
|
};
|
939
1172
|
|
940
|
-
//
|
1173
|
+
// OOP
|
941
1174
|
// ---------------
|
942
|
-
|
943
1175
|
// If Underscore is called as a function, it returns a wrapped object that
|
944
1176
|
// can be used OO-style. This wrapper holds altered versions of all the
|
945
1177
|
// underscore functions. Wrapped objects may be chained.
|
946
|
-
var wrapper = function(obj) { this._wrapped = obj; };
|
947
|
-
|
948
|
-
// Expose `wrapper.prototype` as `_.prototype`
|
949
|
-
_.prototype = wrapper.prototype;
|
950
1178
|
|
951
1179
|
// Helper function to continue chaining intermediate results.
|
952
|
-
var result = function(obj
|
953
|
-
return
|
954
|
-
};
|
955
|
-
|
956
|
-
// A method to easily add functions to the OOP wrapper.
|
957
|
-
var addToWrapper = function(name, func) {
|
958
|
-
wrapper.prototype[name] = function() {
|
959
|
-
var args = slice.call(arguments);
|
960
|
-
unshift.call(args, this._wrapped);
|
961
|
-
return result(func.apply(_, args), this._chain);
|
962
|
-
};
|
1180
|
+
var result = function(obj) {
|
1181
|
+
return this._chain ? _(obj).chain() : obj;
|
963
1182
|
};
|
964
1183
|
|
965
1184
|
// Add all of the Underscore functions to the wrapper object.
|
@@ -968,32 +1187,35 @@
|
|
968
1187
|
// Add all mutator Array functions to the wrapper.
|
969
1188
|
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
970
1189
|
var method = ArrayProto[name];
|
971
|
-
|
972
|
-
var
|
973
|
-
method.apply(
|
974
|
-
|
975
|
-
|
976
|
-
return result(wrapped, this._chain);
|
1190
|
+
_.prototype[name] = function() {
|
1191
|
+
var obj = this._wrapped;
|
1192
|
+
method.apply(obj, arguments);
|
1193
|
+
if ((name == 'shift' || name == 'splice') && obj.length === 0) delete obj[0];
|
1194
|
+
return result.call(this, obj);
|
977
1195
|
};
|
978
1196
|
});
|
979
1197
|
|
980
1198
|
// Add all accessor Array functions to the wrapper.
|
981
1199
|
each(['concat', 'join', 'slice'], function(name) {
|
982
1200
|
var method = ArrayProto[name];
|
983
|
-
|
984
|
-
return result(method.apply(this._wrapped, arguments)
|
1201
|
+
_.prototype[name] = function() {
|
1202
|
+
return result.call(this, method.apply(this._wrapped, arguments));
|
985
1203
|
};
|
986
1204
|
});
|
987
1205
|
|
988
|
-
|
989
|
-
wrapper.prototype.chain = function() {
|
990
|
-
this._chain = true;
|
991
|
-
return this;
|
992
|
-
};
|
1206
|
+
_.extend(_.prototype, {
|
993
1207
|
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
1208
|
+
// Start chaining a wrapped Underscore object.
|
1209
|
+
chain: function() {
|
1210
|
+
this._chain = true;
|
1211
|
+
return this;
|
1212
|
+
},
|
1213
|
+
|
1214
|
+
// Extracts the result from a wrapped and chained object.
|
1215
|
+
value: function() {
|
1216
|
+
return this._wrapped;
|
1217
|
+
}
|
1218
|
+
|
1219
|
+
});
|
998
1220
|
|
999
1221
|
}).call(this);
|