/
dissocPath.js
47 lines (45 loc) · 1.46 KB
/
dissocPath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import _curry2 from './internal/_curry2';
import _isInteger from './internal/_isInteger';
import _isArray from './internal/_isArray';
import assoc from './assoc';
import dissoc from './dissoc';
import remove from './remove';
import update from './update';
/**
* Makes a shallow clone of an object, omitting the property at the given path.
* Note that this copies and flattens prototype properties onto the new object
* as well. All non-primitive properties are copied by reference.
*
* @func
* @memberOf R
* @since v0.11.0
* @category Object
* @typedefn Idx = String | Int
* @sig [Idx] -> {k: v} -> {k: v}
* @param {Array} path The path to the value to omit
* @param {Object} obj The object to clone
* @return {Object} A new object without the property at path
* @see R.assocPath
* @example
*
* R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
*/
var dissocPath = _curry2(function dissocPath(path, obj) {
switch (path.length) {
case 0:
return obj;
case 1:
return _isInteger(path[0]) && _isArray(obj) ? remove(path[0], 1, obj) : dissoc(path[0], obj);
default:
var head = path[0];
var tail = Array.prototype.slice.call(path, 1);
if (obj[head] == null) {
return obj;
} else if (_isInteger(head) && _isArray(obj)) {
return update(head, dissocPath(tail, obj[head]), obj);
} else {
return assoc(head, dissocPath(tail, obj[head]), obj);
}
}
});
export default dissocPath;