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