README
nhabit
A simple way to parse environment variables.
About
It is a common practice when deploying NodeJS applications to a hosting provider that configuration settings are stored as environment variables. For simple scenarios, reading the environment variable directly from your applications works fine:
var port = process.env.PORT;
This becomes inconvenient however when you start to have many configuration settings. Another common practice is to use a separator in the variable name to simulate a hierarchy of settings (referred to as snake case):
export MYAPP_MYDB_SERVER_PORT=8080;
export MYAPP_MYDB_SERVER_HOST="www.mydb.com"
This package is designed to make it easy to work with environment variables in your code. You can make partial matches on variable names, and it returns values as an Object with nested hierarchal properties. For more information see Usage.
Install
Install from the NPM repository, or directly from GitLab:
npm install nhabit
npm install gitlab:arsnebula/nhabit
Usage
Start by importing the module into your application using require
or the newer ES2015 import
:
var nhabit = require('nhabit');
import nhabit from 'nhabit';
The function requires a path
argument, and optional collection
and default
arguments.
nhabit(path, [collection, default])
The path
argument is used as a prefix filter, and can be specified as an array, dot notation, or snake notation. Matches are always case insensitive. If the path is a partial prefix, the remaining portion of the environment variable is parsed and converted to an Object
of nested properties.
export MYAPP_MYDB_SERVER_PORT=8080
export MYAPP_MYDB_SERVER_HOST="www.mydb.com"
In reference to the environment variables specified above, the following path
arguments are all equivalent and return the same result:
var server = nhabit(['myapp', 'mydb', 'server']);
var server = nhabit('myapp.mydb.server');
var server = nhabit('myapp_mydb_server');
var server = nhabit('mYapp_myDB_seRVer');
/*
server = {
port: 8080,
host: "www.mydb.com"
}
*/
If the path matches the full name of a variable, the value is returned.
var port = nhabit('myapp.mydb.server.port') //-> 8080
The collection
argument defaults to process.env
, but any Object
containing environment style keys can be used.
var settings = {
'MYAPP_MYDB_SERVER_PORT': 8080,
'MYAPP_MYDB_SERVER_HOST': 'www.mydb.com'
}
var server = nhabit('myapp.mydb.server', settings)
/*
server = {
port: 8080,
host: "www.mydb.com"
}
*/
The value specified for default
is returned if no variable matches the specified path
, otherwise it returns undefined
.
var server = nhabit('myapp.myq.server.port', {}, 8080) //-> 8080
In the above example, we are providing an empty collection, so the default value is returned. The default value can be a simple type, an Array, or an Object.
Issues, Enhancements and Contributions
Feedback is welcome and appreciated. Issues or enhancement requests can be submitted using the Issues List on GitLab. For code contributions, please see our Contributing Guide.
Dependencies
This package is written in ES2015 and requires NodeJS v4 or higher.
This package has no external dependencies.
License
MIT - Copyright (c) 2016 Ars Nebula