-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node-config-provider): add utility booleanSelector (#2941)
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { booleanSelector, SelectorType } from "./booleanSelector"; | ||
|
||
describe(booleanSelector.name, () => { | ||
const key = "key"; | ||
const obj: { [key]: any } = {} as any; | ||
|
||
describe.each(Object.entries(SelectorType))(`Selector %s`, (selectorKey, selectorValue) => { | ||
beforeEach(() => { | ||
delete obj[key]; | ||
}); | ||
|
||
it(`should return undefined if ${key} is not defined`, () => { | ||
expect(booleanSelector(obj, key, SelectorType[selectorKey])).toBeUndefined(); | ||
}); | ||
|
||
it.each([ | ||
[true, "true"], | ||
[false, "false"], | ||
])(`should return boolean %s if ${key}="%s"`, (output, input) => { | ||
obj[key] = input; | ||
expect(booleanSelector(obj, key, SelectorType[selectorKey])).toBe(output); | ||
}); | ||
|
||
it.each(["0", "1", "yes", "no", undefined, null, void 0, ""])(`should throw if ${key}=%s`, (input) => { | ||
obj[key] = input; | ||
expect(() => booleanSelector(obj, key, SelectorType[selectorKey])).toThrow( | ||
`Cannot load ${selectorValue} "${key}". Expected "true" or "false", got ${obj[key]}.` | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export enum SelectorType { | ||
ENV = "env", | ||
CONFIG = "shared config entry", | ||
} | ||
|
||
/** | ||
* Returns boolean value true/false for string value "true"/"false", | ||
* if the string is defined in obj[key] | ||
* Returns undefined, if obj[key] is not defined. | ||
* Throws error for all other cases. | ||
* | ||
* @internal | ||
*/ | ||
export const booleanSelector = (obj: { [key: string]: string }, key: string, type: SelectorType) => { | ||
if (!(key in obj)) return undefined; | ||
if (obj[key] === "true") return true; | ||
if (obj[key] === "false") return false; | ||
throw new Error(`Cannot load ${type} "${key}". Expected "true" or "false", got ${obj[key]}.`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./booleanSelector"; | ||
export * from "./configLoader"; |