Giter Site home page Giter Site logo

jd-getenv's Introduction

jd-getenv

usage

//With env 
// NODE_ENV : "production"
// MY_ENV_VAR : "hello"
// MY_INT_ENV_VAR : "123"
// MY_JSON_ENV_VAR : "{\"aproperty\" : 12345}"
// MY_NEGATIVEINT_ENV_VAR : "-1"

const EnvGet = require('jd-getenv`);
let t;
t = EnvGet.string("NODE_ENV");
// returns 'production'
t = EnvGet.string("MY_ENV_VAR", "My Default Value if not found");
// returns 'hello'
t = EnvGet.string("MY_ENV_VAR2", "DefaultValue");
// returns 'DefaultValue'
t = EnvGet.int("MY_INT_ENV_VAR", 10);
// returns '123'
t = EnvGet.positiveInt("MY_INT_ENV_VAR", 10);
// returns '123'
t = EnvGet.json("MY_JSON_ENV_VAR", {some : "jsonObject"} );
// returns {aproperty : 12345}

// Following will return exception : 
t = EnvGet.int("NODE_ENV"); // "production"  not int
t = EnvGet.json("NODE_ENV");  // "production"  not json
t = EnvGet.json("MY_INT_ENV_VAR");
t = EnvGet.json("MY_NEGATIVEINT_ENV_VAR");

EnvGet.{type}( env_var_name, default_value) :

  • {type} : one of {int/positiveInt/json/string}, to check type of the env var
  • env_var_name : name of the environment variable to be get/checked
  • default_value : Value to be used if environment variable not set.
  • returns the parsed value according to {type}, or, if not found the default_value if passed.

Throw an error if

  • env_var_name not found and no default_value
  • env_var_name found but not compatible

Remark :

  • No check of type validity of the default_value
  • json type expects a valid object , ie string starting with { or [

jd-getenv's People

Contributors

jdarrort avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.