@incanta/config
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -16,3 +16,3 @@ export interface IConfigOptions { | ||
getWithParts<T>(keyParts: string[]): T; | ||
has(key: string): boolean; | ||
tryGet<T>(key: string): T | null; | ||
set<T>(key: string, value: T): void; | ||
@@ -19,0 +19,0 @@ setWithParts<T>(keyParts: string[], value: T): void; |
@@ -100,12 +100,42 @@ "use strict"; | ||
} | ||
const variableRegex = /\$\{[a-zA-Z\-_0-9.]+\}/g; | ||
const replaceValue = (value) => { | ||
const regexResult = value.matchAll(variableRegex); | ||
for (const match of regexResult) { | ||
const keyToReplace = match[0].slice(2, match[0].length - 1); | ||
const value = this.tryGet(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) => { | ||
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; | ||
} | ||
has(key) { | ||
tryGet(key) { | ||
try { | ||
this.get(key); | ||
const value = this.get(key); | ||
return value; | ||
} | ||
catch { | ||
return false; | ||
return null; | ||
} | ||
return true; | ||
} | ||
@@ -112,0 +142,0 @@ set(key, value) { |
{ | ||
"name": "@incanta/config", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
33028
579