underscore-source 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
@@ -30,8 +30,11 @@
|
|
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
|
+
|
33
36
|
// Current version.
|
34
|
-
_.VERSION = '0.4.
|
37
|
+
_.VERSION = '0.4.7';
|
35
38
|
|
36
39
|
/*------------------------ Collection Functions: ---------------------------*/
|
37
40
|
|
@@ -43,11 +46,10 @@
|
|
43
46
|
if (obj.forEach) {
|
44
47
|
obj.forEach(iterator, context);
|
45
48
|
} else if (obj.length) {
|
46
|
-
for (var i=0, l
|
49
|
+
for (var i=0, l=obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
|
47
50
|
} else {
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
+
var keys = _.keys(obj), l = keys.length;
|
52
|
+
for (var i=0; i<l; i++) iterator.call(context, obj[keys[i]], keys[i], obj);
|
51
53
|
}
|
52
54
|
} catch(e) {
|
53
55
|
if (e != breaker) throw e;
|
@@ -379,10 +381,11 @@
|
|
379
381
|
_.compose = function() {
|
380
382
|
var funcs = _.toArray(arguments);
|
381
383
|
return function() {
|
384
|
+
var args = _.toArray(arguments);
|
382
385
|
for (var i=funcs.length-1; i >= 0; i--) {
|
383
|
-
|
386
|
+
args = [funcs[i].apply(this, args)];
|
384
387
|
}
|
385
|
-
return
|
388
|
+
return args[0];
|
386
389
|
};
|
387
390
|
};
|
388
391
|
|
@@ -390,7 +393,10 @@
|
|
390
393
|
|
391
394
|
// Retrieve the names of an object's properties.
|
392
395
|
_.keys = function(obj) {
|
393
|
-
|
396
|
+
if(_.isArray(obj)) return _.range(0, obj.length);
|
397
|
+
var keys = [];
|
398
|
+
for (var key in obj) if (objPro.hasOwnProperty.call(obj, key)) keys.push(key);
|
399
|
+
return keys;
|
394
400
|
};
|
395
401
|
|
396
402
|
// Retrieve the values of an object's properties.
|
@@ -419,12 +425,16 @@
|
|
419
425
|
if (atype != btype) return false;
|
420
426
|
// Basic equality test (watch out for coercions).
|
421
427
|
if (a == b) return true;
|
428
|
+
// Check dates' integer values.
|
429
|
+
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
422
430
|
// One of them implements an isEqual()?
|
423
431
|
if (a.isEqual) return a.isEqual(b);
|
424
432
|
// Both are NaN?
|
425
|
-
if (_.
|
433
|
+
if (_.isNaN(a) && _.isNaN(b)) return true;
|
426
434
|
// If a is not an object by this point, we can't handle it.
|
427
435
|
if (atype !== 'object') return false;
|
436
|
+
// Check for different array lengths before comparing contents.
|
437
|
+
if (!_.isUndefined(a.length) && a.length !== b.length) return false;
|
428
438
|
// Nothing else worked, deep compare the contents.
|
429
439
|
var aKeys = _.keys(a), bKeys = _.keys(b);
|
430
440
|
// Different object sizes?
|
@@ -436,7 +446,7 @@
|
|
436
446
|
|
437
447
|
// Is a given array or object empty?
|
438
448
|
_.isEmpty = function(obj) {
|
439
|
-
return
|
449
|
+
return _.keys(obj).length == 0;
|
440
450
|
};
|
441
451
|
|
442
452
|
// Is a given value a DOM element?
|
@@ -446,22 +456,38 @@
|
|
446
456
|
|
447
457
|
// Is a given value a real Array?
|
448
458
|
_.isArray = function(obj) {
|
449
|
-
return
|
459
|
+
return objPro.toString.call(obj) == '[object Array]';
|
450
460
|
};
|
451
461
|
|
452
462
|
// Is a given value a Function?
|
453
463
|
_.isFunction = function(obj) {
|
454
|
-
return
|
464
|
+
return objPro.toString.call(obj) == '[object Function]';
|
455
465
|
};
|
456
466
|
|
457
467
|
// Is a given value a String?
|
458
468
|
_.isString = function(obj) {
|
459
|
-
return
|
469
|
+
return objPro.toString.call(obj) == '[object String]';
|
460
470
|
};
|
461
471
|
|
462
472
|
// Is a given value a Number?
|
463
473
|
_.isNumber = function(obj) {
|
464
|
-
return
|
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
|
+
// Is the given value NaN -- this one is interesting. NaN != NaN, and
|
483
|
+
// isNaN(undefined) == true, so we make sure it's a number first.
|
484
|
+
_.isNaN = function(obj) {
|
485
|
+
return _.isNumber(obj) && isNaN(obj);
|
486
|
+
};
|
487
|
+
|
488
|
+
// Is a given value equal to null?
|
489
|
+
_.isNull = function(obj) {
|
490
|
+
return obj === null;
|
465
491
|
};
|
466
492
|
|
467
493
|
// Is a given variable undefined?
|
@@ -498,9 +524,7 @@
|
|
498
524
|
|
499
525
|
// Return a sorted list of the function names available in Underscore.
|
500
526
|
_.functions = function() {
|
501
|
-
|
502
|
-
for (var key in _) if (Object.prototype.hasOwnProperty.call(_, key)) functions.push(key);
|
503
|
-
return _.without(functions, 'VERSION', 'prototype', 'noConflict').sort();
|
527
|
+
return _.without(_.keys(_), 'VERSION', 'prototype', 'noConflict').sort();
|
504
528
|
};
|
505
529
|
|
506
530
|
// JavaScript templating a-la ERB, pilfered from John Resig's
|