-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEphemeralConfig.js
40 lines (38 loc) · 1.06 KB
/
EphemeralConfig.js
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
export default class EphemeralConfig {
constructor(object, path) {
const self = this;
this.object = object;
this.path = path;
if (this.object) {
Object.assign(self, this.object);
}
}
get(path, defaultValue) {
if (!(typeof this.object?.[path] === 'undefined')) {
return this.object?.[path];
}
const pathSteps = path?.split('.') || [];
let root = this.object;
for (let i = 0; i < pathSteps.length && root !== null && root !== undefined; i++) {
root = root?.[pathSteps[i]];
}
if (root) {
return root;
}
if ((typeof defaultValue !== 'undefined')) {
return defaultValue;
}
throw new Error(`Config path ${path} returned no value.`);
}
has(path) {
if (!(typeof this.object?.[path] === 'undefined')) {
return true;
}
const pathSteps = path?.split('.') || [];
let root = this.object;
for (let i = 0; i < pathSteps.length && root !== null && root !== undefined; i++) {
root = root?.[pathSteps[i]];
}
return root !== null && root !== undefined;
}
}