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

light-conf

v0.0.3

Published

lightweight config file and environment variable reader

Downloads

18

Readme

light-conf NPM version Build Status Code Coverage install size

A very tiny package (zero dependencies) that allows you to read json configuration file and environment variables

Description

When parsing, following steps proceed in this order:

  1. Read default values
  2. Read json config file. All existing values overwrite its default values
  3. Read all environment variables with specified prefix. All existing values overwrite its config values and default values

Install

$ npm install light-conf

Usage

Some arguments are optional, but you can not skip them, you should use undefined instead of correct values

ConfigManager arguments

  • configDefaultKeyValues - object with default config key values, required
  • configFilePath - string with absolute path to json config file, required
  • environment - object with environment variable key values (typically it will be process.env), required
  • envPrefix - string with environment variable prefix to read (variables without that prefix will be ignored), required
  • backwardCompatibilityFunction - function that pre-process json config content, optional
  • keyTypeMapping - object with type mapping for key values (if you want to explicit type conversion from string for environment variables), optional

Example

const ConfigManager = require("light-conf");

const configDefaultKeyValues = {
    "key2": 10,
    "key3": 3.14,
};

// config.json content
//const configFileContent = {
//    "key1": false,
//    "key2": 20,
//    "key6": ["value0"],
//}

process.env.EP_KEY1 = "true";
process.env.EP_KEY4 = "120000";
process.env.EP_KEY5 = "year";
process.env.EP_KEY6 = "value1;value2";
function backwardCompatibilityFunction(configFileContent) {
    // do staff with config for older config files
    return configFileContent;
}
const keyTypeMapping = {
    "key1": "boolean",
    "key2": "integer",
    "key3": "double",
    "key4": "try_integer",
    "key5": "try_integer",
    "key6": "array",
};

const config = new ConfigManager(
                   configDefaultKeyValues,
                   path.join(__dirname, "config.json"),
                   process.env,
                   "EP_",
                   backwardCompatibilityFunction,
                   keyTypeMapping,
               );

console.log(
    config.get("key1")
);
// should print true (env var value overwritten all other)

console.log(
    config.get("key2")
);
// should print 20 (cfg value overwritten default value)

console.log(
    config.get("key3")
);
// should print 3.14 (default value not overwritten by anything)

console.log(
    config.get("key4")
);
// should print 120000 (integer type)

console.log(
    config.get("key5")
);
// should print year (string type)

console.log(
    config.get("key6")
);
// should print ["value1", "value2"] (stringified array type)

console.log(
    config.get()
);
// should print all config values