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

@darkobits/env

v2.0.0

Published

Functional environment variable getter/parser.

Downloads

2,975

Readme

A functional getter/parser for process.env.

Features

  • Casts number-like values to numbers.
  • Casts "true" and "false" to booleans.
  • Parses JSON.
  • Throws if process or process.env are non-objects.
  • (Optional) Throw if a variable is undefined.

Install

$ npm i @darkobits/env

Use

This package's default export is a function with the following signature:

interface Env {
  (variableName: string, strict: boolean = false): any;
  has(variableName: string): boolean;
  eq(variableName: string, value: any, strict: boolean = false): boolean;
}

Keeping in mind that the values in process.env may only be strings, let's assume process.env looks like this:

{
  "FOO": "foo",
  "BAR": "42",
  "BAZ": "true",
  "QUX": "false",
  "JSON": "{\"kittens\": true}"
}
import env from '@darkobits/env';

env('FOO')         //=> 'foo'
typeof env('FOO')  //=> 'string'

env('BAR')         //=> 42
typeof env('BAR')  //=> 'number'

env('BAZ')         //=> true
typeof env('BAZ')  //=> 'boolean'

env('QUX')         //=> false
typeof env('BAZ')  //=> 'boolean'

env('JSON')        //=> {kittens: true}
typeof env('JSON') //=> 'object'

env('NOAP')        //=> undefined
env('NOAP', true)  //=> throws

// Throws if process.env has been tampered-with.
process.env = null;
env('FOO')         //=> throws

// Throws if process has been tampered-with, or if process doesn't exist.
process = null;
env('FOO')         //=> throws

env.has

This helper predicate is a shorthand for Object.keys(process.env).includes(x). It returns true if the provided variable name exists in process.env and false otherwise. Useful when you don't care what the value of a variable is, only whether it is set or not.

Using our example process.env object from above:

env.has('FOO') //=> true
env.has('UNICORNS') //=> false

env.eq

This helper predicate is a shorthand for env(variableName) === value. It returns true if the provided variable name exists in process.env and is equal to the provided value and false otherwise. Useful when you need to quickly test the value of an environment variable. A third strict argument may be set to true to cause env.eq to throw if the provided variable does not exist in process.env.

Note: When comparing against non-primitives (objects, arrays), env.eq will serialize the provided value and compare it against the serialized (re: string) form of the environment variable.

Using our example process.env object from above:

import env from '@darkobits/env';

env.eq('FOO', 'foo') //=> true
env.eq('BAR', 42)    //=> true
env.eq('BAR', null)  //=> false
env.eq('BAZ', true)  //=> true
env.eq('JSON', {kittens: true}) //=> true