ramda-adjunct
Version:
Ramda Adjunct is the most popular and most comprehensive set of utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.
26 lines (25 loc) • 1.04 kB
JavaScript
import { apply, curryN, flip, map, pipe, toPairs, transpose, when } from 'ramda';
import lengthEq from './lengthEq';
/**
* Creates a new list out of the supplied object by applying the function to each key/value pairing.
*
* @func unzipObjWith
* @memberOf RA
* @category Object
* @since {@link https://char0n.github.io/ramda-adjunct/2.22.0|v2.22.0}
* @sig (v, k) => [k, v] -> { k: v } -> [[k], [v]]
* @param {Function} fn The function to transform each value-key pair
* @param {Object} obj Object to unzip
* @return {Array} A pair of tw lists: a list of keys and a list of values
* @see {@link https://ramdajs.com/docs/#zipObj|zipObj}, {@link RA.zipObjWith|zipObjWith}
* @example
*
* RA.unzipObjWith((v, k) => [`new${k.toUpperCase()}`, 2 * v], { a: 1, b: 2, c: 3 });
* //=> [['newA', 'newB', 'newC'], [2, 4, 6]]
*/
var unzipObjWith = curryN(2, function (fn, obj) {
return pipe(toPairs, map(pipe(flip, apply)(fn)), transpose, when(lengthEq(0), function () {
return [[], []];
}))(obj);
});
export default unzipObjWith;