UNPKG

@signaldb/core

Version:

SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in

38 lines (37 loc) 1.45 kB
//#region src/utils/set.ts /** * Sets a value at a specified path within an object. Creates nested structures * (arrays or objects) as needed to set the value at the correct location. Supports * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`. * @template T - The type of the object to modify. * @template K - The type of the value to set. * @param object - The object to modify. The object is mutated directly. * @param path - The path (dot or bracket notation) where the value should be set. * @param value - The value to set at the specified path. * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`). * @returns The modified object. */ function set(object, path, value, deleteIfUndefined = false) { if (object == null) return object; const segments = path.split(/[.[\]]/g); if (segments[0] === "") segments.shift(); if (segments.at(-1) === "") segments.pop(); const apply = (node) => { if (segments.length > 1) { const key = segments.shift(); const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10)); if (node[key] === void 0) node[key] = nextIsNumber ? [] : {}; apply(node[key]); } else { if (deleteIfUndefined && value === void 0) { delete node[segments[0]]; return; } node[segments[0]] = value; } }; apply(object); return object; } //#endregion export { set as default };