node package manager
Loosely couple your services. Use Orgs to version and reuse your code. Create a free org »

glob-tree

glob-tree.js

Build Status SemVer License

Tree with glob iterator. Glob expressions can be used to iterate over matching sub trees. The tree can be modified while iterating over the nodes.

Install with npm

npm install glob-tree

Browser compatibility

To use this module in a browser, download the npm package and then use Browserify to create a standalone version.

Usage

var Node = require('glob-tree').Node;
 
var n = new Node('root');
n.set('**', 3);
n.set('a.b', 7);
n.set('a.c', 42);
 
var c, i = n.iterator('a.*');
while ((= i.next()) !== undefined) {
  console.log(c.value);
}

Match expressions

Assuming we have this tree:

var n = new Node('root');
n.set('a.b', 1);
n.set('a.c', 2);
n.set('x.b', 3);
n.set('x.y.c', 4);
n.set('z', 5);

You can find nodes with n.iterator(match) using wildcards:

match result
'**' 1, 2, 3, 4
'*' 5
'x.**' 3, 4
'x.*' 3
'*.b' 1, 3
'**.c' 2, 4
'x.*.c' 4

Matchers

Node names may contain wildcards as well. Assuming we have this tree:

var n = new Node('root');
n.set('**', 1);
n.set('a.**', 2);
n.set('a.*.c', 3);
n.set('a.**.d', 4);
n.set('*.b', 5);
n.set('**.d', 6);

Querying for a node will also include the matchers:

match result
'a.b' 1, 2, 5
'a.b.c' 1, 2, 3
'a.b.c.d' 1, 2, 4, 6

Node API

Node is derived from live-tree Node and overrides the iterator function:

  • iterator(match[, options]): Returns an Iterator for the given match expression with these options:
    • matchers: whether to include matchers, defaults to true
    • onlyMatchers: whether to only include matchers, defaults to false

Iterator is derived from min-iterator.

License

MIT