-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
1 addition
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
|
||
export function get(obj: Record<string, any> | undefined, path: string, defValue?: any): any { | ||
// If path is not defined or it has false value | ||
if (!path) return undefined; | ||
// Check if path is string or array. Regex : ensure that we do not have '.' and brackets. | ||
// Regex explained: https://regexr.com/58j0k | ||
const pathArray = path.match(/([^[.\]])+/g); | ||
// Find value | ||
const result = pathArray?.reduce((prevObj, key) => { | ||
return prevObj && prevObj[key]; | ||
}, obj); | ||
// If found value is undefined return default value; otherwise return the value | ||
|
||
return result === undefined ? defValue : result; | ||
} |