getChildData.js
4.45 KB
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
function addPath(node, parentPath) {
if (!parentPath) {
return node;
}
return { ...node, file: parentPath + '/' + node.file };
}
function flatten(nodes, parentPath) {
let children = [];
for (let i = 0; i < nodes.length; i++) {
const child = nodes[i];
if (child.children) {
children = [
...children,
...flatten(
child.children,
(parentPath ? parentPath + '/' : '') + child.file
)
];
} else {
children.push(addPath(child, parentPath));
}
}
return children;
}
function filterByFile(nodes, fileFilter, parentPath) {
let children = [];
for (let i = 0; i < nodes.length; i++) {
const child = nodes[i];
const childFullPath = (parentPath ? parentPath + '/' : '') + child.file;
const isChildUnderFilter =
fileFilter === childFullPath ||
fileFilter.indexOf(childFullPath + '/') === 0;
const isChildAboveFilter =
childFullPath.indexOf(fileFilter + '/') === 0;
if (isChildUnderFilter) {
// flatten and continue looking underneath
children = [
...children,
...filterByFile(child.children, fileFilter, childFullPath)
];
} else if (isChildAboveFilter) {
// remove the parent path and add everything underneath
const charsToRemoveFromFile =
fileFilter.length - (parentPath ? parentPath.length : 0);
let childFilename = child.file.slice(charsToRemoveFromFile);
if (childFilename[0] === '/') {
childFilename = childFilename.slice(1);
}
children.push({
...child,
file: childFilename
});
}
}
return children;
}
function sort(childData, activeSort) {
const top = activeSort.order === 'asc' ? 1 : -1;
const bottom = activeSort.order === 'asc' ? -1 : 1;
childData.sort((a, b) => {
let valueA;
let valueB;
if (activeSort.sortKey === 'file') {
valueA = a.file;
valueB = b.file;
} else {
const [metricType, valueType] = activeSort.sortKey.split('.');
valueA = a.metrics[metricType][valueType];
valueB = b.metrics[metricType][valueType];
}
if (valueA === valueB) {
return 0;
}
return valueA < valueB ? top : bottom;
});
for (let i = 0; i < childData.length; i++) {
const child = childData[i];
if (child.children) {
childData[i] = {
...child,
children: sort(child.children, activeSort)
};
}
}
return childData;
}
function filter(nodes, metricsMap, activeFilters) {
const children = [];
for (let i = 0; i < nodes.length; i++) {
let child = nodes[i];
if (child.children) {
const newSubChildren = filter(
child.children,
metricsMap,
activeFilters
);
if (newSubChildren.length) {
child = { ...child, children: newSubChildren };
children.push(child);
}
} else {
if (
(metricsMap.statements &&
activeFilters[child.metrics.statements.classForPercent]) ||
(metricsMap.branches &&
activeFilters[child.metrics.branches.classForPercent]) ||
(metricsMap.functions &&
activeFilters[child.metrics.functions.classForPercent]) ||
(metricsMap.lines &&
activeFilters[child.metrics.lines.classForPercent])
) {
children.push(child);
}
}
}
return children;
}
module.exports = function getChildData(
sourceData,
metricsToShow,
activeSort,
isFlat,
activeFilters,
fileFilter
) {
let childData = sourceData.children;
if (isFlat) {
childData = flatten(childData.slice(0));
}
if (fileFilter) {
childData = filterByFile(childData, fileFilter);
}
if (activeFilters.low) {
activeFilters = { ...activeFilters, empty: true };
}
childData = filter(childData, metricsToShow, activeFilters);
if (activeSort) {
childData = sort(childData, activeSort);
}
return childData;
};