[d24f17c] | 1 | var _curry2 =
|
---|
| 2 | /*#__PURE__*/
|
---|
| 3 | require("./internal/_curry2.js");
|
---|
| 4 |
|
---|
| 5 | var _isArray =
|
---|
| 6 | /*#__PURE__*/
|
---|
| 7 | require("./internal/_isArray.js");
|
---|
| 8 |
|
---|
| 9 | var _isObject =
|
---|
| 10 | /*#__PURE__*/
|
---|
| 11 | require("./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 |
|
---|
| 41 | var 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 |
|
---|
| 60 | module.exports = evolve; |
---|