UNPKG

read-config-ng

Version:
59 lines 1.76 kB
/** * Pick a value from a nested object using dot notation * @param obj The object to pick from * @param prop The property path (e.g., 'a.b.c' or ['a', 'b', 'c']) * @returns The pick result or null if not found */ export function pick(obj, prop) { const splitted = Array.isArray(prop) ? prop : prop.split('.'); let lastProp = splitted.shift(); let lastObj = obj; if (!lastProp) { return null; } for (const item of splitted) { if (!lastObj || typeof lastObj !== 'object' || Array.isArray(lastObj)) { return null; } lastObj = lastObj[lastProp]; lastProp = item; } if (!lastObj || typeof lastObj !== 'object' || Array.isArray(lastObj)) { return null; } const finalObj = lastObj; const value = finalObj[lastProp]; if (value === undefined) { return null; } return { obj: finalObj, prop: lastProp, value }; } /** * Put a value into a nested object using dot notation * @param obj The object to modify * @param prop The property path (e.g., 'a.b.c' or ['a', 'b', 'c']) * @param value The value to set * @returns The modified object */ export function put(obj, prop, value) { const splitted = Array.isArray(prop) ? prop : prop.split('.'); let lastProp = splitted.shift(); let lastObj = obj; if (!lastProp) { return obj; } for (const item of splitted) { if (lastObj[lastProp] === undefined || typeof lastObj[lastProp] !== 'object' || Array.isArray(lastObj[lastProp])) { lastObj[lastProp] = {}; } lastObj = lastObj[lastProp]; lastProp = item; } lastObj[lastProp] = value; return obj; } //# sourceMappingURL=deep.js.map