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