-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget.ts
42 lines (39 loc) · 1.45 KB
/
get.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable @typescript-eslint/ban-types */
import { toPath } from '../util/toPath';
type PropertyName = string | number | symbol;
type Many<T> = T | T[];
type PropertyPath = Many<PropertyName>;
function get<T extends object, K extends keyof T>(object: T, path: K | [K]): T[K];
function get<T extends object, K extends keyof T, Default>(
object: T,
path: K | [K],
defaultValue: Default
): Exclude<T[K], undefined> | Default;
function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1]>(object: T, path: [K1, K2]): T[K1][K2];
function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1], Default>(
object: T,
path: [K1, K2],
defaultValue: Default
): Exclude<T[K1][K2], undefined> | Default;
function get<K extends PropertyName, V>(object: Record<K, V>, path: Many<K>): V;
function get<K extends PropertyName, V, Default>(
object: Record<K, V>,
path: Many<K>,
defaultValue: Default
): Exclude<V, undefined> | Default;
function get(object: any, path: PropertyPath, defaultValue?: any): any;
function get(object: any, path: PropertyPath, defaultValue?: any): any {
if (object === undefined) {
return defaultValue;
}
const formattedPath = toPath(path);
let result = object;
for (let i = 0; i < formattedPath.length; i++) {
result = result[formattedPath[i]];
if (result === undefined) {
return defaultValue;
}
}
return result;
}
export { get };