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

reconfig

v3.3.0

Published

JavaScript configurations as they're meant to be. Kinda.

Downloads

1,483

Readme

Reconfig

Build Status

JavaScript configurations as they're meant to be. Kinda.

Reconfig helps you by keeping your configuration clean, decoupled and smart. Don't believe us? Read on.

The problem

How do write declarative configuration in JavaScript?

Easy, simply create a JS object:

var config = {
    protocol:   'https',
    domain:     'example.org',
};

var url = config.protocol + '://' + config.domain; // https://example.org

Easy and ugly as hell. How can we make the config more elegant?

The Solution

Enter Reconfig.


var reconfig = require('reconfig');

var config =  new reconfig({
    protocol:   'https',
    domain:     'example.org',
});

var url = config.get('protocol') + '://' + config.get('domain'); // https://example.org

Blah, this is just some more code...but reconfig is more than that.

With this library you can self-reference config values:


var reconfig = require('reconfig');

var config =  new reconfig({
    protocol:   'https',
    domain:     'example.org',
    url:        '{{ protocol }}://{{ domain }}',
});

var url = config.get('url'); // https://example.org

Pretty nice right? Even better, reconfig is smart enough to be able to reference parameters at any level of the config:


var reconfig = require('reconfig');

var config =  new reconfig({
    credentials: {
        admin: {
            read:   true,
            write:  true
        },
        reader: {
            read:   true,
            write:  false
        }
    },
    users: {
        someImportantDude: {
            username:       'him',
            password:       '...',
            credentials:    '{{ credentials.admin }}'
        }
    }
});

var canTheImportantDudeWrite = config.get('users.someImportantDude.credentials').write; // true

Nice, as you saw we self-referenced a nested value in the config (credentials.admin) and we used the same dot-notation to retrieve the credentials of a specific user (users.someImportantDude.credentials).

It's not over: you can also pass parameters to your configurations!


var reconfig = require('reconfig');

var config =  new reconfig({
    greet: 'Hello :who!'
});

var greetJohn = config.get('greet', {who: 'John'}); // Hello, John!

By default, reconfig uses : to interpolate the parameters. For example, in the above code, :who gets replaced by the parameter value. You can specify your own interpolation for resolving the parameters by setting the paramsInterpolation option.


var reconfig = require('reconfig');

var config =  new reconfig({
    greet: 'Hello {who}!'
}, { 
    paramsInterpolation: ['{', '}'] 
});

var greetJohn = config.get('greet', {who: 'John'}); // Hello, John!

Last but not least, reconfig lets you specify a default value to return if a config value you're trying to access doesn't exist:

var reconfig = require('reconfig');

var config =  new reconfig({
    a: 1
});

config.get('b', {}, 2); // 2

** Setting you config object ** If you wish to change parts of your config on an already existent Reconfig instance:

var reconfig = require('reconfig');

var config =  new reconfig({
    a: 1,
    b: 2,
    c: '{{a}}_{{b}}'
});

config.get('c') // 1_2;

config.set({b: 3});

config.get('c') // 1_3;

Nodejs specifics

ENV overriders

So you're using reconfig in your node.js app and you'd like your sys admins to change values without touching too much JSON? How about the always familiar ENV vars?

Here's how you do it:

just tell reconfig your env vars prefix:

var reconfig = require('reconfig');

var configValues = {
    a: 1,
    b: [2]
};

var config =  new reconfig(configValues, { envPrefix: 'MYPREFIX' });

and you'll be able to modify or add new values directly from your shell!

export or add your vars before runnig your node app:

MYPREFIX_a=3 MYPREFIX_b_0=4 MYPREFIX_c=5 node app.js

and here's what you'll get:

config.get('a')     // 3
config.get('b')[0]  // 4  <-- Pay attention!! It works with arrays too! :D
config.get('c')     // 5

The default properties separator is: _ (1 underscore). You can use your custom separator passing it to the constructor as 3rd parameter:

var config =  new reconfig(configValues, { envPrefix: 'MYPREFIX', separator: '__' });

Installation

Install this library via NPM:

npm install reconfig

If you need it on the client side we highly recommend browserify.

Tests

This library is tested through mocha, simply run either

mocha

or

./node_modules/mocha/bin/mocha

if you don't have mocha installed globally.