You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
44 lines
1.3 KiB
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
import _toArray from "@babel/runtime/helpers/esm/toArray";
|
|
import get from './get';
|
|
|
|
function internalSet(entity, paths, value, removeIfUndefined) {
|
|
if (!paths.length) {
|
|
return value;
|
|
}
|
|
|
|
var _paths = _toArray(paths),
|
|
path = _paths[0],
|
|
restPath = _paths.slice(1);
|
|
|
|
var clone;
|
|
|
|
if (!entity && typeof path === 'number') {
|
|
clone = [];
|
|
} else if (Array.isArray(entity)) {
|
|
clone = _toConsumableArray(entity);
|
|
} else {
|
|
clone = _objectSpread({}, entity);
|
|
} // Delete prop if `removeIfUndefined` and value is undefined
|
|
|
|
|
|
if (removeIfUndefined && value === undefined && restPath.length === 1) {
|
|
delete clone[path][restPath[0]];
|
|
} else {
|
|
clone[path] = internalSet(clone[path], restPath, value, removeIfUndefined);
|
|
}
|
|
|
|
return clone;
|
|
}
|
|
|
|
export default function set(entity, paths, value) {
|
|
var removeIfUndefined = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
|
|
// Do nothing if `removeIfUndefined` and parent object not exist
|
|
if (paths.length && removeIfUndefined && value === undefined && !get(entity, paths.slice(0, -1))) {
|
|
return entity;
|
|
}
|
|
|
|
return internalSet(entity, paths, value, removeIfUndefined);
|
|
} |