1 | # key-tree-store
|
2 |
|
3 | Simple tool for storing/retrieving objects events based hierarchical keypaths.
|
4 |
|
5 | It 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 | ```
|
10 | npm install key-tree-store
|
11 | ```
|
12 |
|
13 | ## example
|
14 |
|
15 | Assume 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 |
|
25 | Then you can retrive it by key. Where it returns anything at or deeper than level supplied.
|
26 |
|
27 | ```javascript
|
28 | var KeyTree = require('key-tree-store');
|
29 |
|
30 | var tree = new KeyTree();
|
31 |
|
32 | tree.add('first', {id: 'one'});
|
33 | tree.add('first.second', {id: 'two'});
|
34 | tree.add('first.second', {id: 'three'});
|
35 | tree.add('first.second.third', {id: 'four'});
|
36 |
|
37 | // now we can retrieve them by key
|
38 | tree.get('first'); // returns all of them
|
39 | tree.get('first.second'); // returns array of objects two, three and four
|
40 | tree.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`
|
45 | tree.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 |
|
54 | removing items:
|
55 |
|
56 | ```javascript
|
57 | var KeyTree = require('key-tree-store');
|
58 |
|
59 | var tree = new KeyTree();
|
60 | var obj1 = {obj: '1'};
|
61 |
|
62 | tree.add('key.path', obj1);
|
63 |
|
64 | // removes it no matter what key
|
65 | tree.remove(obj1);
|
66 | ```
|
67 |
|
68 | ## credits
|
69 |
|
70 | If you like this follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) on twitter.
|
71 |
|
72 | ## license
|
73 |
|
74 | MIT
|
75 |
|