Add _.unzip. [closes #225]

Former-commit-id: 4b2c7fc068fd430f3d78de850a5f7670fd0e1a4e
This commit is contained in:
John-David Dalton
2013-04-02 22:34:15 -07:00
parent 265ca8ba67
commit e76360c1b5
6 changed files with 138 additions and 26 deletions

View File

@@ -222,8 +222,8 @@
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
* `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `values`,
* `where`, `without`, `wrap`, and `zip`
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `unzip`,
* `values`, `where`, `without`, `wrap`, and `zip`
*
* The non-chainable wrapper functions are:
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,
@@ -4051,6 +4051,37 @@
return result;
}
/**
* The inverse of `_.zip`, this method splits groups of elements into arrays
* composed of elements from each group at their corresponding indexes.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to process.
* @returns {Array} Returns a new array of the composed arrays.
* @example
*
* _.unzip([['moe', 30, true], ['larry', 40, false]]);
* // => [['moe', 'larry'], [30, 40], [true, false]];
*/
function unzip(array) {
var index = -1,
length = array ? array.length : 0,
tupleLength = length ? max(pluck(array, 'length')) : 0,
result = Array(tupleLength);
while (++index < length) {
var tupleIndex = -1,
tuple = array[index];
while (++tupleIndex < tupleLength) {
(result[tupleIndex] || (result[tupleIndex] = Array(length)))[index] = tuple[tupleIndex];
}
}
return result;
}
/**
* Creates an array with all occurrences of the passed values removed using
* strict equality for comparisons, i.e. `===`.
@@ -5242,6 +5273,7 @@
lodash.toArray = toArray;
lodash.union = union;
lodash.uniq = uniq;
lodash.unzip = unzip;
lodash.values = values;
lodash.where = where;
lodash.without = without;