mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
Make _.unzip and alias of _.zip.
Former-commit-id: fca00001ad850c250f9883572c4dce7b41dde88d
This commit is contained in:
8
build.js
8
build.js
@@ -55,7 +55,8 @@
|
|||||||
'select': 'filter',
|
'select': 'filter',
|
||||||
'tail': 'rest',
|
'tail': 'rest',
|
||||||
'take': 'first',
|
'take': 'first',
|
||||||
'unique': 'uniq'
|
'unique': 'uniq',
|
||||||
|
'unzip': 'zip'
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Used to associate real names with their aliases */
|
/** Used to associate real names with their aliases */
|
||||||
@@ -74,6 +75,7 @@
|
|||||||
'rest': ['drop', 'tail'],
|
'rest': ['drop', 'tail'],
|
||||||
'some': ['any'],
|
'some': ['any'],
|
||||||
'uniq': ['unique'],
|
'uniq': ['unique'],
|
||||||
|
'zip': ['unzip'],
|
||||||
'zipObject': ['object']
|
'zipObject': ['object']
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -179,13 +181,12 @@
|
|||||||
'union': ['basicFlatten', 'basicUniq'],
|
'union': ['basicFlatten', 'basicUniq'],
|
||||||
'uniq': ['basicUniq', 'overloadWrapper'],
|
'uniq': ['basicUniq', 'overloadWrapper'],
|
||||||
'uniqueId': [],
|
'uniqueId': [],
|
||||||
'unzip': ['max', 'pluck'],
|
|
||||||
'value': ['basicEach', 'forOwn', 'isArray', 'lodash', 'wrapperValueOf', 'lodashWrapper'],
|
'value': ['basicEach', 'forOwn', 'isArray', 'lodash', 'wrapperValueOf', 'lodashWrapper'],
|
||||||
'values': ['keys'],
|
'values': ['keys'],
|
||||||
'where': ['filter'],
|
'where': ['filter'],
|
||||||
'without': ['difference'],
|
'without': ['difference'],
|
||||||
'wrap': [],
|
'wrap': [],
|
||||||
'zip': ['unzip'],
|
'zip': ['max', 'pluck'],
|
||||||
'zipObject': [],
|
'zipObject': [],
|
||||||
|
|
||||||
// private functions
|
// private functions
|
||||||
@@ -282,7 +283,6 @@
|
|||||||
'sortedIndex',
|
'sortedIndex',
|
||||||
'union',
|
'union',
|
||||||
'uniq',
|
'uniq',
|
||||||
'unzip',
|
|
||||||
'without',
|
'without',
|
||||||
'zip',
|
'zip',
|
||||||
'zipObject'
|
'zipObject'
|
||||||
|
|||||||
48
lodash.js
48
lodash.js
@@ -4468,32 +4468,6 @@
|
|||||||
*/
|
*/
|
||||||
var uniq = overloadWrapper(basicUniq);
|
var uniq = overloadWrapper(basicUniq);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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() {
|
|
||||||
var array = arguments.length > 1 ? arguments : arguments[0],
|
|
||||||
index = -1,
|
|
||||||
length = array ? max(pluck(array, 'length')) : 0,
|
|
||||||
result = Array(length < 0 ? 0 : length);
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
result[index] = pluck(array, index);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an array with all occurrences of the passed values removed using
|
* Creates an array with all occurrences of the passed values removed using
|
||||||
* strict equality for comparisons, i.e. `===`.
|
* strict equality for comparisons, i.e. `===`.
|
||||||
@@ -4514,13 +4488,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Groups the elements of each array at their corresponding indexes. Useful for
|
* Creates an array of grouped elements, the first of which contains the first
|
||||||
* separate data sources that are coordinated through matching array indexes.
|
* elements of the given arrays, the second of which contains the second
|
||||||
* For a matrix of nested arrays, `_.zip.apply(...)` can transpose the matrix
|
* elements of the given arrays, and so on.
|
||||||
* in a similar fashion.
|
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
|
* @alias unzip
|
||||||
* @category Arrays
|
* @category Arrays
|
||||||
* @param {Array} [array1, array2, ...] Arrays to process.
|
* @param {Array} [array1, array2, ...] Arrays to process.
|
||||||
* @returns {Array} Returns a new array of grouped elements.
|
* @returns {Array} Returns a new array of grouped elements.
|
||||||
@@ -4529,8 +4503,16 @@
|
|||||||
* _.zip(['moe', 'larry'], [30, 40], [true, false]);
|
* _.zip(['moe', 'larry'], [30, 40], [true, false]);
|
||||||
* // => [['moe', 30, true], ['larry', 40, false]]
|
* // => [['moe', 30, true], ['larry', 40, false]]
|
||||||
*/
|
*/
|
||||||
function zip(array) {
|
function zip() {
|
||||||
return array ? unzip(arguments) : [];
|
var array = arguments.length > 1 ? arguments : arguments[0],
|
||||||
|
index = -1,
|
||||||
|
length = array ? max(pluck(array, 'length')) : 0,
|
||||||
|
result = Array(length < 0 ? 0 : length);
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
result[index] = pluck(array, index);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5746,7 +5728,6 @@
|
|||||||
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.where = where;
|
lodash.where = where;
|
||||||
lodash.without = without;
|
lodash.without = without;
|
||||||
@@ -5764,6 +5745,7 @@
|
|||||||
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);
|
mixin(lodash);
|
||||||
|
|||||||
@@ -67,7 +67,8 @@
|
|||||||
'select': 'filter',
|
'select': 'filter',
|
||||||
'tail': 'rest',
|
'tail': 'rest',
|
||||||
'take': 'first',
|
'take': 'first',
|
||||||
'unique': 'uniq'
|
'unique': 'uniq',
|
||||||
|
'unzip': 'zip'
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Used to associate real names with their aliases */
|
/** Used to associate real names with their aliases */
|
||||||
@@ -86,6 +87,7 @@
|
|||||||
'rest': ['drop', 'tail'],
|
'rest': ['drop', 'tail'],
|
||||||
'some': ['any'],
|
'some': ['any'],
|
||||||
'uniq': ['unique'],
|
'uniq': ['unique'],
|
||||||
|
'zip': ['unzip'],
|
||||||
'zipObject': ['object']
|
'zipObject': ['object']
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -107,7 +109,6 @@
|
|||||||
'sortedIndex',
|
'sortedIndex',
|
||||||
'union',
|
'union',
|
||||||
'uniq',
|
'uniq',
|
||||||
'unzip',
|
|
||||||
'without',
|
'without',
|
||||||
'zip',
|
'zip',
|
||||||
'zipObject'
|
'zipObject'
|
||||||
@@ -284,8 +285,7 @@
|
|||||||
'parseInt',
|
'parseInt',
|
||||||
'partialRight',
|
'partialRight',
|
||||||
'runInContext',
|
'runInContext',
|
||||||
'transform',
|
'transform'
|
||||||
'unzip'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/** List of all functions */
|
/** List of all functions */
|
||||||
@@ -999,9 +999,6 @@
|
|||||||
function Foo() {}
|
function Foo() {}
|
||||||
Foo.prototype = { 'a': 1 };
|
Foo.prototype = { 'a': 1 };
|
||||||
|
|
||||||
actual = lodash.defaults({ 'a': null }, { 'a': 1 });
|
|
||||||
strictEqual(actual.a, 1, '_.defaults should overwrite `null` values: ' + basename);
|
|
||||||
|
|
||||||
deepEqual(lodash.defaults({}, new Foo), Foo.prototype, '_.defaults should assign inherited `source` properties: ' + basename);
|
deepEqual(lodash.defaults({}, new Foo), Foo.prototype, '_.defaults should assign inherited `source` properties: ' + basename);
|
||||||
deepEqual(lodash.extend({}, new Foo), Foo.prototype, '_.extend should assign inherited `source` properties: ' + basename);
|
deepEqual(lodash.extend({}, new Foo), Foo.prototype, '_.extend should assign inherited `source` properties: ' + basename);
|
||||||
|
|
||||||
@@ -1065,7 +1062,6 @@
|
|||||||
actual = lodash.pick(object, function(value) { return value != 3; });
|
actual = lodash.pick(object, function(value) { return value != 3; });
|
||||||
deepEqual(_.keys(actual), [], '_.pick should not accept a `callback`: ' + basename);
|
deepEqual(_.keys(actual), [], '_.pick should not accept a `callback`: ' + basename);
|
||||||
|
|
||||||
strictEqual(lodash.result(), null, '_.result should return `null` for falsey `object` arguments: ' + basename);
|
|
||||||
strictEqual(lodash.some([false, true, false]), true, '_.some: ' + basename);
|
strictEqual(lodash.some([false, true, false]), true, '_.some: ' + basename);
|
||||||
deepEqual(lodash.times(null, function() {}), [null], '_.times should not coerce `n` to a number: ' + basename);
|
deepEqual(lodash.times(null, function() {}), [null], '_.times should not coerce `n` to a number: ' + basename);
|
||||||
equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename);
|
equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename);
|
||||||
@@ -1581,8 +1577,7 @@
|
|||||||
'uniq',
|
'uniq',
|
||||||
'uniqueId',
|
'uniqueId',
|
||||||
'value',
|
'value',
|
||||||
'where',
|
'where'
|
||||||
'zip'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
function strip(value) {
|
function strip(value) {
|
||||||
@@ -1599,9 +1594,6 @@
|
|||||||
if (funcName == 'createCallback') {
|
if (funcName == 'createCallback') {
|
||||||
command += ',where';
|
command += ',where';
|
||||||
}
|
}
|
||||||
if (funcName == 'zip') {
|
|
||||||
command += ',unzip';
|
|
||||||
}
|
|
||||||
if (funcName != 'chain' && _.contains(categoryMap.Chaining.concat('mixin'), funcName)) {
|
if (funcName != 'chain' && _.contains(categoryMap.Chaining.concat('mixin'), funcName)) {
|
||||||
command += ',chain';
|
command += ',chain';
|
||||||
}
|
}
|
||||||
|
|||||||
105
test/test.js
105
test/test.js
@@ -3368,58 +3368,6 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.unzip');
|
|
||||||
|
|
||||||
(function() {
|
|
||||||
var object = {
|
|
||||||
'an empty array': [
|
|
||||||
[],
|
|
||||||
[]
|
|
||||||
],
|
|
||||||
'0-tuples': [
|
|
||||||
[[], []],
|
|
||||||
[]
|
|
||||||
],
|
|
||||||
'1-tuples': [
|
|
||||||
[['moe'], ['larry']],
|
|
||||||
[['moe', 'larry']]
|
|
||||||
],
|
|
||||||
'2-tuples': [
|
|
||||||
[['moe', 30], ['larry', 40]],
|
|
||||||
[['moe', 'larry'], [30, 40]]
|
|
||||||
],
|
|
||||||
'3-tuples': [
|
|
||||||
[['moe', 30, true], ['larry', 40, false]],
|
|
||||||
[['moe', 'larry'], [30, 40], [true, false]]
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
_.forOwn(object, function(pair, key) {
|
|
||||||
test('should work with ' + key, function() {
|
|
||||||
var actual = _.unzip(pair[0]);
|
|
||||||
deepEqual(actual, pair[1]);
|
|
||||||
deepEqual(_.zip.apply(_, actual), pair[1].length ? pair[0] : pair[1]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with tuples of different lengths', function() {
|
|
||||||
var pair = [
|
|
||||||
[['moe', 30], ['larry', 40, false]],
|
|
||||||
[['moe', 'larry'], [30, 40], [undefined, false]]
|
|
||||||
];
|
|
||||||
|
|
||||||
var actual = _.unzip(pair[0]);
|
|
||||||
ok(1 in actual);
|
|
||||||
deepEqual(actual, pair[1]);
|
|
||||||
|
|
||||||
actual = _.zip.apply(_, actual);
|
|
||||||
ok(2 in actual[0]);
|
|
||||||
deepEqual(actual, [['moe', 30, undefined], ['larry', 40, false]]);
|
|
||||||
});
|
|
||||||
}());
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
QUnit.module('lodash.where');
|
QUnit.module('lodash.where');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -3486,6 +3434,59 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.zip');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var object = {
|
||||||
|
'an empty array': [
|
||||||
|
[],
|
||||||
|
[]
|
||||||
|
],
|
||||||
|
'0-tuples': [
|
||||||
|
[[], []],
|
||||||
|
[]
|
||||||
|
],
|
||||||
|
'2-tuples': [
|
||||||
|
[['moe', 'larry'], [30, 40]],
|
||||||
|
[['moe', 30], ['larry', 40]]
|
||||||
|
],
|
||||||
|
'3-tuples': [
|
||||||
|
[['moe', 'larry'], [30, 40], [true, false]],
|
||||||
|
[['moe', 30, true], ['larry', 40, false]]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
_.forOwn(object, function(pair, key) {
|
||||||
|
test('should work with ' + key, function() {
|
||||||
|
var actual = _.zip.apply(_, pair[0]);
|
||||||
|
deepEqual(actual, pair[1]);
|
||||||
|
deepEqual(_.zip.apply(_, actual), actual.length ? pair[0] : []);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with tuples of different lengths', function() {
|
||||||
|
var pair = [
|
||||||
|
[['moe', 30], ['larry', 40, false]],
|
||||||
|
[['moe', 'larry'], [30, 40], [undefined, false]]
|
||||||
|
];
|
||||||
|
|
||||||
|
var actual = _.zip(pair[0]);
|
||||||
|
ok(0 in actual[2]);
|
||||||
|
deepEqual(actual, pair[1]);
|
||||||
|
|
||||||
|
actual = _.zip.apply(_, actual);
|
||||||
|
ok(2 in actual[0]);
|
||||||
|
deepEqual(actual, [['moe', 30, undefined], ['larry', 40, false]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be able to consume the output of `_.unzip`', function() {
|
||||||
|
var expected = [['moe', 'larry'], [30, 40]];
|
||||||
|
deepEqual(_.unzip(_.zip(_.unzip(_.zip(expected)))), expected);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash(...).shift');
|
QUnit.module('lodash(...).shift');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user