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

value-store

v0.4.0

Published

Get/set values on hierarchy of objects

Downloads

17

Readme

value-store

Build Status Dependency Status npm version Coverage Status

Get/set values on hierarchy of objects.

For example, a hierarchy of configuration files could be turned into a 'value-store'. Then it can be queried for the closest key with a value, or, for all values for a key.

Note, I'm using this in nuc.

Install

npm install --save value-store

Usage

// require the package
var buildStore = require('value-store')

// we can provide an array of objects as the initial hierarchy
var initialValues = [
  { first: true, name:'first' },
  { second: true, name:'second' },
  { third: true, name:'third' }
]

// build the instance, provide the array of initial values
var values = buildStore(initialValues)

// Or:
var values = buildStore({ array: initialValues ] })

// outputs:  
values.count()       // 3
values.get('first')  // true
values.get('second') // true
values.get('third')  // true
values.has('third')  // true
values.get('name')   // first
values.all('name')   // [ 'first', 'second', 'third' ]
values.in('third')   // 2

values.get('first', 2) // false
values.info('second')  // { value:true, in:1, overridden:false }
values.info('name')    // { value:'first', in:0, overridden:false }
values.info('name', 1) // { value:'second', in:1, overridden:true }

values.source(0) // constructor
values.source(5) // undefined


// let's change it by setting other values in there

// this puts enabled:true into the first object.
values.set('enabled', true);

// this overrides the current value in the first object
values.set('name', 'primary');

// this overrides the current value in the third object
values.set('name', 'tertiary', 2);

// add an entirely new object as the last object
values.append({ name:'last', something: 'new'});

// add an entirely new object as the first object.
// this essentially overrides all the others.
values.prepend({ name:'overrider' });

// add values from a file (specify path)
values.append('./some/file.json');


// rerun the above series and the new output will be:

values.count()       // 6, was 3

values.get('name')   // 'overrider', was 'first'

// now: [ 'overrider', 'primary', 'second', 'tertiary', 'last' ]
// was: [ 'first', 'second', 'third' ]
values.all('name')

values.in('third')   // 3, was 2

// now: { value:true, in:2, overridden:false }
// was: { value:true, in:1, overridden:false }
values.info('second')

// now: { value:'overrider', in:0, overridden:false }
// was: { value:'first', in:0, overridden:false }
values.info('name')

// now: { value:'primary', in:1, overridden:true }
// was: { value:'second', in:1, overridden:true }
values.info('name', 1)

values.source(0) // prepend
values.source(5) // { file: './some/file.json' }


// others: shift() is like array.shift(). removes the first array element
var result = values.shift();
// is: {
//   removed: [
//      { /* the object with name='overrider' */ }
//   ]
// }
//

// others: pop() is like array.shift(). removes the last array element
result = values.pop();
// is: {
//   removed: [
//     { /* the object with name='overrider' */ }
//   ]
// }

// the result is an array because you can shift/pop more than one by
// specifying how many:
// this removes the first two
values.shift(2);

// this removes the last two
values.pop(2);
// so, now there are the 2 middle ones left.

// when a file is added via its path the path is recorded as its 'source'
values.append('./some/file.json');

// then, that object can be written back to its source.
// let's say the above append put that file as source 3 (4th in array).
// then this would write it back out
values.write(3);

// it will be written as json because the extension is .json
// to alter where it's written to and the format you may pass options:
values.write(3, { file: './some/other/file.ini' });

// the above will be written as an INI file because the extension is '.ini'.
// you may also specify the format:
values.write(3, { file: './other.conf', format:'ini' });
// JSON is the default format. INI is the alternate format.

(MIT License)(LICENSE)