Last active
May 10, 2022 08:54
-
-
Save joseluisq/025ffbea51fe042faf8f4c788d0d1d51 to your computer and use it in GitHub Desktop.
Get a Typescript typed environment variable value from `process.env` (NodeJS) with type casting for booleans, strings and numbers
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
/** | |
* Return an environment variable typed value from `process.env`. | |
* It also supports a default value in case of `undefined`. | |
* | |
* The function will follow these steps to take a typed value: | |
* 1. First it will try to get the env value from `process.env`. | |
* 2. Then if `undefined` then it try with the default environment variable value if defined. | |
* 3. Otherwise if the default value is `undefined` then it will return the Javascript default values: | |
* - Boolean: `false` | |
* - String: `""` | |
* - Number: `0` | |
* | |
* @param name string Environment variable name. | |
* @param defaults an Optional default environment variable value when not found. | |
* @returns env Typed value of the environment variable. | |
*/ | |
function env (name: string, defaults?: string): string | |
function env (name: string, defaults?: boolean): boolean | |
function env (name: string, defaults?: number): number | |
function env (name: string, defaults?: string | boolean | number): string | boolean | number { | |
const val: string = typeof process === "undefined" ? "" : (process.env && process.env[name] || "") | |
switch (typeof defaults) { | |
case "number": { | |
if (!name || val.length === 0) return defaults || 0 | |
return Number.isNaN(val) ? (defaults || 0) : Number(val) | |
} | |
case "boolean": | |
switch (val) { | |
case "0": | |
case "false": | |
case "FALSE": | |
return false | |
case "1": | |
case "true": | |
case "TRUE": | |
return true | |
default: { | |
return defaults || false | |
} | |
} | |
case "string": | |
default: | |
if (!name) return defaults || "" | |
return val || defaults || "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: See TypeScript Playground