1 | import _curry2 from "./internal/_curry2.js";
|
---|
2 | import _dissoc from "./internal/_dissoc.js";
|
---|
3 | import _isInteger from "./internal/_isInteger.js";
|
---|
4 | import _isArray from "./internal/_isArray.js";
|
---|
5 | import assoc from "./assoc.js";
|
---|
6 | /**
|
---|
7 | * Makes a shallow clone of an object. Note that this copies and flattens
|
---|
8 | * prototype properties onto the new object as well. All non-primitive
|
---|
9 | * properties are copied by reference.
|
---|
10 | *
|
---|
11 | * @private
|
---|
12 | * @param {String|Integer} prop The prop operating
|
---|
13 | * @param {Object|Array} obj The object to clone
|
---|
14 | * @return {Object|Array} A new object equivalent to the original.
|
---|
15 | */
|
---|
16 |
|
---|
17 | function _shallowCloneObject(prop, obj) {
|
---|
18 | if (_isInteger(prop) && _isArray(obj)) {
|
---|
19 | return [].concat(obj);
|
---|
20 | }
|
---|
21 |
|
---|
22 | var result = {};
|
---|
23 |
|
---|
24 | for (var p in obj) {
|
---|
25 | result[p] = obj[p];
|
---|
26 | }
|
---|
27 |
|
---|
28 | return result;
|
---|
29 | }
|
---|
30 | /**
|
---|
31 | * Makes a shallow clone of an object, omitting the property at the given path.
|
---|
32 | * Note that this copies and flattens prototype properties onto the new object
|
---|
33 | * as well. All non-primitive properties are copied by reference.
|
---|
34 | *
|
---|
35 | * @func
|
---|
36 | * @memberOf R
|
---|
37 | * @since v0.11.0
|
---|
38 | * @category Object
|
---|
39 | * @typedefn Idx = String | Int | Symbol
|
---|
40 | * @sig [Idx] -> {k: v} -> {k: v}
|
---|
41 | * @param {Array} path The path to the value to omit
|
---|
42 | * @param {Object} obj The object to clone
|
---|
43 | * @return {Object} A new object without the property at path
|
---|
44 | * @see R.assocPath
|
---|
45 | * @example
|
---|
46 | *
|
---|
47 | * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
|
---|
48 | */
|
---|
49 |
|
---|
50 |
|
---|
51 | var dissocPath =
|
---|
52 | /*#__PURE__*/
|
---|
53 | _curry2(function dissocPath(path, obj) {
|
---|
54 | if (obj == null) {
|
---|
55 | return obj;
|
---|
56 | }
|
---|
57 |
|
---|
58 | switch (path.length) {
|
---|
59 | case 0:
|
---|
60 | return obj;
|
---|
61 |
|
---|
62 | case 1:
|
---|
63 | return _dissoc(path[0], obj);
|
---|
64 |
|
---|
65 | default:
|
---|
66 | var head = path[0];
|
---|
67 | var tail = Array.prototype.slice.call(path, 1);
|
---|
68 |
|
---|
69 | if (obj[head] == null) {
|
---|
70 | return _shallowCloneObject(head, obj);
|
---|
71 | } else {
|
---|
72 | return assoc(head, dissocPath(tail, obj[head]), obj);
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 | });
|
---|
77 |
|
---|
78 | export default dissocPath; |
---|