utils/ts-src/get.ts

29 lines
825 B
TypeScript

/**
* Get the value of an item inside an object
* @param obj The source object
* @param path The path to the object
* @param defaultValue A default value to be returned
*
* @example
* U.get({ a: 1, b: 2 }, 'b') // => 2
*/
export function get(obj: object, path: string, defaultValue: unknown = undefined): unknown {
const travel = (regexp: RegExp) => {
return String.prototype.split
.call(path, regexp)
.filter(Boolean)
.reduce((res, key) => {
if (res !== null && res !== undefined && res.hasOwnProperty(key)) {
const { value }: any = Object.getOwnPropertyDescriptor(res, key);
return value;
} else
return res;
}, obj);
};
const result = travel(/[,[\].]+?/);
return result === undefined || result === obj ? defaultValue : result;
}