UNPKG

1.58 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Creates a new object by recursively evolving a shallow copy of `object`,
6 * according to the `transformation` functions. All non-primitive properties
7 * are copied by reference.
8 *
9 * A `transformation` function will not be invoked if its corresponding key
10 * does not exist in the evolved object.
11 *
12 * @func
13 * @memberOf R
14 * @since v0.9.0
15 * @category Object
16 * @sig {k: (v -> v)} -> {k: v} -> {k: v}
17 * @param {Object} transformations The object specifying transformation functions to apply
18 * to the object.
19 * @param {Object} object The object to be transformed.
20 * @return {Object} The transformed object.
21 * @example
22 *
23 * const tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
24 * const transformations = {
25 * firstName: R.trim,
26 * lastName: R.trim, // Will not get invoked.
27 * data: {elapsed: R.add(1), remaining: R.add(-1)}
28 * };
29 * R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
30 */
31
32
33var evolve =
34/*#__PURE__*/
35_curry2(function evolve(transformations, object) {
36 var result = object instanceof Array ? [] : {};
37 var transformation, key, type;
38
39 for (key in object) {
40 transformation = transformations[key];
41 type = typeof transformation;
42 result[key] = type === 'function' ? transformation(object[key]) : transformation && type === 'object' ? evolve(transformation, object[key]) : object[key];
43 }
44
45 return result;
46});
47
48module.exports = evolve;
\No newline at end of file