-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.js
100 lines (82 loc) · 2.29 KB
/
TreeNode.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// A single tree node
// Root node has null parent
const debug = require('debug')('d::domain-tree::TreeNode')
const first = require('lodash/first')
const tail = require('lodash/tail')
const repeat = require('lodash/repeat')
const merge = require('lodash/merge')
const cloneDeep = require('lodash/cloneDeep')
module.exports = class TreeNode {
constructor(id, parent, data) {
this.id = id
this.data = data
this.parent = parent
this._children = []
}
get level(){
if (this._level) return this._level
if (this.root) return this._level = 0
return this._level = this.parent.level + 1
}
//get id() { return this._id }
//set id(_id) { this._id = _id }
//get parent() { return this._parent }
//set parent(_parent) { this._parent = _parent }
get children() {
return this._children
}
get root() {
return ( this.parent === null ) ? true : false
}
// Get a child
getNodeId(id) {
return this._children.find( i => i.id === id )
}
// Get a child
getNode(id) {
let node = this.getNodeId(id)
//return this._children[id]
if (!node) throw new Error(`No node to get "${id}"`)
return node
}
// Recursive fetch
fetchNode(ids) {
let node = this.getNodeId( first(ids) )
if (!node) return false
if (ids.length === 1) return node
return node.fetchNode( tail(ids) )
}
addNode(id, data = {}) {
let existing_node = this.getNodeId(id)
if (existing_node) return existing_node
let node = new this.constructor(id, this, data)
this._children.push( node )
//this._children[id] = node
return node
}
replaceData(data){
this.data = data
}
// shallow merge an object
setData(data){
if (!this.data) this.data = {}
merge(this.data, data)
return this.data
}
toJSON() {
debug('toJSON',this.id)
let o = {}
if (!this.root) o.id = this.id
if ( this.data ) o._data = cloneDeep(this.data)
o.children = this.children.map( i => i.toJSON() )
return o
}
toString() {
let spaces = repeat(' ', this.level)
let newline_for_children = ''
if ( this.children.length > 0 ) newline_for_children = `\n${spaces}`
let str = (this.root) ? '' : `${this.id}${newline_for_children}`
str += this.children.map( i => i.toString() ).join(`\n${spaces}`)
return str
}
}