UNPKG

@layerfig/config

Version:

Layer and runtime-validate type-safe configs for JavaScript apps.

38 lines (36 loc) 1.41 kB
//#region src/utils/is-plain-object.ts /** * Type guard to check if value is a plain object */ function isPlainObject(item) { if (item === null || typeof item !== "object") return false; if (item instanceof Date || item instanceof RegExp || item instanceof Error || item instanceof Map || item instanceof Set || item instanceof WeakMap || item instanceof WeakSet || Array.isArray(item)) return false; const proto = Object.getPrototypeOf(item); return proto === null || proto === Object.prototype; } //#endregion //#region src/utils/set.ts /** * Set value at path in object (lodash set replacement) * @param obj - Target object to modify * @param path - Path to set (dot notation string or array of keys) * @param value - Value to set at the path * @returns The modified object */ function set(obj, path, value) { const keys = Array.isArray(path) ? path.filter((key) => key !== "" && key != null) : path.toString().split(".").filter((key) => key !== ""); if (keys.length === 0) return obj; let current = obj; for (let i = 0; i < keys.length - 1; i++) { const key = keys[i]; if (key == null) continue; if (!(key in current) || !isPlainObject(current[key])) current[key] = {}; current = current[key]; } const lastKey = keys[keys.length - 1]; if (lastKey != null) current[lastKey] = value; return obj; } //#endregion export { isPlainObject, set }; //# sourceMappingURL=set-LPILcL1N.js.map