[d24f17c] | 1 | import _curry2 from "./internal/_curry2.js";
|
---|
| 2 | import _isArray from "./internal/_isArray.js";
|
---|
| 3 | import _isObject from "./internal/_isObject.js";
|
---|
| 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 | var evolve =
|
---|
| 33 | /*#__PURE__*/
|
---|
| 34 | _curry2(function evolve(transformations, object) {
|
---|
| 35 | if (!_isObject(object) && !_isArray(object)) {
|
---|
| 36 | return object;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | var result = object instanceof Array ? [] : {};
|
---|
| 40 | var transformation, key, type;
|
---|
| 41 |
|
---|
| 42 | for (key in object) {
|
---|
| 43 | transformation = transformations[key];
|
---|
| 44 | type = typeof transformation;
|
---|
| 45 | result[key] = type === 'function' ? transformation(object[key]) : transformation && type === 'object' ? evolve(transformation, object[key]) : object[key];
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | return result;
|
---|
| 49 | });
|
---|
| 50 |
|
---|
| 51 | export default evolve; |
---|