-
Notifications
You must be signed in to change notification settings - Fork 41
/
test.js
186 lines (169 loc) · 3.91 KB
/
test.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const tree = require('./dist/index')
function getTree () {
const tree = [
{
id: '1',
title: '节点1',
children: [
{
id: '1-1',
title: '节点1-1'
},
{
id: '1-2',
title: '节点1-2',
children: [
{
id: '1-2-1',
title: '节点1-2-1'
}
]
}
]
},
{
id: '2',
title: '节点2',
children: [
{
id: '2-1',
title: '节点2-1'
}
]
}
]
return tree
}
function getList () {
const list = [
{
id: '1',
title: '节点1',
parentId: '',
},
{
id: '1-1',
title: '节点1-1',
parentId: '1'
},
{
id: '1-2',
title: '节点1-2',
parentId: '1'
},
{
id: '2',
title: '节点2',
parentId: ''
},
{
id: '2-1',
title: '节点2-1',
parentId: '2'
}
]
return list
}
// 创建一个实例,因为数据里的pid属性名与默认值不同,所以需要传递该配置项
const instance = tree.createInstance({ pid: 'parentId' })
// 列表结构转树
function testFromList () {
const list = getList()
const tree = instance.fromList(list)
console.log(JSON.stringify(tree, null, 2))
}
// 树结构转列表结构
function testToList () {
const tree = getTree()
const list = instance.toList(tree)
console.log(list.map(i => i.id))
}
// 查找节点
function testFindNode () {
const callback = node => node.id == '2-1'
const tree = getTree()
const result = instance.findNode(tree, callback)
console.log(JSON.stringify(result, null, 2))
}
// 查找符合条件的所有节点
function testFindNodeAll () {
const list = getList()
const tree = instance.fromList(list)
const callback = node => node.parentId == '1'
const result = instance.findNodeAll(tree, callback)
console.log(JSON.stringify(result, null, 2))
}
// 查找节点路径
function testFindPath () {
const callback = node => node.id == '2-1'
const tree = getTree()
const result = instance.findPath(tree, callback)
console.log(result.map(i => i.id))
}
// 查找符合条件的所有节点的路径
function testFindPathAll () {
const callback = node => node.id == '2-1' || node.id == '1-2-1'
const tree = getTree()
const result = instance.findPathAll(tree, callback)
console.log(result)
}
// 树节点过滤
function testFilter () {
const callback = node => node.id == '2-1'
const tree = getTree()
const result = instance.filter(tree, callback)
console.log(JSON.stringify(result, null, 2))
}
// 树节点遍历 深度优先
function testForEach () {
const tree = getTree()
const idList = []
instance.forEach(tree, node => idList.push(node.id))
console.log(idList)
}
// 节点插入:在node前插入newNode
function testInsertBefore () {
const tree = getTree()
const node = instance.findNode(tree, n => n.id == '1-2-1')
const newNode = {
id: '1-2-0',
title: '节点1-2-0'
}
instance.insertBefore(tree, newNode, node)
const idList = []
instance.forEach(tree, node => idList.push(node.id))
console.log(idList)
}
// 节点插入:在node后插入newNode
function testInsertAfter () {
const tree = getTree()
const node = instance.findNode(tree, n => n.id == '1-2-1')
const newNode = {
id: '1-2-2',
title: '节点1-2-2'
}
instance.insertAfter(tree, node, newNode)
const idList = []
instance.forEach(tree, node => idList.push(node.id))
console.log(idList)
}
// 节点删除:删除符合条件的Node
function testRemoveNode () {
const tree = getTree()
instance.removeNode(tree, n => n.id == '1')
console.log(tree)
}
function test () {
// testToList()
// testFromList()
// testFindNode()
// testFindNodeAll()
// testFindPath()
// testFilter()
// testForEach()
// testFindPathAll()
// testInsertBefore()
// testInsertAfter()
testRemoveNode()
}
test()