merge-change
Version:
Advanced library for deep merging, patching, and immutable updates of data structures. Features declarative operations for specific merging behaviors, property management, custom type merging rules, and difference tracking. Supports complex data transform
57 lines • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.unset = unset;
const type_1 = require("../type");
const split_path_1 = require("../split-path");
function unset(value, path, separator = '.') {
if (value === null || typeof value === 'undefined')
return value;
if (typeof path === 'number')
path = [path];
if (!path)
return value;
if (typeof path === 'string')
return unset(value, (0, split_path_1.splitPath)(path, separator), separator);
const currentPath = path[0];
if (currentPath === '*') {
const t = (0, type_1.type)(value);
// Clearing all object properties
if (t === 'object') {
const source = value;
const keys = Object.keys(source);
for (const key of keys) {
delete source[key];
}
}
else if (t === 'Array') {
const arr = value;
arr.splice(0, arr.length);
}
return value;
}
if (path.length === 1) {
if (Array.isArray(value)) {
const arr = value;
if (typeof currentPath === 'number') {
arr.splice(currentPath, 1);
}
}
else {
const obj = value;
delete obj[currentPath];
}
}
else {
const obj = value;
const nextValue = obj[currentPath];
const t = (0, type_1.type)(nextValue);
if (path[1] === '*' && t !== 'object' && t !== 'Array') {
obj[currentPath] = undefined;
}
else {
return unset(nextValue, path.slice(1), separator);
}
}
return value;
}
//# sourceMappingURL=index.js.map