Split _.fromPairs into _.zipObject.

This commit is contained in:
John-David Dalton
2015-11-21 00:02:08 -08:00
parent bce8f498b4
commit 7745c91c69
3 changed files with 89 additions and 53 deletions

View File

@@ -1508,7 +1508,8 @@
* `thru`, `toArray`, `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`,
* `transform`, `unary`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`,
* `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`,
* `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`, and `zipWith`
* `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`, `zipObject`,
* and `zipWith`
*
* The wrapper methods that are **not** chainable by default are:
* `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`,
@@ -5611,40 +5612,27 @@
}
/**
* The inverse of `_.toPairs`; this method returns an object composed from arrays
* of property names and values. Provide either a single two dimensional array,
* e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names
* and one of corresponding values.
* The inverse of `_.toPairs`; this method returns an object composed
* from key-value `pairs`.
*
* @static
* @memberOf _
* @category Array
* @param {Array} props The property names.
* @param {Array} [values=[]] The property values.
* @param {Array} pairs The key-value pairs.
* @returns {Object} Returns the new object.
* @example
*
* _.fromPairs([['fred', 30], ['barney', 40]]);
* // => { 'fred': 30, 'barney': 40 }
*
* _.fromPairs(['fred', 'barney'], [30, 40]);
* // => { 'fred': 30, 'barney': 40 }
*/
function fromPairs(props, values) {
function fromPairs(pairs) {
var index = -1,
length = props ? props.length : 0,
length = pairs ? pairs.length : 0,
result = {};
if (length && !values && !isArray(props[0])) {
values = [];
}
while (++index < length) {
var key = props[index];
if (values) {
baseSet(result, key, values[index]);
} else if (key) {
baseSet(result, key[0], key[1]);
}
var pair = pairs[index];
baseSet(result, pair[0], pair[1]);
}
return result;
}
@@ -6770,6 +6758,33 @@
*/
var zip = rest(unzip);
/**
* This method is like `_.fromPairs` except that it accepts two arrays,
* one of property names and one of corresponding values.
*
* @static
* @memberOf _
* @category Array
* @param {Array} [props=[]] The property names.
* @param {Array} [values=[]] The property values.
* @returns {Object} Returns the new object.
* @example
*
* _.zipObject(['fred', 'barney'], [30, 40]);
* // => { 'fred': 30, 'barney': 40 }
*/
function zipObject(props, values) {
var index = -1,
length = props ? props.length : 0,
valsLength = values ? values.length : 0,
result = {};
while (++index < length) {
baseSet(result, props[index], index < valsLength ? values[index] : undefined);
}
return result;
}
/**
* This method is like `_.zip` except that it accepts `iteratee` to specify
* how grouped values should be combined. The iteratee is invoked with four
@@ -13775,6 +13790,7 @@
lodash.xorBy = xorBy;
lodash.xorWith = xorWith;
lodash.zip = zip;
lodash.zipObject = zipObject;
lodash.zipWith = zipWith;
// Add aliases.