mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 09:47:48 +00:00
Make _.unzip its own method.
This commit is contained in:
50
lodash.js
50
lodash.js
@@ -4073,6 +4073,35 @@
|
|||||||
return baseUniq(array, isSorted, iterator);
|
return baseUniq(array, isSorted, iterator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is like `_.zip` except that it accepts an array of grouped
|
||||||
|
* elements and creates an array regrouping the elements to their pre `_.zip`
|
||||||
|
* configuration.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Array
|
||||||
|
* @param {Array} array The array of grouped elements to process.
|
||||||
|
* @returns {Array} Returns the new array of regrouped elements.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
|
||||||
|
* // => [['fred', 30, true], ['barney', 40, false]]
|
||||||
|
*
|
||||||
|
* _.unzip(zipped);
|
||||||
|
* // => [['fred', 'barney'], [30, 40], [true, false]]
|
||||||
|
*/
|
||||||
|
function unzip(array) {
|
||||||
|
var index = -1,
|
||||||
|
length = isObject(length = max(array, 'length')) && length.length || 0,
|
||||||
|
result = Array(length);
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
result[index] = pluck(array, index);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an array excluding all provided values using strict equality for
|
* Creates an array excluding all provided values using strict equality for
|
||||||
* comparisons, i.e. `===`.
|
* comparisons, i.e. `===`.
|
||||||
@@ -4128,33 +4157,20 @@
|
|||||||
/**
|
/**
|
||||||
* Creates an array of grouped elements, the first of which contains the first
|
* Creates an array of grouped elements, the first of which contains the first
|
||||||
* elements of the given arrays, the second of which contains the second elements
|
* elements of the given arrays, the second of which contains the second elements
|
||||||
* of the given arrays, and so on. If a zipped value is provided its corresponding
|
* of the given arrays, and so on.
|
||||||
* unzipped value is returned.
|
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @alias unzip
|
|
||||||
* @category Array
|
* @category Array
|
||||||
* @param {...Array} [arrays] The arrays to process.
|
* @param {...Array} [arrays] The arrays to process.
|
||||||
* @returns {Array} Returns the array of grouped elements.
|
* @returns {Array} Returns the new array of grouped elements.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* _.zip(['fred', 'barney'], [30, 40], [true, false]);
|
* _.zip(['fred', 'barney'], [30, 40], [true, false]);
|
||||||
* // => [['fred', 30, true], ['barney', 40, false]]
|
* // => [['fred', 30, true], ['barney', 40, false]]
|
||||||
*
|
|
||||||
* _.unzip([['fred', 30, true], ['barney', 40, false]]);
|
|
||||||
* // => [['fred', 'barney'], [30, 40], [true, false]]
|
|
||||||
*/
|
*/
|
||||||
function zip() {
|
function zip() {
|
||||||
var array = arguments.length > 1 ? arguments : arguments[0],
|
return unzip(arguments);
|
||||||
index = -1,
|
|
||||||
length = isObject(length = max(array, 'length')) && length.length || 0,
|
|
||||||
result = Array(length);
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
result[index] = pluck(array, index);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8980,6 +8996,7 @@
|
|||||||
lodash.transform = transform;
|
lodash.transform = transform;
|
||||||
lodash.union = union;
|
lodash.union = union;
|
||||||
lodash.uniq = uniq;
|
lodash.uniq = uniq;
|
||||||
|
lodash.unzip = unzip;
|
||||||
lodash.values = values;
|
lodash.values = values;
|
||||||
lodash.valuesIn = valuesIn;
|
lodash.valuesIn = valuesIn;
|
||||||
lodash.where = where;
|
lodash.where = where;
|
||||||
@@ -8999,7 +9016,6 @@
|
|||||||
lodash.select = filter;
|
lodash.select = filter;
|
||||||
lodash.tail = rest;
|
lodash.tail = rest;
|
||||||
lodash.unique = uniq;
|
lodash.unique = uniq;
|
||||||
lodash.unzip = zip;
|
|
||||||
|
|
||||||
// add functions to `lodash.prototype`
|
// add functions to `lodash.prototype`
|
||||||
mixin(lodash, baseAssign({}, lodash));
|
mixin(lodash, baseAssign({}, lodash));
|
||||||
|
|||||||
38
test/test.js
38
test/test.js
@@ -10807,9 +10807,12 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.zip');
|
QUnit.module('lodash.unzip and lodash.zip');
|
||||||
|
|
||||||
|
_.each(['unzip', 'zip'], function(methodName) {
|
||||||
|
var func = _[methodName];
|
||||||
|
func = _.bind(methodName == 'zip' ? func.apply : func.call, func, null);
|
||||||
|
|
||||||
(function() {
|
|
||||||
var object = {
|
var object = {
|
||||||
'an empty array': [
|
'an empty array': [
|
||||||
[],
|
[],
|
||||||
@@ -10830,52 +10833,43 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
_.forOwn(object, function(pair, key) {
|
_.forOwn(object, function(pair, key) {
|
||||||
test('should work with ' + key, 2, function() {
|
test('`_.' + methodName + '` should work with ' + key, 2, function() {
|
||||||
var actual = _.zip.apply(_, pair[0]);
|
var actual = func(pair[0]);
|
||||||
deepEqual(actual, pair[1]);
|
deepEqual(actual, pair[1]);
|
||||||
deepEqual(_.zip.apply(_, actual), actual.length ? pair[0] : []);
|
deepEqual(func(actual), actual.length ? pair[0] : []);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with tuples of different lengths', 4, function() {
|
test('`_.' + methodName + '` should work with tuples of different lengths', 4, function() {
|
||||||
var pair = [
|
var pair = [
|
||||||
[['barney', 36], ['fred', 40, false]],
|
[['barney', 36], ['fred', 40, false]],
|
||||||
[['barney', 'fred'], [36, 40], [undefined, false]]
|
[['barney', 'fred'], [36, 40], [undefined, false]]
|
||||||
];
|
];
|
||||||
|
|
||||||
var actual = _.zip(pair[0]);
|
var actual = func(pair[0]);
|
||||||
ok('0' in actual[2]);
|
ok('0' in actual[2]);
|
||||||
deepEqual(actual, pair[1]);
|
deepEqual(actual, pair[1]);
|
||||||
|
|
||||||
actual = _.zip.apply(_, actual);
|
actual = func(actual);
|
||||||
ok('2' in actual[0]);
|
ok('2' in actual[0]);
|
||||||
deepEqual(actual, [['barney', 36, undefined], ['fred', 40, false]]);
|
deepEqual(actual, [['barney', 36, undefined], ['fred', 40, false]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should treat falsey values as empty arrays', 1, function() {
|
test('`_.' + methodName + '` should treat falsey values as empty arrays', 1, function() {
|
||||||
var expected = _.map(falsey, _.constant([]));
|
var expected = _.map(falsey, _.constant([]));
|
||||||
|
|
||||||
var actual = _.map(falsey, function(value) {
|
var actual = _.map(falsey, function(value) {
|
||||||
return _.zip(value, value, value);
|
return func([value, value, value]);
|
||||||
});
|
});
|
||||||
|
|
||||||
deepEqual(actual, expected);
|
deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should support consuming its return value', 1, function() {
|
test('`_.' + methodName + '` should support consuming its return value', 1, function() {
|
||||||
var expected = [['barney', 'fred'], [36, 40]];
|
var expected = [['barney', 'fred'], [36, 40]];
|
||||||
deepEqual(_.zip(_.zip(_.zip(_.zip(expected)))), expected);
|
deepEqual(func(func(func(func(expected)))), expected);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
test('should support consuming its return value', 1, function() {
|
|
||||||
var expected = [['barney', 'fred'], [36, 40]];
|
|
||||||
deepEqual(_.zip(_.zip(_.zip(_.zip(expected)))), expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should be aliased', 1, function() {
|
|
||||||
strictEqual(_.unzip, _.zip);
|
|
||||||
});
|
|
||||||
}());
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user