♻️ Get, set, or delete nested properties of process.env using a dot path
Switch branches/tags
Nothing to show
Clone or download
Latest commit 92d5075 Nov 14, 2017

README.md

env-dot-prop


♻️ Get, set, or delete nested properties of process.env using a dot path

Background

This package aim to let you access to your environment variables as if they were JavaScript object. See this guide to understand how to use this package to create a 12 Factor compliant configuration system for you app.

Install

$ npm install --save env-dot-prop

Usage

const envDotProp = require('env-dot-prop');

// Let's assume process.env contains the following keys
process.env = {
  FOO_BAR: 'unicorn',
  'FOO_DOT.DOT': 'pony',
  'FOO_UND\\_UND': 'whale'
};

console.log(process.env);
// => { FOO_BAR: 'unicorn', 'FOO_DOT.DOT': 'pony', 'FOO_UND\_UND': 'whale' }
envDotProp.get('');
// => { foo: { bar: 'unicorn', 'dot.dot': 'pony', und_und: 'whale' } }

// getter
envDotProp.get('foo.bar');
// => 'unicorn'

envDotProp.get('foo.notDefined.deep');
// => undefined

envDotProp.get('foo.notDefined.deep', 'default value');
// => 'default value'

envDotProp.get('foo.dot\\.dot');
// => 'pony'

// setter
envDotProp.set('foo.bar', 'b');
envDotProp.get('foo.bar');
// => 'b'

envDotProp.get('');
// => { foo: { bar: 'b', 'dot.dot': 'pony', und_und: 'whale' } }

envDotProp.set('foo.baz.e', 'x');
envDotProp.get('foo.baz.e');
// => 'x'
envDotProp.get('foo.baz');
// => { e: 'x' }

envDotProp.get('');
// => { foo: { bar: 'b', baz: { e: 'x' }, 'dot.dot': 'pony', und_und: 'whale' } }

// has
envDotProp.has('foo.bar');
// => true

// deleter
envDotProp.delete('foo.bar');
envDotProp.get('foo');
// => { baz: { e: 'x' }, 'dot.dot': 'pony', und_und: 'whale' }

envDotProp.delete('foo.baz.e');
envDotProp.get('foo.baz');
// => undefined

envDotProp.set('parse', 42);
envDotProp.get('parse');
// => '42'
envDotProp.get('parse', {parse: true});
// => 42

envDotProp.get('');
// => { parse: '42', foo: { 'dot.dot': 'pony', und_und: 'whale' } }
console.log(process.env);
// => { 'FOO_DOT.DOT': 'pony', 'FOO_UND\_UND': 'whale', PARSE: '42' }

API

get(path, [defaultValue], [opts]) ⇒ string

Returns the values of env keys at the path specified.

Returns: string - The value at the path specified.

Param Type Description
path string Dot separated path.
[defaultValue] string Default value to return if there aren't keys in the path provided.
[opts] object Additional options.
[opts.parse] boolean If true the value returned is parsed using circular-json.
[opts.caseSensitive] boolean If true no case conversion is performed from the dot path provided to the env key search.
Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY.

set(path, value, [opts])

Sets an env key at the path specified. If nested keys are present they will be deleted. Assigning a property on process.env will implicitly convert the value to a string. See the node documentation for more information.

Param Type Description
path string Dot separated path.
value string Value to set.
[opts] object Additional options.
[opts.stringify] boolean If true the value passed is stringified using circular-json.
[opts.caseSensitive] boolean If true no case conversion is performed from the dot path provided to the env key search.
Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY.

del(path, [opts])

Deletes an env key at the path specified. If nested keys are present they will be deleted too.

Param Type Description
path string Dot separated path.
[opts] object Additional options.
[opts.caseSensitive] boolean If true no case conversion is performed from the dot path provided to the env key search.
Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY.

has(path, [opts]) ⇒ boolean

Returns whether an env key exists at the path specified.

Returns: boolean - true if at least an env key with that path exists.

Param Type Description
path string Dot separated path.
[opts] object Additional options.
[opts.caseSensitive] boolean If true no case conversion is performed from the dot path provided to the env key search.
Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.