Reduce _.unzip and _.zip.

Former-commit-id: f388c50817910eee510f33b22fd4904fd648a6f0
This commit is contained in:
John-David Dalton
2013-04-30 09:18:26 -07:00
parent d24d83d52a
commit 8f94bd1fbd
2 changed files with 5 additions and 18 deletions

View File

@@ -176,7 +176,7 @@
'where': ['filter'],
'without': ['difference'],
'wrap': [],
'zip': ['max', 'pluck'],
'zip': ['unzip'],
'zipObject': [],
// method used by the `backbone` and `underscore` builds

View File

@@ -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) : [];
}
/**