@modern-kit/utils
Version:
32 lines (28 loc) • 1.1 kB
JavaScript
;
var commonCloneDeep = require('../../common/cloneDeep/index.cjs');
var validatorHasProperty = require('../../validator/hasProperty/index.cjs');
var validatorIsNil = require('../../validator/isNil/index.cjs');
var objectGet = require('../get/index.cjs');
function set(obj, path, value, options = {}) {
const immutable = options?.immutable ?? false;
const clonedObj = immutable ? commonCloneDeep.cloneDeep(obj) : obj;
const resolvedPath = path.replace(/\?/g, "");
const paths = resolvedPath.split(".");
let current = clonedObj;
for (let i = 0; i < paths.length - 1; i++) {
const currentPath = paths[i];
if (!validatorHasProperty.hasProperty(current, currentPath) || validatorIsNil.isNil(current[currentPath])) {
current[currentPath] = {};
}
current = current[currentPath];
}
const lastPath = paths[paths.length - 1];
if (typeof value === "function") {
current[lastPath] = value(objectGet.get(clonedObj, resolvedPath));
} else {
current[lastPath] = value;
}
return clonedObj;
}
exports.set = set;
//# sourceMappingURL=index.cjs.map