UNPKG

@wordpress/global-styles-ui

Version:
32 lines (31 loc) • 1.08 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // packages/global-styles-ui/src/font-library/utils/set-immutably.ts /** * Immutably sets a value inside an object. Like `lodash#set`, but returning a * new object. Treats nullish initial values as empty objects. Clones any * nested objects. Supports arrays, too. * Duplicated from `packages/global-styles-engine/src/utils/object.ts` * * @param object Object to set a value in. * @param path Path in the object to modify. * @param value New value to set. * @return Cloned object with the new value set. */ function setImmutably(object, path, value) { path = Array.isArray(path) ? [...path] : [path]; object = Array.isArray(object) ? [...object] : { ...object }; const leaf = path.pop(); let prev = object; for (const key of path) { const lvl = prev[key]; prev = prev[key] = Array.isArray(lvl) ? [...lvl] : { ...lvl }; } prev[leaf] = value; return object; } exports.setImmutably = setImmutably; //# sourceMappingURL=set-immutably.cjs.map