diff --git a/index.html b/index.html index 753942fcc..3502dde88 100644 --- a/index.html +++ b/index.html @@ -107,11 +107,11 @@

Downloads (Right-click, and u

- + - +
Development Version (0.4.1)Development Version (0.4.2) 18kb, Uncompressed with Comments
Production Version (0.4.1)Production Version (0.4.2) 2kb, Packed and Gzipped
@@ -133,7 +133,7 @@

Object-Oriented and Functional Styles

Using the object-oriented style allows you to chain together methods. Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well. When you've finished the computation, - use get to retrieve the final value. Here's an example of chaining + use value to retrieve the final value. Here's an example of chaining together a map/flatten/reduce, in order to get the word count of every word in a song.

@@ -152,7 +152,7 @@

Object-Oriented and Functional Styles

.reduce({}, function(counts, word) { counts[word] = (counts[word] || 0) + 1; return counts; -}).get(); +}).value(); => {lumberjack : 2, all : 4, night : 2 ... } @@ -217,7 +217,7 @@

Table of Contents

Chaining
- chain, get + chain, value

@@ -864,7 +864,7 @@

Chaining

chain_(obj).chain()
Returns a wrapped object. Calling methods on this object will continue - to return wrapped objects until get is used. ( + to return wrapped objects until value is used. ( A more realistic example.)

@@ -873,22 +873,27 @@ 

Chaining

.sortBy(function(stooge){ return stooge.age; }) .map(function(stooge){ return stooge.name + ' is ' + stooge.age; }) .first() - .get(); + .value(); => "moe is 21"
-

- get_(obj).get() +

+ value_(obj).value()
Extracts the value of a wrapped object.

-_([1, 2, 3]).get();
+_([1, 2, 3]).value();
 => [1, 2, 3]
 

Change Log

+

+ 0.4.2
+ Renamed the unwrapping function to value, for clarity. +

+

0.4.1
Chained Underscore objects now support the Array prototype methods, so diff --git a/test/chaining.js b/test/chaining.js index dc6cef6a4..55f0c8c0d 100644 --- a/test/chaining.js +++ b/test/chaining.js @@ -16,7 +16,7 @@ $(document).ready(function() { hash[l] = hash[l] || 0; hash[l]++; return hash; - }).get(); + }).value(); ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song'); }); @@ -28,7 +28,7 @@ $(document).ready(function() { return n % 4 == 0; }).sortBy(function(n) { return -n; - }).get(); + }).value(); equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers"); }); @@ -40,7 +40,7 @@ $(document).ready(function() { .unshift(17) .pop() .map(function(n){ return n * 2; }) - .get(); + .value(); equals(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.'); }); diff --git a/test/speed.js b/test/speed.js index ce4f878d6..8313b1d56 100644 --- a/test/speed.js +++ b/test/speed.js @@ -1,54 +1,64 @@ (function() { - + var numbers = []; for (var i=0; i<1000; i++) numbers.push(i); var objects = _.map(numbers, function(n){ return {num : n}; }); var randomized = _.sortBy(numbers, function(){ return Math.random(); }); - + JSLitmus.test('_.each()', function() { var timesTwo = []; _.each(numbers, function(num){ timesTwo.push(num * 2); }); return timesTwo; }); - + JSLitmus.test('_(list).each()', function() { var timesTwo = []; _(numbers).each(function(num){ timesTwo.push(num * 2); }); return timesTwo; }); - + + JSLitmus.test('jQuery.each()', function() { + var timesTwo = []; + jQuery.each(numbers, function(){ timesTwo.push(this * 2); }); + return timesTwo; + }); + JSLitmus.test('_.map()', function() { return _.map(objects, function(obj){ return obj.num; }); }); - + + JSLitmus.test('jQuery.map()', function() { + return jQuery.map(objects, function(obj){ return obj.num; }); + }); + JSLitmus.test('_.pluck()', function() { return _.pluck(objects, 'num'); }); - + JSLitmus.test('_.uniq()', function() { return _.uniq(randomized); }); - + JSLitmus.test('_.uniq() (sorted)', function() { return _.uniq(numbers, true); }); - + JSLitmus.test('_.sortBy()', function() { return _.sortBy(numbers, function(num){ return -num; }); }); - + JSLitmus.test('_.isEqual()', function() { return _.isEqual(numbers, randomized); }); - + JSLitmus.test('_.keys()', function() { return _.keys(objects); }); - + JSLitmus.test('_.values()', function() { return _.values(objects); }); - + JSLitmus.test('_.intersect()', function() { return _.intersect(numbers, randomized); }); diff --git a/underscore-min.js b/underscore-min.js index e0fd42d76..8ec85c2f2 100644 --- a/underscore-min.js +++ b/underscore-min.js @@ -1 +1 @@ -(function(){var c=this;var e=c._;var f=function(g){this._wrapped=g};var d=c._=function(g){return new f(g)};if(typeof exports!=="undefined"){d=exports}d.VERSION="0.4.1";d.each=function(p,n,m){var h=0;try{if(p.forEach){p.forEach(n,m)}else{if(p.length){for(var k=0,g=p.length;k=g.computed&&(g={value:p,computed:n})});return g.value};d.min=function(k,j,h){if(!j&&d.isArray(k)){return Math.min.apply(Math,k)}var g={computed:Infinity};d.each(k,function(p,m,o){var n=j?j.call(h,p,m,o):p;nk?1:0}),"value")};d.sortedIndex=function(n,m,j){j=j||d.identity;var g=0,k=n.length;while(g>1;j(n[h])=0})})};d.zip=function(){var g=d.toArray(arguments);var k=d.max(d.pluck(g,"length"));var j=new Array(k);for(var h=0;h=0;h--){arguments=[g[h].apply(this,arguments)]}return arguments[0]}};d.keys=function(g){return d.map(g,function(j,h){return h})};d.values=function(g){return d.map(g,d.identity)};d.extend=function(g,j){for(var h in j){g[h]=j[h]}return g};d.clone=function(g){if(d.isArray(g)){return g.slice(0)}return d.extend({},g)};d.isEqual=function(h,g){if(h===g){return true}var m=typeof(h),o=typeof(g);if(m!=o){return false}if(h==g){return true}if(h.isEqual){return h.isEqual(g)}if(m!=="object"){return false}var j=d.keys(h),n=d.keys(g);if(j.length!=n.length){return false}for(var k in h){if(!d.isEqual(h[k],g[k])){return false}}return true};d.isEmpty=function(g){return(d.isArray(g)?g:d.values(g)).length==0};d.isElement=function(g){return !!(g&&g.nodeType==1)};d.isArray=function(g){return Object.prototype.toString.call(g)=="[object Array]"};d.isFunction=function(g){return Object.prototype.toString.call(g)=="[object Function]"};d.isUndefined=function(g){return typeof g=="undefined"};d.noConflict=function(){c._=e;return this};d.identity=function(g){return g};d.breakLoop=function(){throw"__break__"};var b=0;d.uniqueId=function(g){var h=b++;return g?g+h:h};d.functions=function(){var h=[];for(var g in d){if(Object.prototype.hasOwnProperty.call(d,g)){h.push(g)}}return d.without(h,"VERSION","prototype","noConflict").sort()};d.template=function(j,h){var g=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+j.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 h?g(h):g};d.forEach=d.each;d.foldl=d.inject=d.reduce;d.foldr=d.reduceRight;d.filter=d.select;d.every=d.all;d.some=d.any;d.methods=d.functions;var a=function(h,g){return g?d(h).chain():h};d.each(d.functions(),function(g){f.prototype[g]=function(){Array.prototype.unshift.call(arguments,this._wrapped);return a(d[g].apply(d,arguments),this._chain)}});d.each(["pop","push","reverse","shift","sort","splice","unshift"],function(g){f.prototype[g]=function(){Array.prototype[g].apply(this._wrapped,arguments);return a(this._wrapped,this._chain)}});d.each(["concat","join","slice"],function(g){f.prototype[g]=function(){return a(Array.prototype[g].apply(this._wrapped,arguments),this._chain)}});f.prototype.chain=function(){this._chain=true;return this};f.prototype.get=function(){return this._wrapped}})(); \ No newline at end of file +(function(){var c=this;var e=c._;var f=function(g){this._wrapped=g};var d=c._=function(g){return new f(g)};if(typeof exports!=="undefined"){d=exports}d.VERSION="0.4.2";d.each=function(p,n,m){var h=0;try{if(p.forEach){p.forEach(n,m)}else{if(p.length){for(var k=0,g=p.length;k=g.computed&&(g={value:p,computed:n})});return g.value};d.min=function(k,j,h){if(!j&&d.isArray(k)){return Math.min.apply(Math,k)}var g={computed:Infinity};d.each(k,function(p,m,o){var n=j?j.call(h,p,m,o):p;nk?1:0}),"value")};d.sortedIndex=function(n,m,j){j=j||d.identity;var g=0,k=n.length;while(g>1;j(n[h])=0})})};d.zip=function(){var g=d.toArray(arguments);var k=d.max(d.pluck(g,"length"));var j=new Array(k);for(var h=0;h=0;h--){arguments=[g[h].apply(this,arguments)]}return arguments[0]}};d.keys=function(g){return d.map(g,function(j,h){return h})};d.values=function(g){return d.map(g,d.identity)};d.extend=function(g,j){for(var h in j){g[h]=j[h]}return g};d.clone=function(g){if(d.isArray(g)){return g.slice(0)}return d.extend({},g)};d.isEqual=function(h,g){if(h===g){return true}var m=typeof(h),o=typeof(g);if(m!=o){return false}if(h==g){return true}if(h.isEqual){return h.isEqual(g)}if(m!=="object"){return false}var j=d.keys(h),n=d.keys(g);if(j.length!=n.length){return false}for(var k in h){if(!d.isEqual(h[k],g[k])){return false}}return true};d.isEmpty=function(g){return(d.isArray(g)?g:d.values(g)).length==0};d.isElement=function(g){return !!(g&&g.nodeType==1)};d.isArray=function(g){return Object.prototype.toString.call(g)=="[object Array]"};d.isFunction=function(g){return Object.prototype.toString.call(g)=="[object Function]"};d.isUndefined=function(g){return typeof g=="undefined"};d.noConflict=function(){c._=e;return this};d.identity=function(g){return g};d.breakLoop=function(){throw"__break__"};var b=0;d.uniqueId=function(g){var h=b++;return g?g+h:h};d.functions=function(){var h=[];for(var g in d){if(Object.prototype.hasOwnProperty.call(d,g)){h.push(g)}}return d.without(h,"VERSION","prototype","noConflict").sort()};d.template=function(j,h){var g=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+j.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 h?g(h):g};d.forEach=d.each;d.foldl=d.inject=d.reduce;d.foldr=d.reduceRight;d.filter=d.select;d.every=d.all;d.some=d.any;d.methods=d.functions;var a=function(h,g){return g?d(h).chain():h};d.each(d.functions(),function(g){f.prototype[g]=function(){Array.prototype.unshift.call(arguments,this._wrapped);return a(d[g].apply(d,arguments),this._chain)}});d.each(["pop","push","reverse","shift","sort","splice","unshift"],function(g){f.prototype[g]=function(){Array.prototype[g].apply(this._wrapped,arguments);return a(this._wrapped,this._chain)}});d.each(["concat","join","slice"],function(g){f.prototype[g]=function(){return a(Array.prototype[g].apply(this._wrapped,arguments),this._chain)}});f.prototype.chain=function(){this._chain=true;return this};f.prototype.value=function(){return this._wrapped}})(); \ No newline at end of file diff --git a/underscore.js b/underscore.js index c0fedb145..3dbe9bb9c 100644 --- a/underscore.js +++ b/underscore.js @@ -28,7 +28,7 @@ if (typeof exports !== 'undefined') _ = exports; // Current version. - _.VERSION = '0.4.1'; + _.VERSION = '0.4.2'; /*------------------------ Collection Functions: ---------------------------*/ @@ -531,7 +531,7 @@ }; // Extracts the result from a wrapped and chained object. - wrapper.prototype.get = function() { + wrapper.prototype.value = function() { return this._wrapped; };