Socket
Socket
Sign inDemoInstall

read-env

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    read-env

Convert environment variables into JSON object


Version published
Weekly downloads
64K
increased by0.74%
Maintainers
1
Install size
15.6 kB
Created
Weekly downloads
 

Readme

Source

read-env

Convert environment variables into JSON object with parsed values.

NPM version Build Status Coverage Status

Install

npm install --save read-env

Basic Example

Let's say you have some environment variables starting with prefix "EXAMPLE_" like below:

EXAMPLE_OBJECT_KEY= '{"prop": "value"}',
EXAMPLE_ARRAY_KEY= '[1,2,3, "string", {"prop": "value"}, 5.2]',
EXAMPLE_TRUE_KEY= 'true',
EXAMPLE_FALSE_KEY= 'false',
EXAMPLE_INT_KEY= '5',
EXAMPLE_FLOAT_KEY= '5.2',
EXAMPLE_STRING_KEY= 'example',

your-app.js

import readEnv from 'read-env';

const options = readEnv('EXAMPLE');
console.log(options);

Output:

{ 
  arrayKey: [ 1, 2, 3, 'string', { prop: 'value' }, 5.2 ],
  falseKey: false,
  floatKey: 5.2,
  intKey: 5,
  objectKey: { prop: 'value' },
  stringKey: 'example',
  trueKey: true 
}

Usage

readEnv(prefix = null, transformKey = 'camelcase')

You can pass a string prefix as first paremeter like below:

const options = readEnv('EXAMPLE');
const optionsLower = readEnv('EXAMPLE', 'lowercase');

function ucfirst(string) {
    return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
const optionsUcfirst = readEnv('EXAMPLE', ucfirst);

readEnv(config)

You can pass whole config object:

function ucfirst(string) {
    return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}

const options = readEnv({
  prefix: 'EXAMPLE',
  includePrefix: false,
  transformKey: ucfirst,
  parse: {
    array: false, //not gonna parse arrays
  }, //still gonna parse object, int, float and boolean
});

Config

Available Config Options:

  • prefix (type: string, default: null): filters environment variables by prefix
  • includePrefix (type: bool, default: false): set true if you want to keep prefix in property names.
  • transformKey (type: null|string|function, default: 'camelcase'): transform environment variable name.
    1. null, doesn't transform the environment variable name.
    2. camelcase, transforms variable name to camelCase.
    3. lowercase, transforms variable name to lowercase.
    4. uppercase, transforms variable name to UPPERCASE.
    5. fn(varName), you can write your own transformer function (varName will be provided with prefix, if includePrefix is true)
  • parse (type: bool|object, default: object):
    1. false: returns raw environment variable value
    2. {}: allows you to define which value types are going to be parsed.
      • object (type: bool, default: true): parse string as object (value must be valid JSON input, see: JSON.parse).
      • array (type: bool, default: true): parse stringified array (value must be valid JSON input, see: JSON.parse).
      • int (type: bool, default: true): parse digits into integer (value consists of only numbers).
      • float (type: bool, default: true): parse decimals into integer (value consists of only numbers with decimal point).
      • bool (type: bool, default: true): parse if string equals to 'true' or 'false'.
  • filter (type: null|function, default: null): filters environment variables (overrides prefix rule).
    1. null, don't filter varaibles.
    2. fn(envVarName, index), custom filter function (envVarName will be provided without any transformation).

Use Case Example

Recently, I used Nightmare for acceptance testing and had several environments which have different configurations.

Instead of writing a code like below:

import Nightmare from 'nightmare';

const nightmare = Nightmare({
  show: process.env.X_NIGHTMARE_SHOW || false,
  width:  process.env.X_NIGHTMARE_WIDTH || 1280,
  height:  process.env.X_NIGHTMARE_HEIGHT || 720,
  typeInterval:  process.env.X_NIGHTMARE_TYPE_INTERVAL || 50,
  //... other properties go forever
});

I wrote this, and nightmare is fully configurable with environment variables :)

import Nightmare from 'nightmare';
import readEnv from 'read-env';

const nightmareConfig = readEnv('X_NIGHTMARE');
const nightmare = Nightmare(nightmareConfig);

LICENCE

MIT (c) 2017 Mehmet Yatkı

Keywords

FAQs

Last updated on 08 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc