1 | var _curry3 =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_curry3.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 | var _has =
|
---|
14 | /*#__PURE__*/
|
---|
15 | require("./internal/_has.js");
|
---|
16 |
|
---|
17 | var _assoc =
|
---|
18 | /*#__PURE__*/
|
---|
19 | require("./internal/_assoc.js");
|
---|
20 |
|
---|
21 | var _modify =
|
---|
22 | /*#__PURE__*/
|
---|
23 | require("./internal/_modify.js");
|
---|
24 | /**
|
---|
25 | * Creates a shallow clone of the passed object by applying an `fn` function
|
---|
26 | * to the value at the given path.
|
---|
27 | *
|
---|
28 | * The function will not be invoked, and the object will not change
|
---|
29 | * if its corresponding path does not exist in the object.
|
---|
30 | * All non-primitive properties are copied to the new object by reference.
|
---|
31 | *
|
---|
32 | * @func
|
---|
33 | * @memberOf R
|
---|
34 | * @since v0.28.0
|
---|
35 | * @category Object
|
---|
36 | * @sig [Idx] -> (v -> v) -> {k: v} -> {k: v}
|
---|
37 | * @param {Array} path The path to be modified.
|
---|
38 | * @param {Function} fn The function to apply to the path.
|
---|
39 | * @param {Object} object The object to be transformed.
|
---|
40 | * @return {Object} The transformed object.
|
---|
41 | * @example
|
---|
42 | *
|
---|
43 | * const person = {name: 'James', address: { zipCode: '90216' }};
|
---|
44 | * R.modifyPath(['address', 'zipCode'], R.reverse, person); //=> {name: 'James', address: { zipCode: '61209' }}
|
---|
45 | *
|
---|
46 | * // Can handle arrays too
|
---|
47 | * const person = {name: 'James', addresses: [{ zipCode: '90216' }]};
|
---|
48 | * R.modifyPath(['addresses', 0, 'zipCode'], R.reverse, person); //=> {name: 'James', addresses: [{ zipCode: '61209' }]}
|
---|
49 | */
|
---|
50 |
|
---|
51 |
|
---|
52 | var modifyPath =
|
---|
53 | /*#__PURE__*/
|
---|
54 | _curry3(function modifyPath(path, fn, object) {
|
---|
55 | if (!_isObject(object) && !_isArray(object)) {
|
---|
56 | return object;
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (path.length === 0) {
|
---|
60 | return fn(object);
|
---|
61 | }
|
---|
62 |
|
---|
63 | var idx = path[0];
|
---|
64 |
|
---|
65 | if (!_has(idx, object)) {
|
---|
66 | return object;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (path.length === 1) {
|
---|
70 | return _modify(idx, fn, object);
|
---|
71 | }
|
---|
72 |
|
---|
73 | var val = modifyPath(Array.prototype.slice.call(path, 1), fn, object[idx]);
|
---|
74 |
|
---|
75 | if (val === object[idx]) {
|
---|
76 | return object;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return _assoc(idx, val, object);
|
---|
80 | });
|
---|
81 |
|
---|
82 | module.exports = modifyPath; |
---|