-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatData.js
40 lines (34 loc) · 878 Bytes
/
formatData.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
const { nanoid } = require('nanoid');
function getChildren(node, nodes, level) {
level += 1;
const childNodes = nodes.filter((item) => item[0] === node[1]);
nodes = nodes.filter((item) => item[0] !== node[1]);
const children = childNodes.map((item) => {
const [, name, size, line, node] = item;
return {
id: nanoid(),
label: name,
lineColor: line,
nodeColor: node,
nodeSize: Number(size),
level,
children: getChildren(item, nodes, level),
};
});
return children;
}
module.exports = function (data) {
const nodes = data.slice(0);
let level = 1;
const [, name, size, line, node] = nodes[0];
const result = {
id: nanoid(),
label: name,
lineColor: line,
nodeColor: node,
nodeSize: Number(size),
level,
children: getChildren(nodes[0], nodes, level),
};
return result;
};