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

config-json

v1.0.0

Published

Access JSON based configuration files

Downloads

282

Readme

config-json

A simple class for accessing JSON configuration files.

Use it to store all your app's configuration in replaceable JSON files. My personal use case is a system where multiple instances of the same app run with different configurations.

Note that require('config-json') will always return the same object. The idea here is that you only have to load data in one place without having to handle the same things elsewhere.

config-json will store data from multiple files simultaneously and lets you switch between them.

Installation

npm install config-json

(Use npm install config-json --save to automatically add to the dependencies list in your package.json file.)

Usage

Basic example:

/*
Your example data.json file might look like this:
{
	"key": {
		"subkey": "An important value"
	},
	"key2": 500
}
*/

// Require the module, as usual.
var config = require('config-json');

// Load data from a JSON file.
config.load('./data.json');	

// Retrieve data from that file
var myValue = config.get('key', 'subkey');

.get takes as many parameters as you want. You can drill down to any `level of a JSON file that way.

var entire_file_contents = config.get();
var one_level_down = config.get('L1');
var two_levels_down = config.get('L1', 'L2');

You can also pass in an array of keys:

var the_code = ['UP', 'UP', 'DOWN', 'DOWN', 'LEFT', 'RIGHT', 'LEFT', 'RIGHT', 'B', 'A'];
var cheat = config.get(the_code);

You can load data from a JSON file, or you can pass in a key (we'll get to that in a moment) and an object of any sort yourself:

// Load data from a file
config.load('datafile');

// Load data from existing object
var myData = { ... };
config.load('important_things', myData);

Use .setBaseDir to pre-set the directory where your config files live. That way you need only specify the file name for loading each file:

config.setBaseDir('./my_data_dir');

config.load('file1');
config.load('file2');
config.load('file3');

I mentioned a "key" above. When using .load multiple times, the data is stored separately. When loading from files, the file argument you passed in is the key. When loading data from an existing object, the key is manually set.

By default the last .load defines which object is currently being used. Using .setKey you can switch between the data objects.

config.load('campbell');
config.load('algar');

var firstName = config.get('first_name'); // -> Garth
config.setKey('campbell');
firstName = config.get('first_name'); // -> Wayne

When using .get to fetch a key that does not exist, undefined is returned by default. No error is raised. Note that this happens regardless of whether the first or last key is missing.

By using .setDefault you can override the fallback return value.

var value = config.get('does', 'not', 'compute'); // -> undefined
config.setDefault("I'm sorry Dave, I'm afraid I can't do that");
var value = config.get('does', 'not', 'compute'); // -> "I'm sorry Dave, I'm afraid I can't do that"

Using clearCache() will delete all loaded data and invalidate Node's require cache. Useful for when the files have changed.

config.clearCache();

Using .createInstance() you can create a separate config instance that will not be coupled.

var Config = require('config-json');
var config_one = Config.createInstance.load('./data/file1.json');
var config_two = Config.createInstance.load('./data/file2.json');

Plans

Provide a separate interface object for each data file/object. Right now setKey will set the current key globally which may not be the desired effect.