UNPKG

2.06 kBJavaScriptView Raw
1var _curry1 =
2/*#__PURE__*/
3require("./internal/_curry1");
4
5var apply =
6/*#__PURE__*/
7require("./apply");
8
9var curryN =
10/*#__PURE__*/
11require("./curryN");
12
13var max =
14/*#__PURE__*/
15require("./max");
16
17var pluck =
18/*#__PURE__*/
19require("./pluck");
20
21var reduce =
22/*#__PURE__*/
23require("./reduce");
24
25var keys =
26/*#__PURE__*/
27require("./keys");
28
29var values =
30/*#__PURE__*/
31require("./values"); // Use custom mapValues function to avoid issues with specs that include a "map" key and R.map
32// delegating calls to .map
33
34
35function mapValues(fn, obj) {
36 return keys(obj).reduce(function (acc, key) {
37 acc[key] = fn(obj[key]);
38 return acc;
39 }, {});
40}
41/**
42 * Given a spec object recursively mapping properties to functions, creates a
43 * function producing an object of the same structure, by mapping each property
44 * to the result of calling its associated function with the supplied arguments.
45 *
46 * @func
47 * @memberOf R
48 * @since v0.20.0
49 * @category Function
50 * @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
51 * @param {Object} spec an object recursively mapping properties to functions for
52 * producing the values for these properties.
53 * @return {Function} A function that returns an object of the same structure
54 * as `spec', with each property set to the value returned by calling its
55 * associated function with the supplied arguments.
56 * @see R.converge, R.juxt
57 * @example
58 *
59 * const getMetrics = R.applySpec({
60 * sum: R.add,
61 * nested: { mul: R.multiply }
62 * });
63 * getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
64 * @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
65 */
66
67
68var applySpec =
69/*#__PURE__*/
70_curry1(function applySpec(spec) {
71 spec = mapValues(function (v) {
72 return typeof v == 'function' ? v : applySpec(v);
73 }, spec);
74 return curryN(reduce(max, 0, pluck('length', values(spec))), function () {
75 var args = arguments;
76 return mapValues(function (f) {
77 return apply(f, args);
78 }, spec);
79 });
80});
81
82module.exports = applySpec;
\No newline at end of file