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

@bb301/env-var-parser

v1.0.0

Published

A simple Node.js package (written in TypeScript) containing a set of convenience functions that can be used for retrieving environment variables and casting them into specific types.

Downloads

2

Readme

Simple Node.js Environment Variable Parser

A simple Node.js package (written in TypeScript) containing a set of convenience functions that can be used for retrieving environment variables and casting them into specific types.

Official repository: github.com/BB-301/node-env-var-parser

How to install: npm install --save @bb301/env-var-parser

A quick example

Let's say that we have the following environment variables file:

# The port on which the server should be listening.
SERVER_PORT=20080

# Whether logging should be enabled for requests received
# by the server.
SERVER_LOGGING_ENABLED=true

The following code snippet illustrates a simple Express server, written in TypeScript, in which we make use of three library functions, namely asInteger, asOptionalString, and asBoolean, to respectively retrieve the SERVER_PORT, SERVER_HOSTNAME, and SERVER_LOGGING_ENABLED environment variables:

import { EnvVar } from "@bb301/env-var-parser";
import express from "express";
import morgan from "morgan";

const PORT: number = EnvVar.asInteger("SERVER_PORT");
const HOSTNAME: string = EnvVar.asOptionalString("SERVER_HOSTNAME") ?? "127.0.0.1";
const LOGGING_ENABLED: boolean = EnvVar.asBoolean("SERVER_LOGGING_ENABLED");

const app = express();

if (LOGGING_ENABLED) {
    app.use(morgan("common"));
}

app.get("/", (_, res) => {
    res.send("Hello, Simple Node.js Environment Variable Parser!");
});

app.listen(PORT, HOSTNAME, () => {
    console.log(`Express server listening at 'http://${HOSTNAME}:${PORT}'...`);
});

Please check out this project's official documentation website for a full API description. Alternatively, you may simply read the documentation from the source.

A note about integers and floats

The functions for parsing string-based environment variables into integers and floats are based on JavaScript's Number.parseInt and Number.parseFloat, respectively. That means, for instance, that calling EnvVar.asInteger on an environment variable whose value is 123_test will yield 123 (in other words, it won't fail, as one could expect).

Contact

If you have any questions, if you find bugs, or if you have suggestions for this project, please feel free to contact me by opening an issue on the repository.

License

This project is released under the MIT License.

Copyright

Copyright (c) 2024 BB-301 ([email protected])