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

data-config

v1.0.2

Published

A pair of classes to manage manipulating an objects data and its storage.

Downloads

12

Readme

Build Status Build Status npm version

NPM

Data/Config

This utility provides a Data and Config class, the Data class allows for the manipulation of a data object using a dot deliminated path. While the Config class extends the Data class with load and save functionality.

Installation

$ npm install data-config

Usage

const Data = require('data-config').Data;

let myData = new Data({
    value1: "hello",
    value2: {
        value3: "world"
    }
});

// > "hello"
console.info(myData.get('value1'));

// > "world"
console.info(myData.get('value2.value3'));

// > null
console.info(myData.get('value3'));

// > "hello world"
console.info(myData.get('value3', 'hello world'));

The Data class provides the a get, set, del and has methods to get, set, delete and check for properties.

const Data = require('data-config').Data;

// Create the Data object, and use '.' for the deliminator.
let myData = new Data({}, '.');

// > false
myData.has('value1');

// > null
myData.get('value1');

myData.set('value1', 'hello world');

// > true
myData.has('value1');

// > "hello world"
myData.get('value1');

myData.del('value1');

// > false
myData.has('value1');

// > null
myData.get('value1');

Setting the data using the Data object also updates the provided data object, for example:

const Data = require('data-config').Data;

let data = {};
let myData = new Data(data);

myData.set('value1.value2', 'hello world');

// > {"value1": {"value2": "hello world"}}
console.dir(data);

Finally, you can also create references from a larger data object, which will kepp each other updated:

const Data = require('data-config').Data;

let data = {
    'settings': {
        'module1': {
            'value1': 'hello world'
        },
        'module2': {
            'value1': 'foo bar'
        }
    }
};
let myData = new Data(data);

let ref1 = myData.ref('settings.module1');
let ref2 = myData.ref('settings.module2');

// > "hello world"
console.info(myData.get('settings.module1.value1'));
console.info(ref1.get('value1'));

// > "foo bar"
console.info(myData.get('settings.module2.value1'));
console.info(ref2.get('value1'));

ref1.set('value1', 'foo bar');

// > "foo bar"
console.info(myData.get('settings.module1.value1'));
console.info(ref1.get('value1'));

// > "foo bar"
console.info(myData.get('settings.module2.value1'));
console.info(ref2.get('value1'));

// > {"settings": {"module1": {"value1": "foo bar"}}, "module2": {"value1": "foo bar"}}}
console.dir(data);

Config object

The config object extends from the Data class, but provides the load/loadSync and save/saveSync methods.

const Config = require('data-config').Config;

let config = new Config('path/to/config-file.json');
config.load((err) => {

    config.set('value1', 'hello');

    config.saveSync();

});

Testing

A mocha test suite has been provided and can be run by:

$ npm test