Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: jashkenas/underscore
base: 0.4.1
...
head fork: jashkenas/underscore
compare: 0.4.2
  • 1 commit
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 09, 2009
Jeremy Ashkenas version 0.4.2 -- quick patch to rename get() to value() for clarity, …
…and adding jQuery comparisons in the speed tests
f6e67a5
25 index.html
View
@@ -107,11 +107,11 @@
<p>
<table>
<tr>
- <td><a href="underscore.js">Development Version (0.4.1)</a></td>
+ <td><a href="underscore.js">Development Version (0.4.2)</a></td>
<td><i>18kb, Uncompressed with Comments</i></td>
</tr>
<tr>
- <td><a href="underscore-min.js">Production Version (0.4.1)</a></td>
+ <td><a href="underscore-min.js">Production Version (0.4.2)</a></td>
<td><i>2kb, Packed and Gzipped</i></td>
</tr>
</table>
@@ -133,7 +133,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
Using the object-oriented style allows you to chain together methods. Calling
<tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation,
- use <tt>get</tt> to retrieve the final value. Here's an example of chaining
+ use <tt>value</tt> to retrieve the final value. Here's an example of chaining
together a <b>map/flatten/reduce</b>, in order to get the word count of
every word in a song.
</p>
@@ -152,7 +152,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
.reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
-}).get();
+}).value();
=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
@@ -217,7 +217,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
<p>
<b>Chaining</b>
<br />
- <span class="methods"><a href="#chain">chain</a>, <a href="#get">get</a>
+ <span class="methods"><a href="#chain">chain</a>, <a href="#value">value</a>
</p>
<div id="documentation">
@@ -864,7 +864,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
<b class="header">chain</b><code>_(obj).chain()</code>
<br />
Returns a wrapped object. Calling methods on this object will continue
- to return wrapped objects until <tt>get</tt> is used. (
+ to return wrapped objects until <tt>value</tt> is used. (
<a href="#styles">A more realistic example.</a>)
</p>
<pre>
@@ -873,23 +873,28 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
.sortBy(function(stooge){ return stooge.age; })
.map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
.first()
- .get();
+ .value();
=&gt; "moe is 21"
</pre>
- <p id="get">
- <b class="header">get</b><code>_(obj).get()</code>
+ <p id="value">
+ <b class="header">value</b><code>_(obj).value()</code>
<br />
Extracts the value of a wrapped object.
</p>
<pre>
-_([1, 2, 3]).get();
+_([1, 2, 3]).value();
=&gt; [1, 2, 3]
</pre>
<h2>Change Log</h2>
<p>
+ <b class="header">0.4.2</b><br />
+ Renamed the unwrapping function to <tt>value</tt>, for clarity.
+ </p>
+
+ <p>
<b class="header">0.4.1</b><br />
Chained Underscore objects now support the Array prototype methods, so
that you can perform the full range of operations on a wrapped array
6 test/chaining.js
View
@@ -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.');
});
34 test/speed.js
View
@@ -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);
});
2  underscore-min.js
View
@@ -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;k++){n.call(m,p[k],k,p)}}else{for(var j in p){if(Object.prototype.hasOwnProperty.call(p,j)){n.call(m,p[j],j,p)}}}}}catch(o){if(o!="__break__"){throw o}}return p};d.map=function(k,j,h){if(k&&k.map){return k.map(j,h)}var g=[];d.each(k,function(o,m,n){g.push(j.call(h,o,m,n))});return g};d.reduce=function(k,g,j,h){if(k&&k.reduce){return k.reduce(d.bind(j,h),g)}d.each(k,function(o,m,n){g=j.call(h,g,o,m,n)});return g};d.reduceRight=function(k,g,j,h){if(k&&k.reduceRight){return k.reduceRight(d.bind(j,h),g)}var m=d.clone(d.toArray(k)).reverse();d.each(m,function(o,n){g=j.call(h,g,o,n,k)});return g};d.detect=function(k,j,h){var g;d.each(k,function(o,m,n){if(j.call(h,o,m,n)){g=o;d.breakLoop()}});return g};d.select=function(k,j,h){if(k.filter){return k.filter(j,h)}var g=[];d.each(k,function(o,m,n){j.call(h,o,m,n)&&g.push(o)});return g};d.reject=function(k,j,h){var g=[];d.each(k,function(o,m,n){!j.call(h,o,m,n)&&g.push(o)});return g};d.all=function(k,j,h){j=j||d.identity;if(k.every){return k.every(j,h)}var g=true;d.each(k,function(o,m,n){if(!(g=g&&j.call(h,o,m,n))){d.breakLoop()}});return g};d.any=function(k,j,h){j=j||d.identity;if(k.some){return k.some(j,h)}var g=false;d.each(k,function(o,m,n){if(g=j.call(h,o,m,n)){d.breakLoop()}});return g};d.include=function(j,h){if(d.isArray(j)){return d.indexOf(j,h)!=-1}var g=false;d.each(j,function(k){if(g=k===h){d.breakLoop()}});return g};d.invoke=function(h,j){var g=d.toArray(arguments).slice(2);return d.map(h,function(k){return(j?k[j]:k).apply(k,g)})};d.pluck=function(h,g){return d.map(h,function(j){return j[g]})};d.max=function(k,j,h){if(!j&&d.isArray(k)){return Math.max.apply(Math,k)}var g={computed:-Infinity};d.each(k,function(p,m,o){var n=j?j.call(h,p,m,o):p;n>=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;n<g.computed&&(g={value:p,computed:n})});return g.value};d.sortBy=function(j,h,g){return d.pluck(d.map(j,function(n,k,m){return{value:n,criteria:h.call(g,n,k,m)}}).sort(function(o,n){var m=o.criteria,k=n.criteria;return m<k?-1:m>k?1:0}),"value")};d.sortedIndex=function(n,m,j){j=j||d.identity;var g=0,k=n.length;while(g<k){var h=(g+k)>>1;j(n[h])<j(m)?g=h+1:k=h}return g};d.toArray=function(g){if(!g){return[]}if(d.isArray(g)){return g}return d.map(g,function(h){return h})};d.size=function(g){return d.toArray(g).length};d.first=function(g){return g[0]};d.last=function(g){return g[g.length-1]};d.compact=function(g){return d.select(g,function(h){return !!h})};d.flatten=function(g){return d.reduce(g,[],function(h,j){if(d.isArray(j)){return h.concat(d.flatten(j))}h.push(j);return h})};d.without=function(h){var g=h.slice.call(arguments,0);return d.select(h,function(j){return !d.include(g,j)})};d.uniq=function(h,g){return d.reduce(h,[],function(j,m,k){if(0==k||(g?d.last(j)!=m:!d.include(j,m))){j.push(m)}return j})};d.intersect=function(h){var g=d.toArray(arguments).slice(1);return d.select(d.uniq(h),function(j){return d.all(g,function(k){return d.indexOf(k,j)>=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<k;h++){j[h]=d.pluck(g,String(h))}return j};d.indexOf=function(h,g){if(h.indexOf){return h.indexOf(g)}for(i=0,l=h.length;i<l;i++){if(h[i]===g){return i}}return -1};d.lastIndexOf=function(j,h){if(j.lastIndexOf){return j.lastIndexOf(h)}var g=j.length;while(g--){if(j[g]===h){return g}}return -1};d.bind=function(j,h){h=h||c;var g=d.toArray(arguments).slice(2);return function(){var k=g.concat(d.toArray(arguments));return j.apply(h,k)}};d.bindAll=function(){var g=d.toArray(arguments);var h=g.pop();d.each(g,function(j){h[j]=d.bind(h[j],h)})};d.delay=function(h,j){var g=d.toArray(arguments).slice(2);return setTimeout(function(){return h.apply(h,g)},j)};d.defer=function(g){return d.delay.apply(d,[g,1].concat(d.toArray(arguments).slice(1)))};d.wrap=function(g,h){return function(){var j=[g].concat(d.toArray(arguments));return h.apply(h,j)}};d.compose=function(){var g=d.toArray(arguments);return function(){for(var h=g.length-1;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}})();
+(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;k++){n.call(m,p[k],k,p)}}else{for(var j in p){if(Object.prototype.hasOwnProperty.call(p,j)){n.call(m,p[j],j,p)}}}}}catch(o){if(o!="__break__"){throw o}}return p};d.map=function(k,j,h){if(k&&k.map){return k.map(j,h)}var g=[];d.each(k,function(o,m,n){g.push(j.call(h,o,m,n))});return g};d.reduce=function(k,g,j,h){if(k&&k.reduce){return k.reduce(d.bind(j,h),g)}d.each(k,function(o,m,n){g=j.call(h,g,o,m,n)});return g};d.reduceRight=function(k,g,j,h){if(k&&k.reduceRight){return k.reduceRight(d.bind(j,h),g)}var m=d.clone(d.toArray(k)).reverse();d.each(m,function(o,n){g=j.call(h,g,o,n,k)});return g};d.detect=function(k,j,h){var g;d.each(k,function(o,m,n){if(j.call(h,o,m,n)){g=o;d.breakLoop()}});return g};d.select=function(k,j,h){if(k.filter){return k.filter(j,h)}var g=[];d.each(k,function(o,m,n){j.call(h,o,m,n)&&g.push(o)});return g};d.reject=function(k,j,h){var g=[];d.each(k,function(o,m,n){!j.call(h,o,m,n)&&g.push(o)});return g};d.all=function(k,j,h){j=j||d.identity;if(k.every){return k.every(j,h)}var g=true;d.each(k,function(o,m,n){if(!(g=g&&j.call(h,o,m,n))){d.breakLoop()}});return g};d.any=function(k,j,h){j=j||d.identity;if(k.some){return k.some(j,h)}var g=false;d.each(k,function(o,m,n){if(g=j.call(h,o,m,n)){d.breakLoop()}});return g};d.include=function(j,h){if(d.isArray(j)){return d.indexOf(j,h)!=-1}var g=false;d.each(j,function(k){if(g=k===h){d.breakLoop()}});return g};d.invoke=function(h,j){var g=d.toArray(arguments).slice(2);return d.map(h,function(k){return(j?k[j]:k).apply(k,g)})};d.pluck=function(h,g){return d.map(h,function(j){return j[g]})};d.max=function(k,j,h){if(!j&&d.isArray(k)){return Math.max.apply(Math,k)}var g={computed:-Infinity};d.each(k,function(p,m,o){var n=j?j.call(h,p,m,o):p;n>=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;n<g.computed&&(g={value:p,computed:n})});return g.value};d.sortBy=function(j,h,g){return d.pluck(d.map(j,function(n,k,m){return{value:n,criteria:h.call(g,n,k,m)}}).sort(function(o,n){var m=o.criteria,k=n.criteria;return m<k?-1:m>k?1:0}),"value")};d.sortedIndex=function(n,m,j){j=j||d.identity;var g=0,k=n.length;while(g<k){var h=(g+k)>>1;j(n[h])<j(m)?g=h+1:k=h}return g};d.toArray=function(g){if(!g){return[]}if(d.isArray(g)){return g}return d.map(g,function(h){return h})};d.size=function(g){return d.toArray(g).length};d.first=function(g){return g[0]};d.last=function(g){return g[g.length-1]};d.compact=function(g){return d.select(g,function(h){return !!h})};d.flatten=function(g){return d.reduce(g,[],function(h,j){if(d.isArray(j)){return h.concat(d.flatten(j))}h.push(j);return h})};d.without=function(h){var g=h.slice.call(arguments,0);return d.select(h,function(j){return !d.include(g,j)})};d.uniq=function(h,g){return d.reduce(h,[],function(j,m,k){if(0==k||(g?d.last(j)!=m:!d.include(j,m))){j.push(m)}return j})};d.intersect=function(h){var g=d.toArray(arguments).slice(1);return d.select(d.uniq(h),function(j){return d.all(g,function(k){return d.indexOf(k,j)>=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<k;h++){j[h]=d.pluck(g,String(h))}return j};d.indexOf=function(h,g){if(h.indexOf){return h.indexOf(g)}for(i=0,l=h.length;i<l;i++){if(h[i]===g){return i}}return -1};d.lastIndexOf=function(j,h){if(j.lastIndexOf){return j.lastIndexOf(h)}var g=j.length;while(g--){if(j[g]===h){return g}}return -1};d.bind=function(j,h){h=h||c;var g=d.toArray(arguments).slice(2);return function(){var k=g.concat(d.toArray(arguments));return j.apply(h,k)}};d.bindAll=function(){var g=d.toArray(arguments);var h=g.pop();d.each(g,function(j){h[j]=d.bind(h[j],h)})};d.delay=function(h,j){var g=d.toArray(arguments).slice(2);return setTimeout(function(){return h.apply(h,g)},j)};d.defer=function(g){return d.delay.apply(d,[g,1].concat(d.toArray(arguments).slice(1)))};d.wrap=function(g,h){return function(){var j=[g].concat(d.toArray(arguments));return h.apply(h,j)}};d.compose=function(){var g=d.toArray(arguments);return function(){for(var h=g.length-1;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}})();
4 underscore.js
View
@@ -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;
};

No commit comments for this range

Something went wrong with that request. Please try again.