source: trip-planner-front/node_modules/istanbul-reports/lib/html-spa/src/getChildData.js@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.5 KB
Line 
1function addPath(node, parentPath) {
2 if (!parentPath) {
3 return node;
4 }
5 return { ...node, file: parentPath + '/' + node.file };
6}
7
8function flatten(nodes, parentPath) {
9 let children = [];
10 for (let i = 0; i < nodes.length; i++) {
11 const child = nodes[i];
12 if (child.children) {
13 children = [
14 ...children,
15 ...flatten(
16 child.children,
17 (parentPath ? parentPath + '/' : '') + child.file
18 )
19 ];
20 } else {
21 children.push(addPath(child, parentPath));
22 }
23 }
24 return children;
25}
26
27function filterByFile(nodes, fileFilter, parentPath) {
28 let children = [];
29
30 for (let i = 0; i < nodes.length; i++) {
31 const child = nodes[i];
32 const childFullPath = (parentPath ? parentPath + '/' : '') + child.file;
33
34 const isChildUnderFilter =
35 fileFilter === childFullPath ||
36 fileFilter.indexOf(childFullPath + '/') === 0;
37 const isChildAboveFilter =
38 childFullPath.indexOf(fileFilter + '/') === 0;
39
40 if (isChildUnderFilter) {
41 // flatten and continue looking underneath
42 children = [
43 ...children,
44 ...filterByFile(child.children, fileFilter, childFullPath)
45 ];
46 } else if (isChildAboveFilter) {
47 // remove the parent path and add everything underneath
48 const charsToRemoveFromFile =
49 fileFilter.length - (parentPath ? parentPath.length : 0);
50 let childFilename = child.file.slice(charsToRemoveFromFile);
51 if (childFilename[0] === '/') {
52 childFilename = childFilename.slice(1);
53 }
54 children.push({
55 ...child,
56 file: childFilename
57 });
58 }
59 }
60 return children;
61}
62
63function sort(childData, activeSort) {
64 const top = activeSort.order === 'asc' ? 1 : -1;
65 const bottom = activeSort.order === 'asc' ? -1 : 1;
66 childData.sort((a, b) => {
67 let valueA;
68 let valueB;
69 if (activeSort.sortKey === 'file') {
70 valueA = a.file;
71 valueB = b.file;
72 } else {
73 const [metricType, valueType] = activeSort.sortKey.split('.');
74 valueA = a.metrics[metricType][valueType];
75 valueB = b.metrics[metricType][valueType];
76 }
77
78 if (valueA === valueB) {
79 return 0;
80 }
81 return valueA < valueB ? top : bottom;
82 });
83
84 for (let i = 0; i < childData.length; i++) {
85 const child = childData[i];
86 if (child.children) {
87 childData[i] = {
88 ...child,
89 children: sort(child.children, activeSort)
90 };
91 }
92 }
93 return childData;
94}
95
96function filter(nodes, metricsMap, activeFilters) {
97 const children = [];
98 for (let i = 0; i < nodes.length; i++) {
99 let child = nodes[i];
100 if (child.children) {
101 const newSubChildren = filter(
102 child.children,
103 metricsMap,
104 activeFilters
105 );
106 if (newSubChildren.length) {
107 child = { ...child, children: newSubChildren };
108 children.push(child);
109 }
110 } else {
111 if (
112 (metricsMap.statements &&
113 activeFilters[child.metrics.statements.classForPercent]) ||
114 (metricsMap.branches &&
115 activeFilters[child.metrics.branches.classForPercent]) ||
116 (metricsMap.functions &&
117 activeFilters[child.metrics.functions.classForPercent]) ||
118 (metricsMap.lines &&
119 activeFilters[child.metrics.lines.classForPercent])
120 ) {
121 children.push(child);
122 }
123 }
124 }
125 return children;
126}
127
128module.exports = function getChildData(
129 sourceData,
130 metricsToShow,
131 activeSort,
132 isFlat,
133 activeFilters,
134 fileFilter
135) {
136 let childData = sourceData.children;
137
138 if (isFlat) {
139 childData = flatten(childData.slice(0));
140 }
141
142 if (fileFilter) {
143 childData = filterByFile(childData, fileFilter);
144 }
145
146 if (activeFilters.low) {
147 activeFilters = { ...activeFilters, empty: true };
148 }
149 childData = filter(childData, metricsToShow, activeFilters);
150
151 if (activeSort) {
152 childData = sort(childData, activeSort);
153 }
154 return childData;
155};
Note: See TracBrowser for help on using the repository browser.