Skip to content

Instantly share code, notes, and snippets.

@CristalT
Last active December 28, 2021 16:01
Show Gist options
  • Save CristalT/b025d30eaa5289fcbf5f5616577f8e62 to your computer and use it in GitHub Desktop.
Save CristalT/b025d30eaa5289fcbf5f5616577f8e62 to your computer and use it in GitHub Desktop.
Converts string values into native types for .env values
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