From 8f94bd1fbdf23e03326c1197c0bd029f3c1af1a2 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 30 Apr 2013 09:18:26 -0700 Subject: [PATCH] Reduce `_.unzip` and `_.zip`. Former-commit-id: f388c50817910eee510f33b22fd4904fd648a6f0 --- build.js | 2 +- lodash.js | 21 ++++----------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/build.js b/build.js index 097ae0f3e..eca0164cb 100755 --- a/build.js +++ b/build.js @@ -176,7 +176,7 @@ 'where': ['filter'], 'without': ['difference'], 'wrap': [], - 'zip': ['max', 'pluck'], + 'zip': ['unzip'], 'zipObject': [], // method used by the `backbone` and `underscore` builds diff --git a/lodash.js b/lodash.js index 65069f0f3..3a22ac98b 100644 --- a/lodash.js +++ b/lodash.js @@ -4106,17 +4106,11 @@ */ function unzip(array) { var index = -1, - length = array ? array.length : 0, - tupleLength = length ? max(pluck(array, 'length')) : 0, - result = Array(tupleLength); + length = array ? max(pluck(array, 'length')) : 0, + result = Array(length < 0 ? 0 : length); while (++index < length) { - var tupleIndex = -1, - tuple = array[index]; - - while (++tupleIndex < tupleLength) { - (result[tupleIndex] || (result[tupleIndex] = Array(length)))[index] = tuple[tupleIndex]; - } + result[index] = pluck(array, index); } return result; } @@ -4157,14 +4151,7 @@ * // => [['moe', 30, true], ['larry', 40, false]] */ function zip(array) { - var index = -1, - length = array ? max(pluck(arguments, 'length')) : 0, - result = Array(length); - - while (++index < length) { - result[index] = pluck(arguments, index); - } - return result; + return array ? unzip(arguments) : []; } /**