78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
import _curry2 from "./internal/_curry2.js";
|
|
import _dissoc from "./internal/_dissoc.js";
|
|
import _isInteger from "./internal/_isInteger.js";
|
|
import _isArray from "./internal/_isArray.js";
|
|
import assoc from "./assoc.js";
|
|
/**
|
|
* Makes a shallow clone of an object. Note that this copies and flattens
|
|
* prototype properties onto the new object as well. All non-primitive
|
|
* properties are copied by reference.
|
|
*
|
|
* @private
|
|
* @param {String|Integer} prop The prop operating
|
|
* @param {Object|Array} obj The object to clone
|
|
* @return {Object|Array} A new object equivalent to the original.
|
|
*/
|
|
|
|
function _shallowCloneObject(prop, obj) {
|
|
if (_isInteger(prop) && _isArray(obj)) {
|
|
return [].concat(obj);
|
|
}
|
|
|
|
var result = {};
|
|
|
|
for (var p in obj) {
|
|
result[p] = obj[p];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/**
|
|
* 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 | Symbol
|
|
* @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 =
|
|
/*#__PURE__*/
|
|
_curry2(function dissocPath(path, obj) {
|
|
if (obj == null) {
|
|
return obj;
|
|
}
|
|
|
|
switch (path.length) {
|
|
case 0:
|
|
return obj;
|
|
|
|
case 1:
|
|
return _dissoc(path[0], obj);
|
|
|
|
default:
|
|
var head = path[0];
|
|
var tail = Array.prototype.slice.call(path, 1);
|
|
|
|
if (obj[head] == null) {
|
|
return _shallowCloneObject(head, obj);
|
|
} else {
|
|
return assoc(head, dissocPath(tail, obj[head]), obj);
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
export default dissocPath; |