@incanta/config
Advanced tools
Comparing version 0.1.7 to 0.1.8
{ | ||
"name": "@incanta/config", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -130,13 +130,44 @@ import merge from "lodash.merge"; | ||
const variableRegex = /\$\{[a-zA-Z\-_0-9.]+\}/g; | ||
const replaceValue = (value: string): void => { | ||
const regexResult = value.matchAll(variableRegex); | ||
for (const match of regexResult) { | ||
const keyToReplace = match[0].slice(2, match[0].length - 1); | ||
const value = this.tryGet<string | number | boolean>(keyToReplace); | ||
if (value !== null) { | ||
obj = obj.replace(match[0], `${value}`); | ||
} | ||
} | ||
}; | ||
// replace all ${value} with the value from the config | ||
if (typeof obj === "string") { | ||
replaceValue(obj); | ||
} else if (typeof obj === "object") { | ||
// walk the object and replace all strings with the value from the config | ||
const walkObject = (curObj: any): void => { | ||
for (const property of Object.keys(curObj)) { | ||
const value = curObj[property]; | ||
if (typeof value === "string") { | ||
replaceValue(value); | ||
} else if (typeof value === "object") { | ||
walkObject(value); | ||
} | ||
} | ||
}; | ||
walkObject(obj); | ||
} | ||
return obj as T; | ||
} | ||
public has(key: string): boolean { | ||
public tryGet<T>(key: string): T | null { | ||
try { | ||
this.get(key); | ||
const value = this.get<T>(key); | ||
return value; | ||
} catch { | ||
return false; | ||
return null; | ||
} | ||
return true; | ||
} | ||
@@ -143,0 +174,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30755
549