UNPKG

1.66 kBMarkdownView Raw
1# key-tree-store
2
3Simple tool for storing/retrieving objects events based hierarchical keypaths.
4
5It lets you store and retrive objects that are at an equal or deeper key path than what you give it.
6
7## install
8
9```
10npm install key-tree-store
11```
12
13## example
14
15Assume you've got a structure like this:
16
17```js
18{
19 'first': [ {obj: 1}, {obj: 2} ],
20 'first.stuff': [ {obj: 3} ],
21 'first.something.other': [ {obj: 4}, {obj: 5} ]
22}
23```
24
25Then you can retrive it by key. Where it returns anything at or deeper than level supplied.
26
27```javascript
28var KeyTree = require('key-tree-store');
29
30var tree = new KeyTree();
31
32tree.add('first', {id: 'one'});
33tree.add('first.second', {id: 'two'});
34tree.add('first.second', {id: 'three'});
35tree.add('first.second.third', {id: 'four'});
36
37// now we can retrieve them by key
38tree.get('first'); // returns all of them
39tree.get('first.second'); // returns array of objects two, three and four
40tree.get('first.second.third'); // returns array of object four;
41
42// the `get` method returns them all in an array
43// if we still need them grouped by key we can use
44// `getGrouped`
45tree.getGrouped('first.second'); // returns {'first.second': [...], 'first.second.third': [...]}
46
47// calling `.get()` or `.getGrouped` without arguments
48// returns all in appropriate format
49
50// that's all there is to it
51
52```
53
54removing items:
55
56```javascript
57var KeyTree = require('key-tree-store');
58
59var tree = new KeyTree();
60var obj1 = {obj: '1'};
61
62tree.add('key.path', obj1);
63
64// removes it no matter what key
65tree.remove(obj1);
66```
67
68## credits
69
70If you like this follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) on twitter.
71
72## license
73
74MIT
75