source: node_modules/ramda/es/applySpec.js

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: 2.0 KB
Line 
1import _curry1 from "./internal/_curry1.js";
2import _isArray from "./internal/_isArray.js";
3import apply from "./apply.js";
4import curryN from "./curryN.js";
5import max from "./max.js";
6import pluck from "./pluck.js";
7import reduce from "./reduce.js";
8import keys from "./keys.js";
9import values from "./values.js"; // Use custom mapValues function to avoid issues with specs that include a "map" key and R.map
10// delegating calls to .map
11
12function mapValues(fn, obj) {
13 return _isArray(obj) ? obj.map(fn) : keys(obj).reduce(function (acc, key) {
14 acc[key] = fn(obj[key]);
15 return acc;
16 }, {});
17}
18/**
19 * Given a spec object recursively mapping properties to functions, creates a
20 * function producing an object of the same structure, by mapping each property
21 * to the result of calling its associated function with the supplied arguments.
22 *
23 * @func
24 * @memberOf R
25 * @since v0.20.0
26 * @category Function
27 * @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
28 * @param {Object} spec an object recursively mapping properties to functions for
29 * producing the values for these properties.
30 * @return {Function} A function that returns an object of the same structure
31 * as `spec', with each property set to the value returned by calling its
32 * associated function with the supplied arguments.
33 * @see R.converge, R.juxt
34 * @example
35 *
36 * const getMetrics = R.applySpec({
37 * sum: R.add,
38 * nested: { mul: R.multiply }
39 * });
40 * getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
41 * @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
42 */
43
44
45var applySpec =
46/*#__PURE__*/
47_curry1(function applySpec(spec) {
48 spec = mapValues(function (v) {
49 return typeof v == 'function' ? v : applySpec(v);
50 }, spec);
51 return curryN(reduce(max, 0, pluck('length', values(spec))), function () {
52 var args = arguments;
53 return mapValues(function (f) {
54 return apply(f, args);
55 }, spec);
56 });
57});
58
59export default applySpec;
Note: See TracBrowser for help on using the repository browser.