underscore-source 0.4.7 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -30,11 +30,8 @@
|
|
30
30
|
// Export the Underscore object for CommonJS.
|
31
31
|
if (typeof exports !== 'undefined') exports._ = _;
|
32
32
|
|
33
|
-
// Maintain a reference to the Object prototype for quick access.
|
34
|
-
var objPro = Object.prototype;
|
35
|
-
|
36
33
|
// Current version.
|
37
|
-
_.VERSION = '0.
|
34
|
+
_.VERSION = '0.5.0';
|
38
35
|
|
39
36
|
/*------------------------ Collection Functions: ---------------------------*/
|
40
37
|
|
@@ -337,20 +334,20 @@
|
|
337
334
|
|
338
335
|
// Create a function bound to a given object (assigning 'this', and arguments,
|
339
336
|
// optionally). Binding with arguments is also known as 'curry'.
|
340
|
-
_.bind = function(func,
|
337
|
+
_.bind = function(func, obj) {
|
341
338
|
var args = _.rest(arguments, 2);
|
342
339
|
return function() {
|
343
|
-
return func.apply(
|
340
|
+
return func.apply(obj || root, args.concat(_.toArray(arguments)));
|
344
341
|
};
|
345
342
|
};
|
346
343
|
|
347
344
|
// Bind all of an object's methods to that object. Useful for ensuring that
|
348
345
|
// all callbacks defined on an object belong to it.
|
349
|
-
_.bindAll = function() {
|
350
|
-
var
|
351
|
-
_.
|
352
|
-
|
353
|
-
|
346
|
+
_.bindAll = function(obj) {
|
347
|
+
var funcs = _.rest(arguments);
|
348
|
+
if (funcs.length == 0) funcs = _.functions(obj);
|
349
|
+
_.each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
|
350
|
+
return obj;
|
354
351
|
};
|
355
352
|
|
356
353
|
// Delays a function for the given number of milliseconds, and then calls
|
@@ -395,7 +392,7 @@
|
|
395
392
|
_.keys = function(obj) {
|
396
393
|
if(_.isArray(obj)) return _.range(0, obj.length);
|
397
394
|
var keys = [];
|
398
|
-
for (var key in obj) if (
|
395
|
+
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) keys.push(key);
|
399
396
|
return keys;
|
400
397
|
};
|
401
398
|
|
@@ -425,16 +422,22 @@
|
|
425
422
|
if (atype != btype) return false;
|
426
423
|
// Basic equality test (watch out for coercions).
|
427
424
|
if (a == b) return true;
|
428
|
-
// Check dates' integer values.
|
429
|
-
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
430
425
|
// One of them implements an isEqual()?
|
431
426
|
if (a.isEqual) return a.isEqual(b);
|
427
|
+
// Check dates' integer values.
|
428
|
+
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
432
429
|
// Both are NaN?
|
433
430
|
if (_.isNaN(a) && _.isNaN(b)) return true;
|
431
|
+
// Compare regular expressions.
|
432
|
+
if (_.isRegExp(a) && _.isRegExp(b))
|
433
|
+
return a.source === b.source &&
|
434
|
+
a.global === b.global &&
|
435
|
+
a.ignoreCase === b.ignoreCase &&
|
436
|
+
a.multiline === b.multiline;
|
434
437
|
// If a is not an object by this point, we can't handle it.
|
435
438
|
if (atype !== 'object') return false;
|
436
439
|
// Check for different array lengths before comparing contents.
|
437
|
-
if (
|
440
|
+
if (a.length && (a.length !== b.length)) return false;
|
438
441
|
// Nothing else worked, deep compare the contents.
|
439
442
|
var aKeys = _.keys(a), bKeys = _.keys(b);
|
440
443
|
// Different object sizes?
|
@@ -454,31 +457,6 @@
|
|
454
457
|
return !!(obj && obj.nodeType == 1);
|
455
458
|
};
|
456
459
|
|
457
|
-
// Is a given value a real Array?
|
458
|
-
_.isArray = function(obj) {
|
459
|
-
return objPro.toString.call(obj) == '[object Array]';
|
460
|
-
};
|
461
|
-
|
462
|
-
// Is a given value a Function?
|
463
|
-
_.isFunction = function(obj) {
|
464
|
-
return objPro.toString.call(obj) == '[object Function]';
|
465
|
-
};
|
466
|
-
|
467
|
-
// Is a given value a String?
|
468
|
-
_.isString = function(obj) {
|
469
|
-
return objPro.toString.call(obj) == '[object String]';
|
470
|
-
};
|
471
|
-
|
472
|
-
// Is a given value a Number?
|
473
|
-
_.isNumber = function(obj) {
|
474
|
-
return objPro.toString.call(obj) == '[object Number]';
|
475
|
-
};
|
476
|
-
|
477
|
-
// Is a given value a Date?
|
478
|
-
_.isDate = function(obj) {
|
479
|
-
return objPro.toString.call(obj) == '[object Date]';
|
480
|
-
};
|
481
|
-
|
482
460
|
// Is the given value NaN -- this one is interesting. NaN != NaN, and
|
483
461
|
// isNaN(undefined) == true, so we make sure it's a number first.
|
484
462
|
_.isNaN = function(obj) {
|
@@ -495,6 +473,14 @@
|
|
495
473
|
return typeof obj == 'undefined';
|
496
474
|
};
|
497
475
|
|
476
|
+
// Define the isArray, isDate, isFunction, isNumber, isRegExp, and
|
477
|
+
// isString functions based on their toString identifiers.
|
478
|
+
_.each(['Array', 'Date', 'Function', 'Number', 'RegExp', 'String'], function(type) {
|
479
|
+
_['is' + type] = function(obj) {
|
480
|
+
return Object.prototype.toString.call(obj) == '[object ' + type + ']';
|
481
|
+
};
|
482
|
+
});
|
483
|
+
|
498
484
|
/* -------------------------- Utility Functions: -------------------------- */
|
499
485
|
|
500
486
|
// Run Underscore.js in noConflict mode, returning the '_' variable to its
|
@@ -523,8 +509,8 @@
|
|
523
509
|
};
|
524
510
|
|
525
511
|
// Return a sorted list of the function names available in Underscore.
|
526
|
-
_.functions = function() {
|
527
|
-
return _.
|
512
|
+
_.functions = function(obj) {
|
513
|
+
return _.select(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
528
514
|
};
|
529
515
|
|
530
516
|
// JavaScript templating a-la ERB, pilfered from John Resig's
|
@@ -565,7 +551,7 @@
|
|
565
551
|
};
|
566
552
|
|
567
553
|
// Add all of the Underscore functions to the wrapper object.
|
568
|
-
_.each(_.functions(), function(name) {
|
554
|
+
_.each(_.functions(_), function(name) {
|
569
555
|
wrapper.prototype[name] = function() {
|
570
556
|
Array.prototype.unshift.call(arguments, this._wrapped);
|
571
557
|
return result(_[name].apply(_, arguments), this._chain);
|