@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
24 lines (23 loc) • 862 B
JavaScript
//#region src/utils/get.ts
/**
* Retrieves the value at a specified path within an object.
* Supports dot and bracket notation for navigating nested properties.
* @template T - The type of the object to retrieve the value from.
* @param value - The object to navigate.
* @param path - The path (dot or bracket notation) to the desired value.
* @returns The value at the specified path, or `undefined` if the path does not exist.
*/
function get(value, path) {
const normalized = path.replaceAll(/\[(\w+)\]/g, ".$1");
if (normalized.includes("..") || normalized.startsWith(".") || normalized.endsWith(".")) return;
const segments = normalized.split(".");
let current = value;
for (const key of segments) {
if (current == null) return;
current = current[key];
}
if (current === void 0) return;
return current;
}
//#endregion
export { get as default };