UNPKG

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