-
Notifications
You must be signed in to change notification settings - Fork 5
/
filter-cube-tree-node.html
144 lines (136 loc) · 3.65 KB
/
filter-cube-tree-node.html
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
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
lang="ja"
xml:lang="ja"
dir="ltr"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml"
>
<head>
<title>filter-cube-tree-node</title>
</head>
<body>
<h2>filter-cube-tree-node</h2>
<script>
const cubeTree = [
{
title: "firebase",
key: "firebase",
children: [
{
type: "string",
value: "firebase.ama_app_id",
label: "Ama App Id",
},
{
type: "string",
meta: {
member_groups: ["App Info"],
},
value: "firebase.app_info__id",
label: "App Info - ID",
},
{
type: "string",
meta: {
member_groups: ["App Info"],
},
value: "firebase.app_info__version",
label: "App Info - Version",
},
],
},
{
title: "user ab testing",
key: "user ab testing",
children: [
{
type: "string",
value: "user_ab_testing.aba_id",
label: "Abalyzer v2 ID",
},
],
},
];
const treeNode = [
{
title: "Firebase",
key: "firebase",
children: [
{
title: "App Info",
key: "App Info",
children: [
{
key: "firebase.app_info__id",
title: "App Info - ID",
},
{
key: "firebase.app_info__version",
title: "App Info - Version",
},
],
},
{
title: "Ama App Id",
key: "firebase.ama_app_id",
},
],
},
{
title: "User Ab Testing",
key: "user_ab_testing",
children: [
{
title: "Abalyzer v2 ID",
key: "user_ab_testing.aba_id",
},
],
},
];
function convertCubeTree(cubeTree) {
function processNode(node) {
const resultNode = {
title: node.title.replace(/(^|\s)\S/g, (l) => l.toUpperCase()),
key: node.key,
children: [],
};
const groupedChildren = {};
(node.children || []).forEach((child) => {
if (child.children) {
// Recursively process child nodes
resultNode.children.push(processNode(child));
} else if (child.meta && child.meta.member_groups) {
child.meta.member_groups.forEach((group) => {
if (!groupedChildren[group]) {
groupedChildren[group] = {
title: group,
key: group,
children: [],
};
}
groupedChildren[group].children.push({
title: child.label,
key: child.value,
});
});
} else {
resultNode.children.push({
title: child.label,
key: child.value,
});
}
});
for (const group in groupedChildren) {
resultNode.children.push(groupedChildren[group]);
}
return resultNode;
}
return cubeTree.map(processNode);
}
const result = convertCubeTree(cubeTree);
console.log("result: ", result);
</script>
</body>
</html>