source: node_modules/ramda/src/assocPath.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.6 KB
Line 
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3.js");
4
5var _has =
6/*#__PURE__*/
7require("./internal/_has.js");
8
9var _isInteger =
10/*#__PURE__*/
11require("./internal/_isInteger.js");
12
13var _assoc =
14/*#__PURE__*/
15require("./internal/_assoc.js");
16
17var isNil =
18/*#__PURE__*/
19require("./isNil.js");
20/**
21 * Makes a shallow clone of an object, setting or overriding the nodes required
22 * to create the given path, and placing the specific value at the tail end of
23 * that path. Note that this copies and flattens prototype properties onto the
24 * new object as well. All non-primitive properties are copied by reference.
25 *
26 * @func
27 * @memberOf R
28 * @since v0.8.0
29 * @category Object
30 * @typedefn Idx = String | Int | Symbol
31 * @sig [Idx] -> a -> {a} -> {a}
32 * @param {Array} path the path to set
33 * @param {*} val The new value
34 * @param {Object} obj The object to clone
35 * @return {Object} A new object equivalent to the original except along the specified path.
36 * @see R.dissocPath
37 * @example
38 *
39 * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
40 *
41 * // Any missing or non-object keys in path will be overridden
42 * R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}
43 */
44
45
46var assocPath =
47/*#__PURE__*/
48_curry3(function assocPath(path, val, obj) {
49 if (path.length === 0) {
50 return val;
51 }
52
53 var idx = path[0];
54
55 if (path.length > 1) {
56 var nextObj = !isNil(obj) && _has(idx, obj) && typeof obj[idx] === 'object' ? obj[idx] : _isInteger(path[1]) ? [] : {};
57 val = assocPath(Array.prototype.slice.call(path, 1), val, nextObj);
58 }
59
60 return _assoc(idx, val, obj);
61});
62
63module.exports = assocPath;
Note: See TracBrowser for help on using the repository browser.