dotenv-utils
Covert env var strings to booleans, numbers, arrays, and objects.
Context: Environment variables are a great way to separate config from code. Tools like dotenv
make this almost perfect but...
The problem: Environment variables are always strings. If you set a variable MINIFY=false
and try to check it using if (process.env.MINIFY) { ...some logic }
, then "...some logic" will actually be executed as non-empty strings are truthy.
The quick solution is to write process.env.MINIFY === "true"
instead, but as you get more vars and more var "types", these ad-hoc solutions become tedious, unclear, and error-prone.
The solution: dotenv-utils
provides several helpers to make sure you never have to worry about this again.
Conversion functions always return the right type. That way, you can safely call methods without worrying about getting that Uncaught Type Error: undefined is not a function
fun.
Install
npm install --save dotenv-utils
yarn add dotenv-utils
Tested on Node.js v6.9.2, likely runs on earlier versions too.
API
boolean
Converts a string representation (case-insensitive) of a boolean to an actual boolean.
const {boolean} = require("dotenv-utils")
boolean("true")
boolean("TRUE")
boolean("false")
boolean("foo")
boolean("")
boolean(undefined)
number
Converts a string representation of a number to an actual number. Basically like Number(x)
, but will return a 0
instead of NaN
when string cannot be converted to a number.
const {number} = require("dotenv-utils")
number("123")
number(" 123 ")
number("foo")
number(undefined)
string
Trims the supplied string. If provided a falsy value, returns ""
. This is mainly useful when used in conjunction with the conform
helper.
const {string} = require("dotenv-utils")
string("foo")
string(" foo ")
string("")
string(undefined)
array
Converts a string of comma-separated values ("foo, bar, baz"
) to an array. Any extra whitespace will be trimmed and empty strings discarded.
const {array} = require("dotenv-utils")
array("foo, bar, baz")
array("foo, bar, baz")
array(",,,")
array("")
array(undefined)
object
Converts a string of comma-separated tuples ("foo: bar, baz: quux"
) to an object. Any extra whitespace from either key or value will be discarded, as are tuples with falsy keys.
const {object} = require("dotenv-utils")
object("foo: bar, baz: quux")
object("foo: bar ,baz:quux")
object(":,foo:")
object("::,")
object("")
object(undefined)
conform
Provided a schema, conform
picks keys from an env object and converts them using the supplied functions.
Keys which are present in the schema
, but not in the supplied env
object will be present in the final object, having a value/type based on calling the conversion function with undefined
.
Given these env vars:
DEFAULT_LOCALE=en-GB
SUPPORTED_LOCALES=en-GB,cs-CZ,pl-PL
You can do this:
const {conform, boolean, array, string} = require("dotenv-utils")
const schema = {
MINIFY: boolean,
DEFAULT_LOCALE: string,
SUPPORTED_LOCALES: array,
}
const config = conform(process.env, schema)
module.exports = config
Other Tools
License
MIT