source: node_modules/ramda/src/modifyPath.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: 2.0 KB
Line 
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3.js");
4
5var _isArray =
6/*#__PURE__*/
7require("./internal/_isArray.js");
8
9var _isObject =
10/*#__PURE__*/
11require("./internal/_isObject.js");
12
13var _has =
14/*#__PURE__*/
15require("./internal/_has.js");
16
17var _assoc =
18/*#__PURE__*/
19require("./internal/_assoc.js");
20
21var _modify =
22/*#__PURE__*/
23require("./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
52var 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
82module.exports = modifyPath;
Note: See TracBrowser for help on using the repository browser.