mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Add _.unzip. [closes #225]
Former-commit-id: 4b2c7fc068fd430f3d78de850a5f7670fd0e1a4e
This commit is contained in:
13
build.js
13
build.js
@@ -171,6 +171,7 @@
|
|||||||
'union': ['uniq'],
|
'union': ['uniq'],
|
||||||
'uniq': ['createCallback', 'indexOf'],
|
'uniq': ['createCallback', 'indexOf'],
|
||||||
'uniqueId': [],
|
'uniqueId': [],
|
||||||
|
'unzip': ['max', 'pluck'],
|
||||||
'value': ['forOwn', 'isArray'],
|
'value': ['forOwn', 'isArray'],
|
||||||
'values': ['keys'],
|
'values': ['keys'],
|
||||||
'where': ['filter'],
|
'where': ['filter'],
|
||||||
@@ -258,8 +259,8 @@
|
|||||||
'without'
|
'without'
|
||||||
];
|
];
|
||||||
|
|
||||||
/** List of methods used by Underscore */
|
/** List of Lo-Dash only methods */
|
||||||
var underscoreMethods = _.without.apply(_, [allMethods].concat([
|
var lodashOnlyMethods = [
|
||||||
'at',
|
'at',
|
||||||
'bindKey',
|
'bindKey',
|
||||||
'cloneDeep',
|
'cloneDeep',
|
||||||
@@ -272,8 +273,12 @@
|
|||||||
'merge',
|
'merge',
|
||||||
'parseInt',
|
'parseInt',
|
||||||
'partialRight',
|
'partialRight',
|
||||||
'runInContext'
|
'runInContext',
|
||||||
]));
|
'unzip'
|
||||||
|
];
|
||||||
|
|
||||||
|
/** List of methods used by Underscore */
|
||||||
|
var underscoreMethods = _.without.apply(_, [allMethods].concat(lodashOnlyMethods));
|
||||||
|
|
||||||
/** List of ways to export the `lodash` function */
|
/** List of ways to export the `lodash` function */
|
||||||
var exportsAll = [
|
var exportsAll = [
|
||||||
|
|||||||
@@ -210,6 +210,7 @@
|
|||||||
'uniq',
|
'uniq',
|
||||||
'unique',
|
'unique',
|
||||||
'uniqueId',
|
'uniqueId',
|
||||||
|
'unzip',
|
||||||
'value',
|
'value',
|
||||||
'values',
|
'values',
|
||||||
'variable',
|
'variable',
|
||||||
|
|||||||
36
lodash.js
36
lodash.js
@@ -222,8 +222,8 @@
|
|||||||
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
|
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
|
||||||
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
|
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
|
||||||
* `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
|
* `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
|
||||||
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `values`,
|
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `unzip`,
|
||||||
* `where`, `without`, `wrap`, and `zip`
|
* `values`, `where`, `without`, `wrap`, and `zip`
|
||||||
*
|
*
|
||||||
* The non-chainable wrapper functions are:
|
* The non-chainable wrapper functions are:
|
||||||
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,
|
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,
|
||||||
@@ -4051,6 +4051,37 @@
|
|||||||
return result;
|
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
|
* Creates an array with all occurrences of the passed values removed using
|
||||||
* strict equality for comparisons, i.e. `===`.
|
* strict equality for comparisons, i.e. `===`.
|
||||||
@@ -5242,6 +5273,7 @@
|
|||||||
lodash.toArray = toArray;
|
lodash.toArray = toArray;
|
||||||
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;
|
||||||
|
|||||||
32
perf/perf.js
32
perf/perf.js
@@ -487,6 +487,10 @@
|
|||||||
var _findWhere = _.findWhere || _.find,\
|
var _findWhere = _.findWhere || _.find,\
|
||||||
lodashFindWhere = lodash.findWhere || lodash.find,\
|
lodashFindWhere = lodash.findWhere || lodash.find,\
|
||||||
whereObject = { "num": 9 };\
|
whereObject = { "num": 9 };\
|
||||||
|
}\
|
||||||
|
if (typeof zip != "undefined") {\
|
||||||
|
var unzipped = [["a", "b", "c"], [1, 2, 3], [true, false, true]],\
|
||||||
|
zipped = [["a", 1, true], ["b", 2, false], ["c", 3, true]];\
|
||||||
}'
|
}'
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1752,6 +1756,20 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
suites.push(
|
||||||
|
Benchmark.Suite('`_.unzip`')
|
||||||
|
.add(buildName, {
|
||||||
|
'fn': 'lodash.unzip(zipped);',
|
||||||
|
'teardown': 'function zip(){}'
|
||||||
|
})
|
||||||
|
.add(otherName, {
|
||||||
|
'fn': '_.unzip(zipped);',
|
||||||
|
'teardown': 'function zip(){}'
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
suites.push(
|
suites.push(
|
||||||
Benchmark.Suite('`_.values`')
|
Benchmark.Suite('`_.values`')
|
||||||
.add(buildName, '\
|
.add(buildName, '\
|
||||||
@@ -1802,6 +1820,20 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
suites.push(
|
||||||
|
Benchmark.Suite('`_.zip`')
|
||||||
|
.add(buildName, {
|
||||||
|
'fn': 'lodash.zip.apply(lodash, unzipped);',
|
||||||
|
'teardown': 'function zip(){}'
|
||||||
|
})
|
||||||
|
.add(otherName, {
|
||||||
|
'fn': '_.zip.apply(_, unzipped);',
|
||||||
|
'teardown': 'function zip(){}'
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
if (Benchmark.platform + '') {
|
if (Benchmark.platform + '') {
|
||||||
log(Benchmark.platform);
|
log(Benchmark.platform);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,6 +118,7 @@
|
|||||||
'union',
|
'union',
|
||||||
'uniq',
|
'uniq',
|
||||||
'unique',
|
'unique',
|
||||||
|
'unzip',
|
||||||
'without',
|
'without',
|
||||||
'zip',
|
'zip',
|
||||||
'zipObject'
|
'zipObject'
|
||||||
@@ -296,8 +297,8 @@
|
|||||||
'without'
|
'without'
|
||||||
];
|
];
|
||||||
|
|
||||||
/** List of methods used by Underscore */
|
/** List of Lo-Dash only methods */
|
||||||
var underscoreMethods = _.without.apply(_, [allMethods].concat([
|
var lodashOnlyMethods = [
|
||||||
'at',
|
'at',
|
||||||
'bindKey',
|
'bindKey',
|
||||||
'cloneDeep',
|
'cloneDeep',
|
||||||
@@ -310,8 +311,12 @@
|
|||||||
'merge',
|
'merge',
|
||||||
'parseInt',
|
'parseInt',
|
||||||
'partialRight',
|
'partialRight',
|
||||||
'runInContext'
|
'runInContext',
|
||||||
]));
|
'unzip'
|
||||||
|
];
|
||||||
|
|
||||||
|
/** List of methods used by Underscore */
|
||||||
|
var underscoreMethods = _.without.apply(_, [allMethods].concat(lodashOnlyMethods));
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
@@ -970,22 +975,7 @@
|
|||||||
vm.runInContext(data.source, context);
|
vm.runInContext(data.source, context);
|
||||||
var lodash = context._;
|
var lodash = context._;
|
||||||
|
|
||||||
_.each([
|
_.each(lodashOnlyMethods.concat('assign'), function(methodName) {
|
||||||
'assign',
|
|
||||||
'at',
|
|
||||||
'bindKey',
|
|
||||||
'createCallback',
|
|
||||||
'findIndex',
|
|
||||||
'findKey',
|
|
||||||
'forIn',
|
|
||||||
'forOwn',
|
|
||||||
'isPlainObject',
|
|
||||||
'merge',
|
|
||||||
'parseInt',
|
|
||||||
'partialRight',
|
|
||||||
'runInContext',
|
|
||||||
'zipObject'
|
|
||||||
], function(methodName) {
|
|
||||||
equal(lodash[methodName], undefined, '_.' + methodName + ' should not exist: ' + basename);
|
equal(lodash[methodName], undefined, '_.' + methodName + ' should not exist: ' + basename);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
52
test/test.js
52
test/test.js
@@ -2787,6 +2787,58 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user