key-tree-store
Simple tool for storing/retrieving objects events based hierarchical keypaths.
It lets you store and retrive objects that are at an equal or deeper key path than what you give it.
install
npm install key-tree-store
example
Assume you've got a structure like this:
'first': obj: 1 obj: 2 'first.stuff': obj: 3 'first.something.other': obj: 4 obj: 5
Then you can retrive it by key. Where it returns anything at or deeper than level supplied.
var KeyTree = ; var tree = ; tree;tree;tree;tree; // now we can retrieve them by keytree; // returns all of themtree; // returns array of objects two, three and fourtree; // returns array of object four; // the `get` method returns them all in an array// if we still need them grouped by key we can use// `getGrouped`tree; // returns {'first.second': [...], 'first.second.third': [...]} // calling `.get()` or `.getGrouped` without arguments// returns all in appropriate format // that's all there is to it
removing items:
var KeyTree = ; var tree = ;var obj1 = obj: '1'; tree; // removes it no matter what keytree;
running items:
// as a shortcut, there's also the `run` method// to help you run functions that match they keypath// this assumes you're storing functions, of course.var KeyTree = ; var tree = ; tree; tree; //=> function ran! // you can also optionally pass a context or arguments to run them withtree; // with argumentstree;
credits
If you like this follow @HenrikJoreteg on twitter.
license
MIT