(object: Record<string, any> | any[], path: string, defaultValue?: unknown)
| 9 | * ``` |
| 10 | */ |
| 11 | export function get(object: Record<string, any> | any[], path: string, defaultValue?: unknown): any { |
| 12 | let key = path.split('.')[0]!; |
| 13 | const follow = path.split('.').slice(1); |
| 14 | |
| 15 | if (key.includes(':')) key = key.split(':')[0]!; |
| 16 | |
| 17 | const result = Array.isArray(object) ? getArrayResult(object, key) : object?.[key]; |
| 18 | |
| 19 | if (result !== undefined && follow.length > 0) { |
| 20 | return get(result, follow.join('.'), defaultValue); |
| 21 | } |
| 22 | |
| 23 | return result ?? defaultValue; |
| 24 | } |
| 25 | |
| 26 | function getArrayResult(object: unknown[], key: string): unknown[] | undefined { |
| 27 | const result = object.map((entry) => entry?.[key as keyof unknown]).filter((entry) => entry); |
no test coverage detected