[d24f17c] | 1 | import _curry1 from "./internal/_curry1.js";
|
---|
| 2 | import _isArray from "./internal/_isArray.js";
|
---|
| 3 | import apply from "./apply.js";
|
---|
| 4 | import curryN from "./curryN.js";
|
---|
| 5 | import max from "./max.js";
|
---|
| 6 | import pluck from "./pluck.js";
|
---|
| 7 | import reduce from "./reduce.js";
|
---|
| 8 | import keys from "./keys.js";
|
---|
| 9 | import 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 |
|
---|
| 12 | function 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 |
|
---|
| 45 | var 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 |
|
---|
| 59 | export default applySpec; |
---|