@incanta/config 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/config.ts +36 -5
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -127,17 +127,48 @@ export class Config {
|
|
|
127
127
|
obj = obj[part];
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
const variableRegex = /\$\{[a-zA-Z\-_0-9.]+\}/g;
|
|
131
|
+
|
|
132
|
+
const replaceValue = (value: string): void => {
|
|
133
|
+
const regexResult = value.matchAll(variableRegex);
|
|
134
|
+
for (const match of regexResult) {
|
|
135
|
+
const keyToReplace = match[0].slice(2, match[0].length - 1);
|
|
136
|
+
const value = this.tryGet<string | number | boolean>(keyToReplace);
|
|
137
|
+
if (value !== null) {
|
|
138
|
+
obj = obj.replace(match[0], `${value}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// replace all ${value} with the value from the config
|
|
144
|
+
if (typeof obj === "string") {
|
|
145
|
+
replaceValue(obj);
|
|
146
|
+
} else if (typeof obj === "object") {
|
|
147
|
+
// walk the object and replace all strings with the value from the config
|
|
148
|
+
const walkObject = (curObj: any): void => {
|
|
149
|
+
for (const property of Object.keys(curObj)) {
|
|
150
|
+
const value = curObj[property];
|
|
151
|
+
if (typeof value === "string") {
|
|
152
|
+
replaceValue(value);
|
|
153
|
+
} else if (typeof value === "object") {
|
|
154
|
+
walkObject(value);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
walkObject(obj);
|
|
160
|
+
}
|
|
161
|
+
|
|
130
162
|
return obj as T;
|
|
131
163
|
}
|
|
132
164
|
|
|
133
|
-
public
|
|
165
|
+
public tryGet<T>(key: string): T | null {
|
|
134
166
|
try {
|
|
135
|
-
this.get(key);
|
|
167
|
+
const value = this.get<T>(key);
|
|
168
|
+
return value;
|
|
136
169
|
} catch {
|
|
137
|
-
return
|
|
170
|
+
return null;
|
|
138
171
|
}
|
|
139
|
-
|
|
140
|
-
return true;
|
|
141
172
|
}
|
|
142
173
|
|
|
143
174
|
public set<T>(key: string, value: T): void {
|