-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.js
172 lines (154 loc) · 4.54 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const DEFAULT_CONFIG = {
id: 'id',
children: 'children',
pid: 'pid'
}
const getConfig = config => Object.assign({}, DEFAULT_CONFIG, config)
const tools = {
fromList (list, config = {}) {
config = getConfig(config)
const nodeMap = new Map(), result = [], { id, children, pid } = config
for (const node of list) {
node[children] = node[children] || []
nodeMap.set(node[id], node)
}
for (const node of list) {
const parent = nodeMap.get(node[pid])
;(parent ? parent.children : result).push(node)
}
return result
},
toList (tree, config = {}) {
config = getConfig(config)
const { children } = config, result = [...tree]
for (let i = 0; i < result.length; i++) {
if (!result[i][children]) continue
result.splice(i + 1, 0, ...result[i][children])
}
return result
},
findNode (tree, func, config = {}) {
config = getConfig(config)
const { children } = config, list = [...tree]
for (let node of list) {
if (func(node)) return node
node[children] && list.push(...node[children])
}
return null
},
findNodeAll (tree, func, config = {}) {
config = getConfig(config)
const { children } = config, list = [...tree], result = []
for (let node of list) {
func(node) && result.push(node)
node[children] && list.push(...node[children])
}
return result
},
findPath (tree, func, config = {}) {
config = getConfig(config)
const path = [], list = [...tree], visitedSet = new Set(), { children } = config
while (list.length) {
const node = list[0]
if (visitedSet.has(node)) {
path.pop()
list.shift()
} else {
visitedSet.add(node)
node[children] && list.unshift(...node[children])
path.push(node)
if (func(node)) return path
}
}
return null
},
findPathAll (tree, func, config = {}) {
config = getConfig(config)
const path = [], list = [...tree], result = []
const visitedSet = new Set(), { children } = config
while (list.length) {
const node = list[0]
if (visitedSet.has(node)) {
path.pop()
list.shift()
} else {
visitedSet.add(node)
node[children] && list.unshift(...node[children])
path.push(node)
func(node) && result.push([...path])
}
}
return result
},
filter (tree, func, config = {}) {
config = getConfig(config)
const { children } = config
function listFilter (list) {
return list.map(node => ({ ...node })).filter(node => {
node[children] = node[children] && listFilter(node[children])
return func(node) || (node[children] && node[children].length)
})
}
return listFilter(tree)
},
forEach (tree, func, config = {}) {
config = getConfig(config)
const list = [...tree], { children } = config
for (let i = 0; i < list.length; i++) {
func(list[i])
list[i][children] && list.splice(i + 1, 0, ...list[i][children])
}
},
_insert (tree, node, targetNode, config, after) {
config = getConfig(config)
const { children } = config
function insert (list) {
let idx = list.indexOf(node)
idx < 0 ? list.forEach(n => insert(n[children] || [])) : list.splice(idx + after, 0, targetNode)
}
insert(tree, node)
},
insertBefore (tree, newNode, oldNode, config = {}) {
tools._insert(tree, oldNode, newNode, config, 0)
},
insertAfter (tree, oldNode, newNode, config = {}) {
tools._insert(tree, oldNode, newNode, config, 1)
},
removeNode (tree, func, config = {}) {
config = getConfig(config)
const { children } = config, list = [tree]
while (list.length) {
const nodeList = list.shift()
const delList = nodeList.reduce((r, n, idx) => (func(n) && r.push(idx), r), [])
delList.reverse()
delList.forEach(idx => nodeList.splice(idx, 1))
const childrenList = nodeList.map(n => n[children]).filter(l => l && l.length)
list.push(...childrenList)
}
}
}
const makeHandlers = () => {
const obj = {}
for (let key in tools) {
if (key.startsWith('_')) continue
obj[key] = tools[key]
}
return obj
}
const handlers = makeHandlers()
const treeHandler = {
...handlers,
createInstance (config) {
const obj = {}
for (const key in handlers) {
const func = handlers[key]
obj[key] = (...args) => func(...args, config)
}
return obj
}
}
if (typeof module == 'undefined' && typeof window != 'undefined') {
window.treeTool = treeHandler
} else {
module.exports = treeHandler
}