source: node_modules/ramda/src/evolve.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: 1.7 KB
Line 
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2.js");
4
5var _isArray =
6/*#__PURE__*/
7require("./internal/_isArray.js");
8
9var _isObject =
10/*#__PURE__*/
11require("./internal/_isObject.js");
12/**
13 * Creates a new object by recursively evolving a shallow copy of `object`,
14 * according to the `transformation` functions. All non-primitive properties
15 * are copied by reference.
16 *
17 * A `transformation` function will not be invoked if its corresponding key
18 * does not exist in the evolved object.
19 *
20 * @func
21 * @memberOf R
22 * @since v0.9.0
23 * @category Object
24 * @sig {k: (v -> v)} -> {k: v} -> {k: v}
25 * @param {Object} transformations The object specifying transformation functions to apply
26 * to the object.
27 * @param {Object} object The object to be transformed.
28 * @return {Object} The transformed object.
29 * @example
30 *
31 * const tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
32 * const transformations = {
33 * firstName: R.trim,
34 * lastName: R.trim, // Will not get invoked.
35 * data: {elapsed: R.add(1), remaining: R.add(-1)}
36 * };
37 * R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
38 */
39
40
41var evolve =
42/*#__PURE__*/
43_curry2(function evolve(transformations, object) {
44 if (!_isObject(object) && !_isArray(object)) {
45 return object;
46 }
47
48 var result = object instanceof Array ? [] : {};
49 var transformation, key, type;
50
51 for (key in object) {
52 transformation = transformations[key];
53 type = typeof transformation;
54 result[key] = type === 'function' ? transformation(object[key]) : transformation && type === 'object' ? evolve(transformation, object[key]) : object[key];
55 }
56
57 return result;
58});
59
60module.exports = evolve;
Note: See TracBrowser for help on using the repository browser.