1 | import { apply, curryN, flip, map, pipe, toPairs, transpose, when } from 'ramda';
|
---|
2 | import lengthEq from './lengthEq';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Creates a new list out of the supplied object by applying the function to each key/value pairing.
|
---|
6 | *
|
---|
7 | * @func unzipObjWith
|
---|
8 | * @memberOf RA
|
---|
9 | * @category Object
|
---|
10 | * @since {@link https://char0n.github.io/ramda-adjunct/2.22.0|v2.22.0}
|
---|
11 | * @sig (v, k) => [k, v] -> { k: v } -> [[k], [v]]
|
---|
12 | * @param {Function} fn The function to transform each value-key pair
|
---|
13 | * @param {Object} obj Object to unzip
|
---|
14 | * @return {Array} A pair of tw lists: a list of keys and a list of values
|
---|
15 | * @see {@link https://ramdajs.com/docs/#zipObj|zipObj}, {@link RA.zipObjWith|zipObjWith}
|
---|
16 | * @example
|
---|
17 | *
|
---|
18 | * RA.unzipObjWith((v, k) => [`new${k.toUpperCase()}`, 2 * v], { a: 1, b: 2, c: 3 });
|
---|
19 | * //=> [['newA', 'newB', 'newC'], [2, 4, 6]]
|
---|
20 | */
|
---|
21 | var unzipObjWith = curryN(2, function (fn, obj) {
|
---|
22 | return pipe(toPairs, map(pipe(flip, apply)(fn)), transpose, when(lengthEq(0), function () {
|
---|
23 | return [[], []];
|
---|
24 | }))(obj);
|
---|
25 | });
|
---|
26 | export default unzipObjWith; |
---|