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

nested-config

v1.0.1

Published

Manage and merge nested configuration objects, keeping track of your defaults

Downloads

146

Readme

Build Status

Configuration object wrapper for nested configs.

Features:

  • Nested configs
  • Deep merge of config objects
  • Keeping track of defaults
  • Plays nicely with nconf (see recipe below)
  • See yargs-config to start yargs CLI application from the config

Usage

Create new config object using create(overrides [object], defaults [object]).

const nc = require('nested-config');
let config = nc.create({a: 'new value'}, {a: 'default value'});

Now config is itself a configuration object. All your current settings are stored in it. It keeps track of default options for you as well. The object has the following API.

config.a;  // 'new value'
config.getDefault('a');  // 'default value'

config.add({b: 1}, {b: 0});  // overrides, defaults
config.b;  // 1
config.getDefault('b');  // 0

Initialize with your default settings

const nc = require('nested-config');

let defaults = {
    yourOption: 1,
    nested: {
        option: "two",
        array: [1]
    }
};

let config = nc.create({yourOption: 2}, defaults);
config.yourOption;  // 2
config.nested.array; // [1] (default)

Update current settings

let overrides = {
    yourOption: 3,
    nested: {
        option: "three",
        array: [2]
    },
};

config.add(overrides, {});  // performs deep merge into current config state, leaves defaults unchanged
config.nested.array;  // [2]

Access settings

config.yourOption;  // 3
config.getDefault('yourOption');  // 1
config.nested.option;  // "three"
config.getPropRef('nested.option'); // "three" (same as above)
config.getDefault('nested.option');  // "two"

Additional options

By default, arrays are replaced. For concatenation, pass additional option {arrayBehavior: 1} to add. For more detail and other options, see API docs.

config.add({nested: {array: [3]}}, {}, { arrayBehavior: 1 });
config.nested.array;  // [2, 3]

Use nconf (if needed)

Prepare nconf wrapper:

const nconf = require('nconf');
const nc = require('./config');

let defaultConfig = nc.create({}, {some: "config"});

nconf
  .env()
  .argv()
  .defaults(defaultConfig);

// exports your current config
module.exports.nconf = nconf;  

// manipulate default config (e.g. add new options for embedded modules)
module.exports.defaults = defaultConfig;  

Extras

The module exports two useful functions which it uses internally: mergeDeep() and plainify(). See API docs for more information.

const nc = require('nested-config');

let obj1 = {
    a: 1,
    nested: {
        arr: [1, 2]
    }
};

let obj2 = {
    a: 2,
    nested: {
        arr: [3],
        foo: 'bar'
    }
};

nc.mergeDeep(obj1, obj2, {arrayBehavior: 1});
/* obj1:
{ 
    a: 2, 
    nested: {
        arr: [1, 2, 3],
        foo: 'bar'
    }
}
*/

let obj3 = nc.plainify(obj2);
/* obj3:
{
    a: 2, 
    'nested.arr': [3],
    'nested.foo': 'bar'
}
*/

JSDoc

API docs are available on the wiki.