diff --git a/index.html b/index.html index 74f4b1662..ebbf82dbc 100644 --- a/index.html +++ b/index.html @@ -81,7 +81,7 @@

- Underscore provides 44-odd functions that support both the usual + Underscore provides 45-odd functions that support both the usual functional suspects: map, select, invoke — as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on. It delegates to built-in @@ -107,11 +107,11 @@

- + - +
Development Version (0.3.1)Development Version (0.3.2) 16kb, Uncompressed with Comments
Production Version (0.3.1)Production Version (0.3.2) 4kb, Packed and Gzipped
@@ -158,7 +158,8 @@ Utility
noConflict, - uniqueId, template + identity, uniqueId, + template

@@ -700,6 +701,19 @@ _.isUndefined(window.missingVariable);
 var underscore = _.noConflict();
+

+ identity_.identity(value) +
+ Returns the same value that is used as the argument. In math: + f(x) = x
+ This function looks useless, but is used throughout Underscore as + a default iterator. +

+
+var moe = {name : 'moe'};
+moe === _.identity(moe);
+=> true
+

uniqueId_.uniqueId([prefix])
@@ -736,6 +750,13 @@ _.template(list, {people : ['moe', 'curly', 'larry']});

Change Log

+

+ 0.3.2
+ Now runs on stock Rhino + interpreters with: load("underscore.js"). + Added identity as a utility function. +

+

0.3.1
All iterators are now passed in the original collection as their third diff --git a/underscore-min.js b/underscore-min.js index 56898bab1..ee3af5177 100644 --- a/underscore-min.js +++ b/underscore-min.js @@ -1 +1 @@ -(function(){var d=(typeof window==="undefined"&&typeof exports!=="undefined");var c=d?null:window._;var a=function(e){return e};var b={};d?b=exports:window._=b;b.VERSION="0.3.1";b.each=function(o,m,k){var g=0;try{if(o.forEach){o.forEach(m,k)}else{if(o.length){for(var j=0,f=o.length;j=e.computed&&(e={value:n,computed:k})});return e.value};b.min=function(h,g,f){if(!g&&b.isArray(h)){return Math.min.apply(Math,h)}var e={computed:Infinity};b.each(h,function(n,j,m){var k=g?g.call(f,n,j,m):n;kh?1:0}),"value")};b.sortedIndex=function(k,j,g){g=g||a;var e=0,h=k.length;while(e>1;g(k[f])=0})})};b.zip=function(){var e=b.toArray(arguments);var h=b.max(b.pluck(e,"length"));var g=new Array(h);for(var f=0;f=0;f--){arguments=[e[f].apply(this,arguments)]}return arguments[0]}};b.keys=function(e){return b.map(e,function(g,f){return f})};b.values=function(e){return b.map(e,a)};b.extend=function(e,g){for(var f in g){e[f]=g[f]}return e};b.clone=function(e){return b.extend({},e)};b.isEqual=function(f,e){if(f===e){return true}var j=typeof(f),m=typeof(e);if(j!=m){return false}if(f==e){return true}if(f.isEqual){return f.isEqual(e)}if(j!=="object"){return false}var g=b.keys(f),k=b.keys(e);if(g.length!=k.length){return false}for(var h in f){if(!b.isEqual(f[h],e[h])){return false}}return true};b.isElement=function(e){return !!(e&&e.nodeType==1)};b.isArray=function(e){return Object.prototype.toString.call(e)=="[object Array]"};b.isFunction=function(e){return Object.prototype.toString.call(e)=="[object Function]"};b.isUndefined=function(e){return typeof e=="undefined"};b.noConflict=function(){if(!d){window._=c}return this};b.uniqueId=function(e){var f=this._idCounter=(this._idCounter||0)+1;return e?e+f:f};b.template=function(g,f){var e=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+g.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');");return f?e(f):e};b.forEach=b.each;b.inject=b.reduce;b.filter=b.select;b.every=b.all;b.some=b.any})(); \ No newline at end of file +(function(){var a=this;var c=a._;var b=a._={};if(typeof exports!=="undefined"){b=exports}b.VERSION="0.3.2";b.each=function(n,k,j){var f=0;try{if(n.forEach){n.forEach(k,j)}else{if(n.length){for(var h=0,d=n.length;h=d.computed&&(d={value:m,computed:j})});return d.value};b.min=function(g,f,e){if(!f&&b.isArray(g)){return Math.min.apply(Math,g)}var d={computed:Infinity};b.each(g,function(m,h,k){var j=f?f.call(e,m,h,k):m;jg?1:0}),"value")};b.sortedIndex=function(j,h,f){f=f||b.identity;var d=0,g=j.length;while(d>1;f(j[e])=0})})};b.zip=function(){var d=b.toArray(arguments);var g=b.max(b.pluck(d,"length"));var f=new Array(g);for(var e=0;e=0;e--){arguments=[d[e].apply(this,arguments)]}return arguments[0]}};b.keys=function(d){return b.map(d,function(f,e){return e})};b.values=function(d){return b.map(d,b.identity)};b.extend=function(d,f){for(var e in f){d[e]=f[e]}return d};b.clone=function(d){return b.extend({},d)};b.isEqual=function(e,d){if(e===d){return true}var h=typeof(e),k=typeof(d);if(h!=k){return false}if(e==d){return true}if(e.isEqual){return e.isEqual(d)}if(h!=="object"){return false}var f=b.keys(e),j=b.keys(d);if(f.length!=j.length){return false}for(var g in e){if(!b.isEqual(e[g],d[g])){return false}}return true};b.isElement=function(d){return !!(d&&d.nodeType==1)};b.isArray=function(d){return Object.prototype.toString.call(d)=="[object Array]"};b.isFunction=function(d){return Object.prototype.toString.call(d)=="[object Function]"};b.isUndefined=function(d){return typeof d=="undefined"};b.noConflict=function(){a._=c;return this};b.identity=function(d){return d};b.uniqueId=function(d){var e=this._idCounter=(this._idCounter||0)+1;return d?d+e:e};b.template=function(f,e){var d=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+f.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');");return e?d(e):d};b.forEach=b.each;b.inject=b.reduce;b.filter=b.select;b.every=b.all;b.some=b.any})(); \ No newline at end of file diff --git a/underscore.js b/underscore.js index 45b0ddddb..fee281f8d 100644 --- a/underscore.js +++ b/underscore.js @@ -10,23 +10,20 @@ /*------------------------- Baseline setup ---------------------------------*/ - // Are we running in CommonJS or in the browser? - var commonJS = (typeof window === 'undefined' && typeof exports !== 'undefined'); + // Establish the root object, "window" in the browser, or "global" on the server. + var root = this; // Save the previous value of the "_" variable. - var previousUnderscore = commonJS ? null : window._; - - // Keep the identity function around for default iterators. - var identity = function(value) { return value; }; + var previousUnderscore = root._; // Create a safe reference to the Underscore object for the functions below. - var _ = {}; + var _ = root._ = {}; - // Export the Underscore object for CommonJS, assign it globally otherwise. - commonJS ? _ = exports : window._ = _; + // Export the Underscore object for CommonJS. + if (typeof exports !== 'undefined') _ = exports; // Current version. - _.VERSION = '0.3.1'; + _.VERSION = '0.3.2'; /*------------------------ Collection Functions: ---------------------------*/ @@ -107,7 +104,7 @@ // Determine whether all of the elements match a truth test. Delegate to // JavaScript 1.6's every(), if it is present. _.all = function(obj, iterator, context) { - iterator = iterator || identity; + iterator = iterator || _.identity; if (obj.every) return obj.every(iterator, context); var result = true; _.each(obj, function(value, index, list) { @@ -119,7 +116,7 @@ // Determine if at least one element in the object matches a truth test. Use // JavaScript 1.6's some(), if it exists. _.any = function(obj, iterator, context) { - iterator = iterator || identity; + iterator = iterator || _.identity; if (obj.some) return obj.some(iterator, context); var result = false; _.each(obj, function(value, index, list) { @@ -192,7 +189,7 @@ // Use a comparator function to figure out at what index an object should // be inserted so as to maintain order. Uses binary search. _.sortedIndex = function(array, obj, iterator) { - iterator = iterator || identity; + iterator = iterator || _.identity; var low = 0, high = array.length; while (low < high) { var mid = (low + high) >> 1; @@ -360,7 +357,7 @@ // Retrieve the values of an object's properties. _.values = function(obj) { - return _.map(obj, identity); + return _.map(obj, _.identity); }; // Extend a given object with all of the properties in a source object. @@ -421,10 +418,15 @@ // Run Underscore.js in noConflict mode, returning the '_' variable to its // previous owner. Returns a reference to the Underscore object. _.noConflict = function() { - if (!commonJS) window._ = previousUnderscore; + root._ = previousUnderscore; return this; }; + // Keep the identity function around for default iterators. + _.identity = function(value) { + return value; + }; + // Generate a unique integer id (unique within the entire client session). // Useful for temporary DOM ids. _.uniqueId = function(prefix) {