-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.js
77 lines (60 loc) · 1.8 KB
/
Tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// A Tree to hold TreeNodes
// If you extend this to hold another type of node, ovrride
// `Tree.node_type` via the `module.exports.node_type` variable
const debug = require('debug')('d::domain-tree::TreeNode')
const castArray = require('lodash/castArray')
const TreeNode = require('./TreeNode')
module.exports = class Tree {
// Node type can be set on the class, or on the instance
get node_type(){
return this._node_type || this.constructor.node_type
}
constructor(options = {}) {
// Support custom node types
if (options.node_type) this._node_type = options.node_type
// Create a root node
this.root = new this.node_type(null,null)
// Child string seperator
this.separator = options.separator || '.'
}
// Add a new node to the tree
// Supports . child notation
//addNode(id, data) {
//debug('adding node', id)
//return this.root.addNode(id, data)
//}
addNode(arg, data = {}) {
debug('Adding new node', arg)
let ids = castArray(arg)
let last_node = this.root
ids.forEach( id => {
last_node = last_node.addNode(id, null)
})
last_node.setData(data)
return last_node
}
// Get a node from the tree
// Supports . child notation (or arbitrary seperator)
getNode(arg) {
debug('Getting node', arg)
let ids = castArray(arg)
return this.root.fetchNode(ids)
}
// Get a node from the tree
// Supports . child notation (or arbitrary seperator)
fetchNode(ids_string) {
debug('Fetching node', ids_string)
let ids = ids_string.split(this.separator)
return this.getNode(ids)
}
// Entry into the nodes toJSON
toJSON() {
return this.root.toJSON()
}
// Entry into the nodes toString
toString() {
return this.root.toString()
}
}
//Tree.node_type = TreeNode
module.exports.node_type = TreeNode