Environment Variables
Maps environment variables to a configuration object.
Installation
$ npm install env-to-object
Usage
var env = require( 'env-to-object' );
env( map )
Maps environment variables to a configuration object
.
var map = {
'NODE_ENV': {
'keypath': 'env',
'type': 'string'
},
'PORT': {
'keypath': 'server.port',
'type': 'number'
},
'SSL': {
'keypath': 'server.ssl',
'type': 'boolean'
},
'LOGLEVEL': {
'keypath': 'logger.level',
'type': 'string'
}
};
var out = env( map );
An environment variable mapping must include a keypath
, which is a dot-delimited object
path. By default, this module parses an environment variable value as a string
. The following types are supported:
If an environment variable does not exist, the corresponding configuration keypath
will not exist in the output object
.
var map = {
'UNSET_ENV_VAR': {
'keypath': 'a.b.c'
}
};
var out = env( map );
Examples
var env = require( 'env-to-object' );
var map = {
'DEFAULT': {
'keypath': 'default'
},
'STR': {
'keypath': 'str',
'type': 'string'
},
'NUM': {
'keypath': 'num',
'type': 'number'
},
'BOOL': {
'keypath': 'bool',
'type': 'boolean'
},
'ARR': {
'keypath': 'arr',
'type': 'object'
},
'NESTED': {
'keypath': 'a.b.c.d',
'type': 'object'
}
};
process.env[ 'DEFAULT' ] = 'beep';
process.env[ 'STR' ] = 'boop';
process.env[ 'NUM' ] = '1234.5';
process.env[ 'BOOL' ] = 'true';
process.env[ 'ARR' ] = '[1,2,3,4]';
process.env[ 'NESTED' ] = '{"hello":"world"}';
var out = env( map );
To run the example code from the top-level application directory,
$ node ./examples/index.js
or, alternatively,
$ DEFAULT=boop STR=beep NUM='5432.1' BOOL='FALSE' ARR='[4,3,2,1]' NESTED='{"world":"hello"}' node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
MIT license.
Copyright
Copyright © 2015. Athan Reines.