-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.ts
140 lines (114 loc) · 3.07 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*!
* get-value <https://github.com/jonschlinkert/get-value>
*
* Copyright (c) 2014-present, Jon Schlinkert.
* Released under the MIT License.
*/
export interface Options {
default?: unknown;
separator?: string;
joinChar?: string;
join?: (segs: string[]) => string; // eslint-disable-line
split?: (path: string) => string[]; // eslint-disable-line
isValid?: (key: string, target: {}) => boolean; // eslint-disable-line
}
const isObject = v => v !== null && typeof v === 'object';
const join = (
segs: string[],
joinChar: string,
options: unknown
): string => {
if (typeof options.join === 'function') {
return options.join(segs);
}
return segs[0] + joinChar + segs[1];
};
const split = (
path: string,
splitChar: string,
options: unknown
): string[] => {
if (typeof options.split === 'function') {
return options.split(path);
}
return path.split(splitChar);
};
const isValid = (
key: string,
target: unknown = {},
options: unknown
): boolean => {
if (typeof options?.isValid === 'function') {
return options.isValid(key, target);
}
return true;
};
const isValidObject = (v: unknown): boolean => {
return isObject(v) || typeof v === 'function';
};
// eslint-disable-next-line complexity
const getValue = (
target: unknown,
path: string | number | string[],
options: Options = {}
): unknown => {
if (!isObject(options)) {
options = { default: options };
}
if (!isValidObject(target)) {
return typeof options.default !== 'undefined' ? options.default : target;
}
if (typeof path === 'number') {
path = String(path);
}
const pathIsArray = Array.isArray(path);
const pathIsString = typeof path === 'string';
const splitChar = options.separator || '.';
const joinChar = options.joinChar || (typeof splitChar === 'string' ? splitChar : '.');
if (!pathIsString && !pathIsArray) {
return target;
}
if (target[path] !== undefined) {
return isValid(path, target, options) ? target[path] : options.default;
}
const segs = pathIsArray ? path : split(path, splitChar, options);
const len = segs.length;
let idx = 0;
do {
let prop = segs[idx];
if (typeof prop !== 'string') {
prop = String(prop);
}
while (prop && prop.slice(-1) === '\\') {
prop = join([prop.slice(0, -1), segs[++idx] || ''], joinChar, options);
}
if (target[prop] !== undefined) {
if (!isValid(prop, target, options)) {
return options.default;
}
target = target[prop];
} else {
let hasProp = false;
let n = idx + 1;
while (n < len) {
prop = join([prop, segs[n++]], joinChar, options);
if ((hasProp = target[prop] !== undefined)) {
if (!isValid(prop, target, options)) {
return options.default;
}
target = target[prop];
idx = n - 1;
break;
}
}
if (!hasProp) {
return options.default;
}
}
} while (++idx < len && isValidObject(target));
if (idx === len) {
return target;
}
return options.default;
};
export default getValue;