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

sugar-env

v1.5.15

Published

Sugar code to read environment variables with default values.

Downloads

560

Readme

JavaScript Style Guide Coverage Status Build Status node-current

NPM

sugar-env

Sugar code to read environment variables with default values.

Usage

const app = require('./app')
const env = require('sugar-env')

if (env.is('production')) {
    console.error('Oh no, we shouldn\'t be here!')
    process.exit(1)
}

const port = env.get('PORT')
const host = env.get(['HOST', 'BIND_ADDR'], '0.0.0.0')

app.listen({ host, port }, () => {
  console.log(`${env.current} server running at ${host}:${port}`) // "development server running at 0.0.0.0:3000"
})

API

Please read the test results to learn more about sugar-env's API:

  const env = require('sugar-env')
    env.current: string
      ✔ returns the current environment's name (from NODE_ENV)
      ✔ returns "development" when `NODE_ENV` isn't set
    env.DEVELOPMENT: string
      ✔ returns development's environment name: "development"
    env.TEST: string
      ✔ returns test's environment name: "test"
    env.STAGING: string
      ✔ returns staging's environment name: "staging"
    env.REVIEW: string
      ✔ returns review's environment name: "review"
    env.PRODUCTION: string
      ✔ returns production's environment name: "production"

  const env = require('sugar-env')
    env.is(environment: string): boolean
      ✔ returns `true` when the current environment name is the one specified
      ✔ returns `false` when the current environment name is different than the one specified
    [ DEPRECATED ] env(a: String).is(b: String): boolean
      ✔ returns `true` when "a" is equals "b"
      ✔ returns `false` when "a" is different than "b"

  const env = require('sugar-env')
    env.get(envVarName: string): string
      ✔ returns `null` when the environment variable doesn't exist
      ✔ returns `null` when the environment variable's value is empty
      ✔ returns the environment variable's value when it exists and it isn't empty
    env.get(envVarNames: string[]): string
      ✔ returns the value of the first environment variable which contains a non-empty value
    env.get(envVarName: string | string[], defaultValue: string): string
      ✔ returns `defaultValue` when the given environment variable doesn't exist
      ✔ returns `defaultValue` when the given environment variable's value is empty

  const env = require('sugar-env')
    env.get.boolean(envVarName: string): boolean
      ✔ returns `true` when the given environment variable's value is "1"
      ✔ returns `true` when the given environment variable's value is "true" (case insensitve)
      ✔ returns `false` for anything else
    env.get.int(envVarName: string): number
      ✔ returns `null` when the given environment variable is missing
      ✔ returns `null` when the given environment variable's value is empty
      ✔ returns `NaN` when the given environment variable's value isn't an integer
      ✔ return a `number` when the given environment variable's value is an integer
    env.get.float(envVarName: string): number
      ✔ returns `null` when the given environment variable is missing
      ✔ returns `null` when the given environment variable's value is empty
      ✔ returns `NaN` when the given environment variable's value isn't numeric
      ✔ returns a `number` when the given environment variable's value is numeric
    [ DEPRECATED ] env.get.url(envVarName: string): string
      ✔ returns `null` when the environment variable's value is missing
      ✔ returns `null` when the environment variable's value is empty
      ✔ returns the environment variable's exact value when there is already a trailing slash
      ✔ returns the environment variable's value with a trailing slash
    env.get.base64(envVarName: string): string
      ✔ returns `null` when the environment variable doesn't exist
      ✔ returns `null` when the environment variable's value is empty
      ✔ returns the environment variable's value after decoding it from base64

  const env = require('sugar-env')
    env.has(envVarName: string): boolean
      ✔ returns `true` when the given environment variable exists
      ✔ returns `false` when the given environment variable doesn't exist