Last active
December 28, 2021 16:01
-
-
Save CristalT/b025d30eaa5289fcbf5f5616577f8e62 to your computer and use it in GitHub Desktop.
Converts string values into native types for .env values
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
const env = (key, defaultValue = null) => { | |
const value = process.env[key] ?? defaultValue; | |
switch (String(value).toLowerCase()) { | |
case 'true' || '(true)': | |
return true; | |
case 'false' || '(false)': | |
return false; | |
case 'empty' || '(empty)': | |
return ''; | |
case 'null' || '(null)': | |
return null; | |
default: | |
return value; | |
} | |
}; | |
// Use | |
env('APP_ENV_KEY', 'default_value'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment