-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
90 additions
and
22 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
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
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
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
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
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
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
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
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
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
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
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
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,28 @@ | ||
import { envToOptionalBoolean } from './utils.js'; | ||
|
||
const TEST_KEY: string = 'CONFIG_UTIL_TEST_KEY'; | ||
|
||
describe('Config Utils', () => { | ||
describe('envToOptionalBoolean', () => { | ||
it.each([ | ||
['', undefined], | ||
['true', true], | ||
['TRUE', true], | ||
['false', false], | ||
['FALSE', false], | ||
])( | ||
'when environment variable is "%s", should return %s', | ||
(input: string | undefined, expected: boolean | undefined) => { | ||
process.env[TEST_KEY] = input; | ||
|
||
expect(envToOptionalBoolean(TEST_KEY)).toBe(expected); | ||
}, | ||
); | ||
|
||
it('should throw error, if the environment variable is set to an invalid string', () => { | ||
process.env[TEST_KEY] = 'INVALID'; | ||
|
||
expect(() => envToOptionalBoolean(TEST_KEY)).toThrow(); | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
/** | ||
* Reads the environment variable and returns an optional boolean. | ||
* Depending on the input: | ||
* - undefined or empty string -> undefined | ||
* - "true" (case insensitive) -> true | ||
* - "false" (case insensitive) -> false | ||
* - any other string -> throws error | ||
* | ||
* @param key The name of the environment variable | ||
*/ | ||
export function envToOptionalBoolean(key: string): boolean | undefined { | ||
const value: string | undefined = process.env[key]; | ||
|
||
if (!value) { | ||
return undefined; | ||
} | ||
|
||
const lower: string | undefined = value.toLowerCase(); | ||
|
||
switch (lower) { | ||
case 'true': | ||
return true; | ||
case 'false': | ||
return false; | ||
default: | ||
throw new Error(`Expected environment variable "${key}" to be "true" or "false", received "${value}".`); | ||
} | ||
} |
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