jsoneo
Version:
A powerful JSON enhancement library that supports all JSON primitives, Date, RegExp, Symbol, Functions, Map, Set, TypedArray and much more! Almost everything in JavaScript.
42 lines (40 loc) • 1.26 kB
JavaScript
/**
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is
* returned in its place.
*
* @param obj - The object to query
* @param path - The path of the property to get (accepts strings, numbers, and symbols)
* @param defaultValue - The value returned for undefined resolved values
*
* @returns The resolved value
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getByPath(obj, path, defaultValue) {
// Handle null/undefined objects
if (obj == null) {
return defaultValue;
}
// Handle empty path
if (!path || path.length === 0) {
return obj;
}
let current = obj;
for (const key of path) {
if (current == null) {
return defaultValue;
}
current = current[key];
}
return current === undefined ? defaultValue : current;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getFullKeys(obj) {
if (obj == null) {
return [];
}
return [...Object.getOwnPropertyNames(obj), ...Object.getOwnPropertySymbols(obj)].filter(key => {
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
return descriptor && ('value' in descriptor || descriptor.get);
});
}
//# sourceMappingURL=get.js.map