3.1.0 • Published 4 years ago

objenv v3.1.0

Weekly downloads
12
License
MIT
Repository
github
Last release
4 years ago

OBJENV

objenv overrides an object with it's corresponding environment variables.

Build Status Dependency Status Development Dependency Status Coverage Status

Install:

npm install objenv --save

Quick usage example:

// import objenv from 'objenv'
import {objEnv} from 'objenv'

const envEnrichedObject = objEnv(obj, {prefix: 'psichi'})

objEnv(obj, options, matchFn)

Will traverse obj and test whether an environment variable is set to replace it's value.

matchFn(key, newValue) can be used to execute a function whenever a value is replaced. This is can be useful to report about the environment override.

If you return false from matchFn the value will not be overwritten.

Available options are:

  • options.prefix: defaults to none.
  • options.separator: defaults to '_'
  • options.camelCase: defaults to false

Both options and matchFn are optional, if options are omitted, the matchFn will be the second argument.

objenv alters obj in place, if this is not desired create a copy of the object first.

The camelCase option will expand camelCased keys using the separator.

e.g. a key named databaseName can be overruled using a DATABASE_NAME environment variable.

Example Usage

Example object:

const config = {
  database: {
    host: 'localhost',
    port: '8000'
  }
};

The initial object defines what values can be set.

According to the above the following environment variables will be considered:

DATABASE_HOST
DATABASE_PORT

If you provide a prefix option the environment variables must start with this prefix. e.g. when the prefix is MYORG, the following env vars are tested:

MYORG_DATABASE_HOST
MYORG_DATABASE_PORT

Script:

import objenv from 'objenv'

const options = {
  prefix: 'myorg',
  seperator: '_' // is the default
};

objenv(config, options, (key, value) => {
  console.log('Env key %s found value is now %s', key, value)
});

console.log(config);

Run:

$ MYORG_DATABASE_HOST=0.0.0.0 node script.js
{ "database": { "host": "0.0.0.0", "port": "8000"} }

Similar modules:

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.0

6 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.0

8 years ago