1 | import { apply, curryN, fromPairs, map, pipe, zip } from 'ramda';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Creates a new object out of a list of keys and a list of values by applying the function
|
---|
5 | * to each equally-positioned pair in the lists.
|
---|
6 | * Key/value pairing is truncated to the length of the shorter of the two lists.
|
---|
7 | *
|
---|
8 | * @func zipObjWith
|
---|
9 | * @memberOf RA
|
---|
10 | * @category Object
|
---|
11 | * @since {@link https://char0n.github.io/ramda-adjunct/2.22.0|v2.22.0}
|
---|
12 | * @sig (b, a) -> [k, v] -> [a] -> [b] -> { k: v }
|
---|
13 | * @param {Function} fn The function to transform each value-key pair
|
---|
14 | * @param {Array} keys Array to transform into the properties on the output object
|
---|
15 | * @param {Array} values Array to transform into the values on the output object
|
---|
16 | * @return {Object} The object made by pairing up and transforming same-indexed elements of `keys` and `values`.
|
---|
17 | * @see {@link https://ramdajs.com/docs/#zipObj|zipObj}, {@link RA.unzipObjWith|unzipObjWith}
|
---|
18 | * @example
|
---|
19 | *
|
---|
20 | * RA.zipObjWith((value, key) => [key, `${key}${value + 1}`]), ['a', 'b', 'c'], [1, 2, 3]);
|
---|
21 | * // => { a: 'a2', b: 'b3', c: 'c4' }
|
---|
22 | */
|
---|
23 | const zipObjWith = curryN(3, (fn, keys, values) =>
|
---|
24 | pipe(zip, map(apply(fn)), fromPairs)(values, keys)
|
---|
25 | );
|
---|
26 |
|
---|
27 | export default zipObjWith;
|
---|