utils/ts-src/get.ts
Martin Donnelly b0e843fd3e Added get function
Bumped to version 2.0.2
2020-11-17 16:31:00 +00:00

25 lines
656 B
TypeScript

/**
*
* @param obj
* @param path
* @param defaultValue
*/
export function get(obj: Object, path: string, defaultValue: any = undefined): any {
const travel = (regexp: any) => {
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;
}