underscore-source 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -16,15 +16,20 @@
|
|
16
16
|
// Save the previous value of the "_" variable.
|
17
17
|
var previousUnderscore = root._;
|
18
18
|
|
19
|
-
//
|
20
|
-
|
19
|
+
// If Underscore is called as a function, it returns a wrapped object that
|
20
|
+
// can be used OO-style. This wrapper holds altered versions of all the
|
21
|
+
// underscore functions. Wrapped objects may be chained.
|
22
|
+
var wrapper = function(obj) { this._wrapped = obj; };
|
23
|
+
|
24
|
+
// Create a safe reference to the Underscore object for reference below.
|
25
|
+
var _ = root._ = function(obj) { return new wrapper(obj); };
|
21
26
|
|
22
27
|
// Export the Underscore object for CommonJS.
|
23
28
|
if (typeof exports !== 'undefined') _ = exports;
|
24
29
|
|
25
30
|
// Current version.
|
26
|
-
_.VERSION = '0.
|
27
|
-
|
31
|
+
_.VERSION = '0.4.0';
|
32
|
+
|
28
33
|
/*------------------------ Collection Functions: ---------------------------*/
|
29
34
|
|
30
35
|
// The cornerstone, an each implementation.
|
@@ -442,11 +447,19 @@
|
|
442
447
|
|
443
448
|
// Generate a unique integer id (unique within the entire client session).
|
444
449
|
// Useful for temporary DOM ids.
|
450
|
+
var idCounter = 0;
|
445
451
|
_.uniqueId = function(prefix) {
|
446
|
-
var id =
|
452
|
+
var id = idCounter++;
|
447
453
|
return prefix ? prefix + id : id;
|
448
454
|
};
|
449
455
|
|
456
|
+
// Return a sorted list of the function names available in Underscore.
|
457
|
+
_.functions = function() {
|
458
|
+
var functions = [];
|
459
|
+
for (var key in _) if (Object.prototype.hasOwnProperty.call(_, key)) functions.push(key);
|
460
|
+
return _.without(functions, 'VERSION', 'prototype', 'noConflict').sort();
|
461
|
+
};
|
462
|
+
|
450
463
|
// JavaScript templating a-la ERB, pilfered from John Resig's
|
451
464
|
// "Secrets of the JavaScript Ninja", page 83.
|
452
465
|
_.template = function(str, data) {
|
@@ -472,6 +485,29 @@
|
|
472
485
|
_.foldr = _.reduceRight;
|
473
486
|
_.filter = _.select;
|
474
487
|
_.every = _.all;
|
475
|
-
_.some = _.any;
|
488
|
+
_.some = _.any;
|
489
|
+
_.methods = _.functions;
|
490
|
+
|
491
|
+
/*------------------------ Setup the OOP Wrapper: --------------------------*/
|
492
|
+
|
493
|
+
// Add all of the Underscore functions to the wrapper object.
|
494
|
+
_.each(_.functions(), function(name) {
|
495
|
+
wrapper.prototype[name] = function() {
|
496
|
+
Array.prototype.unshift.call(arguments, this._wrapped);
|
497
|
+
var result = _[name].apply(_, arguments);
|
498
|
+
return this._chain ? _(result).chain() : result;
|
499
|
+
};
|
500
|
+
});
|
501
|
+
|
502
|
+
// Start chaining a wrapped Underscore object.
|
503
|
+
wrapper.prototype.chain = function() {
|
504
|
+
this._chain = true;
|
505
|
+
return this;
|
506
|
+
};
|
507
|
+
|
508
|
+
// Extracts the result from a wrapped and chained object.
|
509
|
+
wrapper.prototype.get = function() {
|
510
|
+
return this._wrapped;
|
511
|
+
};
|
476
512
|
|
477
513
|
})();
|