From 36488fd8360cf6f5d812db08c5c8495fff9b40db Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 8 Jul 2013 08:29:18 -0700 Subject: [PATCH] Simplify `_.zip` for the `underscore` build. Former-commit-id: 18560e78f052fad5c5d7af1ef6571dd31accf690 --- build.js | 15 +++++++++++++++ dist/lodash.underscore.js | 7 +++---- dist/lodash.underscore.min.js | 2 +- test/test-build.js | 7 ++++++- test/test.js | 2 +- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/build.js b/build.js index 4df218bc3..19da535ef 100755 --- a/build.js +++ b/build.js @@ -3598,6 +3598,21 @@ '}' ].join('\n')); } + // replace `_.zip` + if(!isLodashFunc('zip')) { + source = replaceFunction(source, 'zip', [ + 'function zip() {', + ' var index = -1,', + " length = max(pluck(arguments, 'length')),", + ' result = Array(length < 0 ? 0 : length);', + '', + ' while (++index < length) {', + ' result[index] = pluck(arguments, index);', + ' }', + ' return result;', + '}' + ].join('\n')); + } // unexpose `lodash.support` if (!isLodashFunc('support')) { source = source.replace(/\blodash\.support *= */, ''); diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 19d3b3a9e..fac2636f7 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -3214,13 +3214,12 @@ * // => [['moe', 30, true], ['larry', 40, false]] */ function zip() { - var array = arguments.length > 1 ? arguments : arguments[0], - index = -1, - length = array ? max(pluck(array, 'length')) : 0, + var index = -1, + length = max(pluck(arguments, 'length')), result = Array(length < 0 ? 0 : length); while (++index < length) { - result[index] = pluck(array, index); + result[index] = pluck(arguments, index); } return result; } diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 2263aa9d6..600dbde9f 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -14,7 +14,7 @@ if(null==n||null==t)return n===t;if(o=xt.call(n),u=xt.call(t),o!=u)return!1;swit else for(;++iu&&(u=r);return u}function B(n,t){var r=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++rarguments.length;t=G(t,e,4);var i=-1,o=n.length;if(typeof o=="number")for(u&&(r=n[++i]);++iarguments.length;if(typeof u!="number")var o=$t(n),u=o.length;return t=G(t,e,4),F(n,function(e,a,f){a=o?o[--u]:--u,r=i?(i=!1,n[a]):t(r,n[a],a,f) }),r}function D(n,t,r){var e;t=G(t,r),r=-1;var u=n?n.length:0;if(typeof u=="number")for(;++rr(u,o)&&i.push(o)}return i}function $(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&null!=t){var i=-1;for(t=G(t,r);++ie?Nt(0,u+e):e||0}else if(e)return e=C(n,r),n[e]===r?e:-1;return n?t(n,r,e):-1}function W(n,t,r){if(typeof t!="number"&&null!=t){var e=0,u=-1,i=n?n.length:0;for(t=G(t,r);++u>>1,r(n[e])i(f,l))&&(r&&f.push(l),a.push(e))}return a}function U(){for(var n=1r?0:r);++t>>1,r(n[e])i(f,l))&&(r&&f.push(l),a.push(e))}return a}function U(){for(var n=-1,t=N(B(arguments,"length")),r=Array(0>t?0:t);++nl&&(a=n.apply(f,o));else{var r=new Date;!s&&!h&&(c=r);var e=p-(r-c);0"'\/]/g,nt=/($^)/,tt=/['\n\r\t\u2028\u2029\\]/g,rt="[object Arguments]",et="[object Array]",ut="[object Boolean]",it="[object Date]",ot="[object Number]",at="[object Object]",ft="[object RegExp]",lt="[object String]",ct={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},pt={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},st=ct[typeof exports]&&exports,vt=ct[typeof module]&&module&&module.exports==st&&module,gt=ct[typeof global]&&global; diff --git a/test/test-build.js b/test/test-build.js index f517dbb1e..2886e431d 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -1079,6 +1079,10 @@ deepEqual(lodash.findWhere(collection, { 'a': 1 }), collection[0], '_.findWhere: ' + basename); strictEqual(lodash.findWhere(collection, {}), null, '_.findWhere should return `null` for falsey `properties`: ' + basename); + var expected = [[['moe', 30, true]], [['larry', 40, false]]]; + actual = lodash.unzip(lodash.zip(['moe', 'larry'], [30, 40], [true, false])); + deepEqual(actual, expected, '_.zip is unable to correctly consume it\'s output: ' + basename); + start(); }); }); @@ -1577,7 +1581,8 @@ 'uniq', 'uniqueId', 'value', - 'where' + 'where', + 'zip' ]; function strip(value) { diff --git a/test/test.js b/test/test.js index 5882460cd..b04804efa 100644 --- a/test/test.js +++ b/test/test.js @@ -3513,7 +3513,7 @@ deepEqual(actual, [['moe', 30, undefined], ['larry', 40, false]]); }); - test('should be able to consume the output of `_.unzip`', function() { + test('should correctly consume it\'s output', function() { var expected = [['moe', 'larry'], [30, 40]]; deepEqual(_.unzip(_.zip(_.unzip(_.zip(expected)))), expected); });