[d24f17c] | 1 | var _curry3 =
|
---|
| 2 | /*#__PURE__*/
|
---|
| 3 | require("./internal/_curry3.js");
|
---|
| 4 |
|
---|
| 5 | var _has =
|
---|
| 6 | /*#__PURE__*/
|
---|
| 7 | require("./internal/_has.js");
|
---|
| 8 |
|
---|
| 9 | var _isInteger =
|
---|
| 10 | /*#__PURE__*/
|
---|
| 11 | require("./internal/_isInteger.js");
|
---|
| 12 |
|
---|
| 13 | var _assoc =
|
---|
| 14 | /*#__PURE__*/
|
---|
| 15 | require("./internal/_assoc.js");
|
---|
| 16 |
|
---|
| 17 | var isNil =
|
---|
| 18 | /*#__PURE__*/
|
---|
| 19 | require("./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 |
|
---|
| 46 | var 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 |
|
---|
| 63 | module.exports = assocPath; |
---|