Install
npm install --save virvar
Tested with Node >= 0.12
Use
// process.env = {// STRING_VAL: 'http://localhost',// INT_VAL: '3000',// FLOAT_VAL: '33.333',// BOOL_VAL1: 'true',// BOOL_VAL2: 'yes',// BOOL_VAL3: '1',// BOOL_VAL3: 'false',// JSON_VAL: '{"a":1,"b":2,"c":3}',// ARRAY_VAL: 'item1,item2,item3'// } var vv = ; console; // 'http://localhost'console; // throws Errorconsole; // 3000console; // 33.333console; // trueconsole; // trueconsole; // trueconsole; // falseconsole; // { a: 1, b: 2, c: 3 }console; // [ 'item1', 'item2', 'item3' ]
vv(name, required, type)
Returns the value of the environment variable, optionally parsing it into the given type.
Throws an error if the required
parameter is truthy and the value is loosely equal to null
(before conversion).
name
Type: string
The name of the environment variable.
required
Type: boolean
Default: false
When true, an Error is thrown if the variable is not set.
type
Type: string
By default, a string is returned. That string can be parsed into a different type by passing one of these values:
'a'
Parses a comma-separated string into an array'b'
Parses the value into a boolean'f'
Parses the value into a float (usingparseFloat
)'i'
Parses the value into an integer (usingparseInt
)'j'
Parses the value into an object (usingJSON.parse
)
Why virvar?
Because environment variable.
🙃