|
1
|
import { isDefined } from "./is-defined" |
|
2
|
import { isObject } from "./is-object" |
|
3
|
|
|
4
|
export function pickValueFromObject( |
|
5
|
path: string, |
|
6
|
object: Record<any, any> |
|
7
|
): any { |
|
8
|
const segments = path.split(".") |
|
9
|
let result: any = object |
|
10
|
|
|
11
|
for (let i = 0; i < segments.length; i++) { |
|
12
|
const segment = segments[i] |
|
13
|
result = result[segment] |
|
14
|
|
|
15
|
if (!isDefined(result)) { |
|
16
|
return |
|
17
|
} |
|
18
|
|
|
19
|
if (i === segments.length - 1) { |
|
20
|
return result |
|
21
|
} |
|
22
|
|
|
23
|
if (Array.isArray(result)) { |
|
24
|
const subPath = segments.slice(i + 1).join(".") |
|
25
|
return result.map((item) => pickValueFromObject(subPath, item)).flat() |
|
26
|
} |
|
27
|
|
|
28
|
if (!isObject(result)) { |
|
29
|
return result |
|
30
|
} |
|
31
|
} |
|
32
|
|
|
33
|
return result |
|
34
|
} |