npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openenv

v0.0.1

Published

Use some simple conventions to read process.env into a deeply nested javascript object recognizing arrays and numbers.

Downloads

94

Readme

Open ENV

Use some simple conventions to read process.env into a deeply nested javascript object recognizing arrays and numbers.

Install

yarn add openenv or npm install openenv

Basic Usage

  //example process.env
  {
    'NODE_ENV':'development',
    'socket.port':'1234',
    'socket.host':'localhost',
    'express.port':'4321'
    'express.host':'localhost',
    'whitelist':'http://example1.com,http://example2.com'
  }

  const config = require('openenv')(process.env)

  //config output
  {
    NODE_ENV:'development',
    //dot notation are interpreted as paths into final objet
    socket:{
      port:1234,       //note numbers try to get parsed as numbers
      host:'localhost',
    },
    express:{
      port:4321,
      host:'localhost',
    },
    //if values have a comma(,) in them, it gets interpreted as an array
    whitelist:['http://example1.com','http://example2.com']
  }

Justification

Its is sometimes necessary for a configuration object to be more complex than what can be represented in a standard process.env of string:string mappings. This library defines a convention to define keys which map to nested paths in an object, as well as rules for parsing arrays and numbers from values. Advanced configuration allows you to prefix or regex filter unecessary variables from your final configuation object. If you can follow these conventions in your env then you dont need any specific env parsing logic in your application, simplifying your app configuration.

There are many other libraries like this available, the main difference with this one is that you can configure your delimiters (ex: use '_' instead of '.' for paths), as well as Regex key filtering (ex: only parse envs starting with lowercase), map values and other options.

API

openenv(process.env,options) => config

const openenv = require('openenv')

//the second object is optional, if not provided it will use default values.
const config = openenv(process.env,{
  //the following are all optional options with defaults shown.

  //specify a regex rule for matching to keys, keys which do 
  //not match are ignored in final output. Regex matches will 
  //not be removed from final key.
  regex:undefined,

  //specify only keys with this prefix to be parsed into the 
  //final config. Prefixes will be removed from the final key.
  prefix:'',
   
  //specify an array delimiter, if seen in the value, 
  //the parser will interpret the value as an array of values.
  arrayDelimiter:','  

  //specify a path delimiter, if seen in a key, the parser will 
  //split the string and interpret the array as a path into the final config.
  pathDelimiter:'.'   

  //all values will run through this parser, before being split for array
  //if an error occurs the raw string is passed instead.
  valueParser:x=>JSON.parse(x)       

  //all keys will pass through this parser, before being split by path
  //by default it will remove the prefix from a matching key.
  keyParser:x=>x.replace(prefix,'')  
})