Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jashkenas/underscore
base: 0.3.0
...
head fork: jashkenas/underscore
compare: 0.3.1
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
21 index.html
@@ -107,11 +107,11 @@
<p>
<table>
<tr>
- <td><a href="underscore.js">Development Version (0.3.0)</a></td>
+ <td><a href="underscore.js">Development Version (0.3.1)</a></td>
<td><i>16kb, Uncompressed with Comments</i></td>
</tr>
<tr>
- <td><a href="underscore-min.js">Production Version (0.3.0)</a></td>
+ <td><a href="underscore-min.js">Production Version (0.3.1)</a></td>
<td><i>4kb, Packed and Gzipped</i></td>
</tr>
</table>
@@ -171,8 +171,9 @@
<br />
Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
- passed. If <b>list</b> is a JavaScript object, a pair with <b>key</b>
- and <b>value</b> properties will be yielded. If the list has an <b>each</b>
+ passed. Each invocation of <b>iterator</b> is called with three arguments:
+ <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
+ arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> has an <b>each</b>
method of its own, it will be used instead. Delegates to the native
<b>forEach</b> function if it exists.
</p>
@@ -297,8 +298,8 @@
<p id="pluck">
<b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
<br />
- An optimized version of what is perhaps the most common use-case for
- <b>map</b>: returning a list of property values.
+ An convenient version of what is perhaps the most common use-case for
+ <b>map</b>: extracting a list of property values.
</p>
<pre>
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
@@ -736,6 +737,14 @@
<h2>Change Log</h2>
<p>
+ <b class="header">0.3.1</b><br />
+ All iterators are now passed in the original collection as their third
+ argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
+ objects is now called with <tt>(value, key, collection)</tt>, for details
+ see <a href="#each">_.each</a>.
+ </p>
+
+ <p>
<b class="header">0.3.0</b><br />
Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
comprehensive optimizations, merged in
View
8 test/collections.js
@@ -26,9 +26,13 @@ $(document).ready(function() {
answers = [];
var obj = {one : 1, two : 2, three : 3};
obj.constructor.prototype.four = 4;
- _.each(obj, function(pair){ answers.push(pair.key); });
+ _.each(obj, function(value, key){ answers.push(key); });
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
delete obj.constructor.prototype.four;
+
+ answer = null;
+ _.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
+ ok(answer, 'can reference the original collection from inside the iterator');
});
test('collections: map', function() {
@@ -132,7 +136,7 @@ $(document).ready(function() {
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
var numbers = _.toArray({one : 1, two : 2, three : 3});
- equals(_.pluck(numbers, '0').join(', '), 'one, two, three', 'object flattened into array');
+ equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
});
test('collections: size', function() {
View
2  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.0";b.each=function(j,m,f){var n=0;try{if(j.forEach){j.forEach(m,f)}else{if(j.length){for(var k=0,g=j.length;k<g;k++){m.call(f,j[k],k)}}else{if(j.each){j.each(function(e){m.call(f,e,n++)})}else{var k=0;for(var q in j){if(Object.prototype.hasOwnProperty.call(j,q)){var p=j[q],h=[q,p];h.key=q;h.value=p;m.call(f,h,k++)}}}}}}catch(o){if(o!="__break__"){throw o}}return j};b.map=function(h,g,f){if(h&&h.map){return h.map(g,f)}var e=[];b.each(h,function(k,j){e.push(g.call(f,k,j))});return e};b.reduce=function(h,e,g,f){b.each(h,function(k,j){e=g.call(f,e,k,j)});return e};b.detect=function(h,g,f){var e;b.each(h,function(k,j){if(g.call(f,k,j)){e=k;throw"__break__"}});return e};b.select=function(h,g,f){if(h.filter){return h.filter(g,f)}var e=[];b.each(h,function(k,j){g.call(f,k,j)&&e.push(k)});return e};b.reject=function(h,g,f){var e=[];b.each(h,function(k,j){!g.call(f,k,j)&&e.push(k)});return e};b.all=function(h,g,f){g=g||a;if(h.every){return h.every(g,f)}var e=true;b.each(h,function(k,j){if(!(e=e&&g.call(f,k,j))){throw"__break__"}});return e};b.any=function(h,g,f){g=g||a;if(h.some){return h.some(g,f)}var e=false;b.each(h,function(k,j){if(e=g.call(f,k,j)){throw"__break__"}});return e};b.include=function(g,f){if(b.isArray(g)){return b.indexOf(g,f)!=-1}var e=false;b.each(g,function(h){if(e=h.value===f){throw"__break__"}});return e};b.invoke=function(f,g){var e=b.toArray(arguments).slice(2);return b.map(f,function(h){return(g?h[g]:h).apply(h,e)})};b.pluck=function(g,f){var e=[];b.each(g,function(h){e.push(h[f])});return e};b.max=function(h,g,f){if(!g&&b.isArray(h)){return Math.max.apply(Math,h)}var e={computed:-Infinity};b.each(h,function(m,j){var k=g?g.call(f,m,j):m;k>=e.computed&&(e={value:m,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(m,j){var k=g?g.call(f,m,j):m;k<e.computed&&(e={value:m,computed:k})});return e.value};b.sortBy=function(g,f,e){return b.pluck(b.map(g,function(j,h){return{value:j,criteria:f.call(e,j,h)}}).sort(function(m,k){var j=m.criteria,h=k.criteria;return j<h?-1:j>h?1:0}),"value")};b.sortedIndex=function(k,j,g){g=g||a;var e=0,h=k.length;while(e<h){var f=(e+h)>>1;g(k[f])<g(j)?e=f+1:h=f}return e};b.toArray=function(e){if(!e){return[]}if(b.isArray(e)){return e}return b.map(e,function(f){return f})};b.size=function(e){return b.toArray(e).length};b.first=function(e){return e[0]};b.last=function(e){return e[e.length-1]};b.compact=function(e){return b.select(e,function(f){return !!f})};b.flatten=function(e){return b.reduce(e,[],function(f,g){if(b.isArray(g)){return f.concat(b.flatten(g))}f.push(g);return f})};b.without=function(f){var e=f.slice.call(arguments,0);return b.select(f,function(g){return !b.include(e,g)})};b.uniq=function(f,e){return b.reduce(f,[],function(g,j,h){if(0==h||(e?b.last(g)!=j:!b.include(g,j))){g.push(j)}return g})};b.intersect=function(f){var e=b.toArray(arguments).slice(1);return b.select(b.uniq(f),function(g){return b.all(e,function(h){return b.indexOf(h,g)>=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<h;f++){g[f]=b.pluck(e,String(f))}return g};b.indexOf=function(f,e){if(f.indexOf){return f.indexOf(e)}for(i=0,l=f.length;i<l;i++){if(f[i]===e){return i}}return -1};b.lastIndexOf=function(g,f){if(g.lastIndexOf){return g.lastIndexOf(f)}var e=g.length;while(e--){if(g[e]===f){return e}}return -1};b.bind=function(g,f){if(!f){return g}var e=b.toArray(arguments).slice(2);return function(){var h=e.concat(b.toArray(arguments));return g.apply(f,h)}};b.bindAll=function(){var e=b.toArray(arguments);var f=e.pop();b.each(e,function(g){f[g]=b.bind(f[g],f)})};b.delay=function(f,g){var e=b.toArray(arguments).slice(2);return setTimeout(function(){return f.apply(f,e)},g)};b.defer=function(e){return b.delay.apply(b,[e,1].concat(b.toArray(arguments).slice(1)))};b.wrap=function(e,f){return function(){var g=[e].concat(b.toArray(arguments));return f.apply(f,g)}};b.compose=function(){var e=b.toArray(arguments);return function(){for(var f=e.length-1;f>=0;f--){arguments=[e[f].apply(this,arguments)]}return arguments[0]}};b.keys=function(e){return b.pluck(e,"key")};b.values=function(e){return b.pluck(e,"value")};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})();
+(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<f;j++){m.call(k,o[j],j,o)}}else{if(o.each){o.each(function(e){m.call(k,e,g++,o)})}else{for(var h in o){if(Object.prototype.hasOwnProperty.call(o,h)){m.call(k,o[h],h,o)}}}}}}catch(n){if(n!="__break__"){throw n}}return o};b.map=function(h,g,f){if(h&&h.map){return h.map(g,f)}var e=[];b.each(h,function(m,j,k){e.push(g.call(f,m,j,k))});return e};b.reduce=function(h,e,g,f){b.each(h,function(m,j,k){e=g.call(f,e,m,j,k)});return e};b.detect=function(h,g,f){var e;b.each(h,function(m,j,k){if(g.call(f,m,j,k)){e=m;throw"__break__"}});return e};b.select=function(h,g,f){if(h.filter){return h.filter(g,f)}var e=[];b.each(h,function(m,j,k){g.call(f,m,j,k)&&e.push(m)});return e};b.reject=function(h,g,f){var e=[];b.each(h,function(m,j,k){!g.call(f,m,j,k)&&e.push(m)});return e};b.all=function(h,g,f){g=g||a;if(h.every){return h.every(g,f)}var e=true;b.each(h,function(m,j,k){if(!(e=e&&g.call(f,m,j,k))){throw"__break__"}});return e};b.any=function(h,g,f){g=g||a;if(h.some){return h.some(g,f)}var e=false;b.each(h,function(m,j,k){if(e=g.call(f,m,j,k)){throw"__break__"}});return e};b.include=function(g,f){if(b.isArray(g)){return b.indexOf(g,f)!=-1}var e=false;b.each(g,function(h){if(e=h===f){throw"__break__"}});return e};b.invoke=function(f,g){var e=b.toArray(arguments).slice(2);return b.map(f,function(h){return(g?h[g]:h).apply(h,e)})};b.pluck=function(f,e){return b.map(f,function(g){return g[e]})};b.max=function(h,g,f){if(!g&&b.isArray(h)){return Math.max.apply(Math,h)}var e={computed:-Infinity};b.each(h,function(n,j,m){var k=g?g.call(f,n,j,m):n;k>=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;k<e.computed&&(e={value:n,computed:k})});return e.value};b.sortBy=function(g,f,e){return b.pluck(b.map(g,function(k,h,j){return{value:k,criteria:f.call(e,k,h,j)}}).sort(function(m,k){var j=m.criteria,h=k.criteria;return j<h?-1:j>h?1:0}),"value")};b.sortedIndex=function(k,j,g){g=g||a;var e=0,h=k.length;while(e<h){var f=(e+h)>>1;g(k[f])<g(j)?e=f+1:h=f}return e};b.toArray=function(e){if(!e){return[]}if(b.isArray(e)){return e}return b.map(e,function(f){return f})};b.size=function(e){return b.toArray(e).length};b.first=function(e){return e[0]};b.last=function(e){return e[e.length-1]};b.compact=function(e){return b.select(e,function(f){return !!f})};b.flatten=function(e){return b.reduce(e,[],function(f,g){if(b.isArray(g)){return f.concat(b.flatten(g))}f.push(g);return f})};b.without=function(f){var e=f.slice.call(arguments,0);return b.select(f,function(g){return !b.include(e,g)})};b.uniq=function(f,e){return b.reduce(f,[],function(g,j,h){if(0==h||(e?b.last(g)!=j:!b.include(g,j))){g.push(j)}return g})};b.intersect=function(f){var e=b.toArray(arguments).slice(1);return b.select(b.uniq(f),function(g){return b.all(e,function(h){return b.indexOf(h,g)>=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<h;f++){g[f]=b.pluck(e,String(f))}return g};b.indexOf=function(f,e){if(f.indexOf){return f.indexOf(e)}for(i=0,l=f.length;i<l;i++){if(f[i]===e){return i}}return -1};b.lastIndexOf=function(g,f){if(g.lastIndexOf){return g.lastIndexOf(f)}var e=g.length;while(e--){if(g[e]===f){return e}}return -1};b.bind=function(g,f){if(!f){return g}var e=b.toArray(arguments).slice(2);return function(){var h=e.concat(b.toArray(arguments));return g.apply(f,h)}};b.bindAll=function(){var e=b.toArray(arguments);var f=e.pop();b.each(e,function(g){f[g]=b.bind(f[g],f)})};b.delay=function(f,g){var e=b.toArray(arguments).slice(2);return setTimeout(function(){return f.apply(f,e)},g)};b.defer=function(e){return b.delay.apply(b,[e,1].concat(b.toArray(arguments).slice(1)))};b.wrap=function(e,f){return function(){var g=[e].concat(b.toArray(arguments));return f.apply(f,g)}};b.compose=function(){var e=b.toArray(arguments);return function(){for(var f=e.length-1;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})();
View
66 underscore.js
@@ -26,7 +26,7 @@
commonJS ? _ = exports : window._ = _;
// Current version.
- _.VERSION = '0.3.0';
+ _.VERSION = '0.3.1';
/*------------------------ Collection Functions: ---------------------------*/
@@ -38,16 +38,12 @@
if (obj.forEach) {
obj.forEach(iterator, context);
} else if (obj.length) {
- for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i);
+ for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
} else if (obj.each) {
- obj.each(function(value) { iterator.call(context, value, index++); });
+ obj.each(function(value) { iterator.call(context, value, index++, obj); });
} else {
- var i = 0;
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) {
- var value = obj[key], pair = [key, value];
- pair.key = key;
- pair.value = value;
- iterator.call(context, pair, i++);
+ iterator.call(context, obj[key], key, obj);
}
}
} catch(e) {
@@ -61,8 +57,8 @@
_.map = function(obj, iterator, context) {
if (obj && obj.map) return obj.map(iterator, context);
var results = [];
- _.each(obj, function(value, index) {
- results.push(iterator.call(context, value, index));
+ _.each(obj, function(value, index, list) {
+ results.push(iterator.call(context, value, index, list));
});
return results;
};
@@ -70,8 +66,8 @@
// Reduce builds up a single result from a list of values. Also known as
// inject, or foldl.
_.reduce = function(obj, memo, iterator, context) {
- _.each(obj, function(value, index) {
- memo = iterator.call(context, memo, value, index);
+ _.each(obj, function(value, index, list) {
+ memo = iterator.call(context, memo, value, index, list);
});
return memo;
};
@@ -79,8 +75,8 @@
// Return the first value which passes a truth test.
_.detect = function(obj, iterator, context) {
var result;
- _.each(obj, function(value, index) {
- if (iterator.call(context, value, index)) {
+ _.each(obj, function(value, index, list) {
+ if (iterator.call(context, value, index, list)) {
result = value;
throw '__break__';
}
@@ -93,8 +89,8 @@
_.select = function(obj, iterator, context) {
if (obj.filter) return obj.filter(iterator, context);
var results = [];
- _.each(obj, function(value, index) {
- iterator.call(context, value, index) && results.push(value);
+ _.each(obj, function(value, index, list) {
+ iterator.call(context, value, index, list) && results.push(value);
});
return results;
};
@@ -102,8 +98,8 @@
// Return all the elements for which a truth test fails.
_.reject = function(obj, iterator, context) {
var results = [];
- _.each(obj, function(value, index) {
- !iterator.call(context, value, index) && results.push(value);
+ _.each(obj, function(value, index, list) {
+ !iterator.call(context, value, index, list) && results.push(value);
});
return results;
};
@@ -114,8 +110,8 @@
iterator = iterator || identity;
if (obj.every) return obj.every(iterator, context);
var result = true;
- _.each(obj, function(value, index) {
- if (!(result = result && iterator.call(context, value, index))) throw '__break__';
+ _.each(obj, function(value, index, list) {
+ if (!(result = result && iterator.call(context, value, index, list))) throw '__break__';
});
return result;
};
@@ -126,8 +122,8 @@
iterator = iterator || identity;
if (obj.some) return obj.some(iterator, context);
var result = false;
- _.each(obj, function(value, index) {
- if (result = iterator.call(context, value, index)) throw '__break__';
+ _.each(obj, function(value, index, list) {
+ if (result = iterator.call(context, value, index, list)) throw '__break__';
});
return result;
};
@@ -137,8 +133,8 @@
_.include = function(obj, target) {
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
var found = false;
- _.each(obj, function(pair) {
- if (found = pair.value === target) {
+ _.each(obj, function(value) {
+ if (found = value === target) {
throw '__break__';
}
});
@@ -153,19 +149,17 @@
});
};
- // Optimized version of a common use case of map: fetching a property.
+ // Convenience version of a common use case of map: fetching a property.
_.pluck = function(obj, key) {
- var results = [];
- _.each(obj, function(value){ results.push(value[key]); });
- return results;
+ return _.map(obj, function(value){ return value[key]; });
};
// Return the maximum item or (item-based computation).
_.max = function(obj, iterator, context) {
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
var result = {computed : -Infinity};
- _.each(obj, function(value, index) {
- var computed = iterator ? iterator.call(context, value, index) : value;
+ _.each(obj, function(value, index, list) {
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
computed >= result.computed && (result = {value : value, computed : computed});
});
return result.value;
@@ -175,8 +169,8 @@
_.min = function(obj, iterator, context) {
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
var result = {computed : Infinity};
- _.each(obj, function(value, index) {
- var computed = iterator ? iterator.call(context, value, index) : value;
+ _.each(obj, function(value, index, list) {
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
computed < result.computed && (result = {value : value, computed : computed});
});
return result.value;
@@ -184,10 +178,10 @@
// Sort the object's values by a criteria produced by an iterator.
_.sortBy = function(obj, iterator, context) {
- return _.pluck(_.map(obj, function(value, index) {
+ return _.pluck(_.map(obj, function(value, index, list) {
return {
value : value,
- criteria : iterator.call(context, value, index)
+ criteria : iterator.call(context, value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria, b = right.criteria;
@@ -361,12 +355,12 @@
// Retrieve the names of an object's properties.
_.keys = function(obj) {
- return _.pluck(obj, 'key');
+ return _.map(obj, function(value, key){ return key; });
};
// Retrieve the values of an object's properties.
_.values = function(obj) {
- return _.pluck(obj, 'value');
+ return _.map(obj, identity);
};
// Extend a given object with all of the properties in a source object.

No commit comments for this range

Something went wrong with that request. Please try again.