source: node_modules/ramda/es/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: 1.9 KB
Line 
1import _curry3 from "./internal/_curry3.js";
2import _isArray from "./internal/_isArray.js";
3import _isObject from "./internal/_isObject.js";
4import _has from "./internal/_has.js";
5import _assoc from "./internal/_assoc.js";
6import _modify from "./internal/_modify.js";
7/**
8 * Creates a shallow clone of the passed object by applying an `fn` function
9 * to the value at the given path.
10 *
11 * The function will not be invoked, and the object will not change
12 * if its corresponding path does not exist in the object.
13 * All non-primitive properties are copied to the new object by reference.
14 *
15 * @func
16 * @memberOf R
17 * @since v0.28.0
18 * @category Object
19 * @sig [Idx] -> (v -> v) -> {k: v} -> {k: v}
20 * @param {Array} path The path to be modified.
21 * @param {Function} fn The function to apply to the path.
22 * @param {Object} object The object to be transformed.
23 * @return {Object} The transformed object.
24 * @example
25 *
26 * const person = {name: 'James', address: { zipCode: '90216' }};
27 * R.modifyPath(['address', 'zipCode'], R.reverse, person); //=> {name: 'James', address: { zipCode: '61209' }}
28 *
29 * // Can handle arrays too
30 * const person = {name: 'James', addresses: [{ zipCode: '90216' }]};
31 * R.modifyPath(['addresses', 0, 'zipCode'], R.reverse, person); //=> {name: 'James', addresses: [{ zipCode: '61209' }]}
32 */
33
34var modifyPath =
35/*#__PURE__*/
36_curry3(function modifyPath(path, fn, object) {
37 if (!_isObject(object) && !_isArray(object)) {
38 return object;
39 }
40
41 if (path.length === 0) {
42 return fn(object);
43 }
44
45 var idx = path[0];
46
47 if (!_has(idx, object)) {
48 return object;
49 }
50
51 if (path.length === 1) {
52 return _modify(idx, fn, object);
53 }
54
55 var val = modifyPath(Array.prototype.slice.call(path, 1), fn, object[idx]);
56
57 if (val === object[idx]) {
58 return object;
59 }
60
61 return _assoc(idx, val, object);
62});
63
64export default modifyPath;
Note: See TracBrowser for help on using the repository browser.