From 16748c0920f93047d4815a6ae8b3ffc4bf54fd02 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 14 Oct 2012 18:52:59 -0700 Subject: [PATCH] Cleanup lodash.js and ensure `_.where` returns an empty array when passed an empty `properties` object. Former-commit-id: 34d1f8d967806d3c59895eff3a7d4d32262a46ea --- lodash.js | 19 ++++++++++--------- lodash.min.js | 8 ++++---- lodash.underscore.min.js | 4 ++-- test/test.js | 4 ++++ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lodash.js b/lodash.js index 61c6b16ba..d975317a8 100644 --- a/lodash.js +++ b/lodash.js @@ -420,7 +420,7 @@ /** * Reusable iterator options shared by `every`, `filter`, forEach`, `forIn`, - * `forOwn`, `map`, `reject`, and `some`. + * `forOwn`, `map`, and `some`. */ var forEachIteratorOptions = { 'args': 'collection, callback, thisArg', @@ -1348,7 +1348,6 @@ return false; } } - // assume cyclic structures are equal // the algorithm for detecting cyclic structures is adapted from ES 5.1 // section 15.12.3, abstract operation `JO` (http://es5.github.com/#x15.12.3) @@ -1386,7 +1385,6 @@ } return result; } - // deep compare objects for (var prop in a) { if (hasOwnProperty.call(a, prop)) { @@ -1673,7 +1671,8 @@ // avoid merging previously merged cyclic sources var stackLength = stackA.length; while (stackLength--) { - if ((found = stackA[stackLength] == source)) { + found = stackA[stackLength] == source; + if (found) { break; } } @@ -2475,7 +2474,7 @@ * @memberOf _ * @category Collections * @param {Array|Object|String} collection The collection to iterate over. - * @param {Object} properties The object of properties/values to filter by. + * @param {Object} properties The object of property values to filter by. * @returns {Array} Returns a new array of elements that contain the given `properties`. * @example * @@ -2492,13 +2491,15 @@ var props = []; forIn(properties, function(value, prop) { props.push(prop); }); - var propsLength = props.length, + var length = props.length, result = []; forEach(collection, function(value) { - for (var pass = true, propIndex = 0; propIndex < propsLength; propIndex++) { - var prop = props[propIndex]; - if (!(pass = value[prop] === properties[prop])) { + var index = -1, passed = true; + while (++index < length) { + var prop = props[index], + pass = value[prop] === properties[prop]; + if (!pass) { break; } } diff --git a/lodash.min.js b/lodash.min.js index 72654b956..c640b86c8 100644 --- a/lodash.min.js +++ b/lodash.min.js @@ -34,7 +34,7 @@ r--;)if(e[r]===t)return r;return-1},s.lateBind=function(e,t){return a(t,e,ct.cal e,t){return t(e),e},s.template=function(e,t,n){e||(e=""),n||(n={});var r,i,o=0,u=s.templateSettings,a="__p += '",f=n.variable||u.variable,l=f;e.replace(RegExp((n.escape||u.escape||et).source+"|"+(n.interpolate||u.interpolate||et).source+"|"+(n.evaluate||u.evaluate||et).source+"|$","g"),function(t,n,i,s,u){a+=e.slice(o,u).replace(nt,c),a+=n?"'+__e("+n+")+'":s?"';"+s+";__p+='":i?"'+((__t=("+i+"))==null?'':__t)+'":"",r||(r=s||V.test(n||i)),o=u+t.length}),a+="';",l||(f="obj",r?a="with("+f+"){"+a+"}": (n=RegExp("(\\(\\s*)"+f+"\\."+f+"\\b","g"),a=a.replace(Y,"$&"+f+".").replace(n,"$1__d"))),a=(r?a.replace(J,""):a).replace(K,"$1").replace(Q,"$1;"),a="function("+f+"){"+(l?"":f+"||("+f+"={});")+"var __t,__p='',__e=_.escape"+(r?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":(l?"":",__d="+f+"."+f+"||"+f)+";")+a+"return __p}";try{i=Function("_","return "+a)(s)}catch(h){throw h.source=a,h}return t?i(t):(i.source=a,i)},s.throttle=function(e,t){function n(){a=new Date,u=r,s=e. apply(o,i)}var i,s,o,u,a=0;return function(){var r=new Date,f=t-(r-a);return i=arguments,o=this,0>=f?(Lt(u),a=r,s=e.apply(o,i)):u||(u=At(n,f)),s}},s.times=function(e,t,n){for(var e=+e||0,r=-1,i=Array(e);++rD(r,i)&&r.push(i)}return r},s.uniq=B,s.uniqueId=function(e){var t=z++;return e?e+t:t},s.values=T,s.where=function(e,t){var r=[];Kt(t,function(e,t){r.push(t)});var i=r.length,s=[];return un(e,function(e){for(var o=n,u=0;uD(r,i)&&r.push(i)}return r},s.uniq=B,s.uniqueId=function(e){var t=z++;return e?e+t:t},s.values=T,s.where=function(e,t){var n=[];Kt(t,function(e,t){n.push(t)});var r=n.length,i=[];return un(e,function(e){for(var s=-1;++s=f?(St(u),a=r,s=e.apply(o,i)):u||(u=xt(n,f)),s}},p.times=function(e,t,n){for(var e=+e||0,r=-1,i=Array(e);++rB(r,i)&&r.push(i)}return r},p.uniq=I,p.uniqueId=function(e){var t=V++;return e?e+t:t},p.values=k,p.where=function(e,t){var r=[];h(t,function(e,t){r.push(t)});var i=r.length,s=[];return u(e,function(e){for(var o=n,u=0;uB(arguments,i,1)&&r.push(i)}return r},p.wrap=function( -e,t){return function(){var n=[e];return arguments.length&&st.apply(n,arguments),t.apply(this,n)}},p.zip=function(e){for(var t=-1,n=e?O(M(arguments,"length")):0,r=Array(n);++tB(r,i)&&r.push(i)}return r},p.uniq=I,p.uniqueId=function(e){var t=V++;return e?e+t:t},p.values=k,p.where=function(e,t){var n=[];h(t,function(e,t){n.push(t)});var r=n.length,i=[];return u(e,function(e){for(var s=-1;++sB(arguments,i,1)&&r.push(i)}return r},p.wrap= +function(e,t){return function(){var n=[e];return arguments.length&&st.apply(n,arguments),t.apply(this,n)}},p.zip=function(e){for(var t=-1,n=e?O(M(arguments,"length")):0,r=Array(n);++t