Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80669 views
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
9
(function() {
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.
18
var previousUnderscore = root._;
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;
90
}
91
}
92
}
93
};
94
95
// Return the results of applying the iterator to each element.
96
// Delegates to **ECMAScript 5**'s native `map` if available.
97
_.map = function(obj, iterator, context) {
98
var results = [];
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);
103
});
104
if (obj.length === +obj.length) results.length = obj.length;
105
return results;
106
};
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
}
124
});
125
if (!initial) throw new TypeError('Reduce of empty array with no initial value');
126
return memo;
127
};
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) {
145
var result;
146
any(obj, function(value, index, list) {
147
if (iterator.call(context, value, index, list)) {
148
result = value;
149
return true;
150
}
151
});
152
return result;
153
};
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) {
159
var results = [];
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;
164
});
165
return results;
166
};
167
168
// Return all the elements for which a truth test fails.
169
_.reject = function(obj, iterator, context) {
170
var results = [];
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;
174
});
175
return results;
176
};
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) {
182
var result = true;
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;
187
});
188
return result;
189
};
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);
196
var result = false;
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;
201
});
202
return !!result;
203
};
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) {
208
var found = false;
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;
213
});
214
return found;
215
};
216
217
// Invoke a method (with arguments) on every item in a collection.
218
_.invoke = function(obj, method) {
219
var args = slice.call(arguments, 2);
220
return _.map(obj, function(value) {
221
return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
222
});
223
};
224
225
// Convenience version of a common use case of `map`: fetching a property.
226
_.pluck = function(obj, key) {
227
return _.map(obj, function(value){ return value[key]; });
228
};
229
230
// Return the maximum element or (element-based computation).
231
_.max = function(obj, iterator, context) {
232
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
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});
238
});
239
return result.value;
240
};
241
242
// Return the minimum element (or element-based computation).
243
_.min = function(obj, iterator, context) {
244
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
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});
250
});
251
return result.value;
252
};
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.
270
_.sortBy = function(obj, iterator, context) {
271
return _.pluck(_.map(obj, function(value, index, list) {
272
return {
273
value : value,
274
criteria : iterator.call(context, value, index, list)
275
};
276
}).sort(function(left, right) {
277
var a = left.criteria, b = right.criteria;
278
return a < b ? -1 : a > b ? 1 : 0;
279
}), 'value');
280
};
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
294
// Use a comparator function to figure out at what index an object should
295
// be inserted so as to maintain order. Uses binary search.
296
_.sortedIndex = function(array, obj, iterator) {
297
iterator || (iterator = _.identity);
298
var low = 0, high = array.length;
299
while (low < high) {
300
var mid = (low + high) >> 1;
301
iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
302
}
303
return low;
304
};
305
306
// Safely convert anything iterable into a real, live array.
307
_.toArray = function(iterable) {
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);
313
};
314
315
// Return the number of elements in an object.
316
_.size = function(obj) {
317
return _.toArray(obj).length;
318
};
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
}
346
};
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);
354
};
355
356
// Trim out all falsy values from an array.
357
_.compact = function(array) {
358
return _.filter(array, function(value){ return !!value; });
359
};
360
361
// Return a completely flattened version of an array.
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;
366
return memo;
367
}, []);
368
};
369
370
// Return a version of the array that does not contain the specified value(s).
371
_.without = function(array) {
372
return _.difference(array, slice.call(arguments, 1));
373
};
374
375
// Produce a duplicate-free version of the array. If the array has already
376
// been sorted, you have the option of using a faster algorithm.
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
}
386
return memo;
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));
395
};
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) {
403
return _.indexOf(other, item) >= 0;
404
});
405
});
406
};
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
415
// Zip together multiple lists into a single array -- elements that share
416
// an index go together.
417
_.zip = function() {
418
var args = slice.call(arguments);
419
var length = _.max(_.pluck(args, 'length'));
420
var results = new Array(length);
421
for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
422
return results;
423
};
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
427
// item in an array, or -1 if the item is not included in the array.
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;
440
return -1;
441
};
442
443
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
444
_.lastIndexOf = function(array, item) {
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;
449
return -1;
450
};
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;
496
};
497
};
498
499
// Bind all of an object's methods to that object. Useful for ensuring that
500
// all callbacks defined on an object belong to it.
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;
506
};
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
518
// Delays a function for the given number of milliseconds, and then calls
519
// it with the arguments supplied.
520
_.delay = function(func, wait) {
521
var args = slice.call(arguments, 2);
522
return setTimeout(function(){ return func.apply(func, args); }, wait);
523
};
524
525
// Defers a function, scheduling it to run after the current call stack has
526
// cleared.
527
_.defer = function(func) {
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
};
579
};
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
583
// conditionally execute the original function.
584
_.wrap = function(func, wrapper) {
585
return function() {
586
var args = [func].concat(slice.call(arguments, 0));
587
return wrapper.apply(this, args);
588
};
589
};
590
591
// Returns a function that is the composition of a list of functions, each
592
// consuming the return value of the function that follows.
593
_.compose = function() {
594
var funcs = arguments;
595
return function() {
596
var args = arguments;
597
for (var i = funcs.length - 1; i >= 0; i--) {
598
args = [funcs[i].apply(this, args)];
599
}
600
return args[0];
601
};
602
};
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
615
// Retrieve the names of an object's properties.
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;
622
};
623
624
// Retrieve the values of an object's properties.
625
_.values = function(obj) {
626
return _.map(obj, _.identity);
627
};
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;
657
};
658
659
// Create a (shallow-cloned) duplicate of an object.
660
_.clone = function(obj) {
661
if (!_.isObject(obj)) return obj;
662
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
663
};
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
761
// Perform a deep comparison to check if two objects are equal.
762
_.isEqual = function(a, b) {
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;
771
return true;
772
};
773
774
// Is a given value a DOM element?
775
_.isElement = function(obj) {
776
return !!(obj && obj.nodeType == 1);
777
};
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]';
783
};
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?
801
_.isFunction = function(obj) {
802
return toString.call(obj) == '[object Function]';
803
};
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
841
// Is a given variable undefined?
842
_.isUndefined = function(obj) {
843
return obj === void 0;
844
};
845
846
// Utility Functions
847
// -----------------
848
849
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
850
// previous owner. Returns a reference to the Underscore object.
851
_.noConflict = function() {
852
root._ = previousUnderscore;
853
return this;
854
};
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
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
879
// Generate a unique integer id (unique within the entire client session).
880
// Useful for temporary DOM ids.
881
var idCounter = 0;
882
_.uniqueId = function(prefix) {
883
var id = idCounter++;
884
return prefix ? prefix + id : id;
885
};
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.
903
_.template = function(str, data) {
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);
996
997