main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | import {
|
---|
2 | apply,
|
---|
3 | curryN,
|
---|
4 | flip,
|
---|
5 | map,
|
---|
6 | pipe,
|
---|
7 | toPairs,
|
---|
8 | transpose,
|
---|
9 | when,
|
---|
10 | } from 'ramda';
|
---|
11 |
|
---|
12 | import lengthEq from './lengthEq';
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Creates a new list out of the supplied object by applying the function to each key/value pairing.
|
---|
16 | *
|
---|
17 | * @func unzipObjWith
|
---|
18 | * @memberOf RA
|
---|
19 | * @category Object
|
---|
20 | * @since {@link https://char0n.github.io/ramda-adjunct/2.22.0|v2.22.0}
|
---|
21 | * @sig (v, k) => [k, v] -> { k: v } -> [[k], [v]]
|
---|
22 | * @param {Function} fn The function to transform each value-key pair
|
---|
23 | * @param {Object} obj Object to unzip
|
---|
24 | * @return {Array} A pair of tw lists: a list of keys and a list of values
|
---|
25 | * @see {@link https://ramdajs.com/docs/#zipObj|zipObj}, {@link RA.zipObjWith|zipObjWith}
|
---|
26 | * @example
|
---|
27 | *
|
---|
28 | * RA.unzipObjWith((v, k) => [`new${k.toUpperCase()}`, 2 * v], { a: 1, b: 2, c: 3 });
|
---|
29 | * //=> [['newA', 'newB', 'newC'], [2, 4, 6]]
|
---|
30 | */
|
---|
31 | const unzipObjWith = curryN(2, (fn, obj) =>
|
---|
32 | pipe(
|
---|
33 | toPairs,
|
---|
34 | map(pipe(flip, apply)(fn)),
|
---|
35 | transpose,
|
---|
36 | when(lengthEq(0), () => [[], []])
|
---|
37 | )(obj)
|
---|
38 | );
|
---|
39 |
|
---|
40 | export default unzipObjWith;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.