-
Notifications
You must be signed in to change notification settings - Fork 22
/
generateSummaryTableHtml.ts
88 lines (78 loc) · 2.56 KB
/
generateSummaryTableHtml.ts
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
import { icons } from '../icons';
import { oneLine } from 'common-tags';
import { Thresholds } from '../types/Threshold';
import { CoverageReport, ReportNumbers } from '../types/JsonSummary';
function generateSummaryTableHtml(
jsonReport: CoverageReport,
thresholds: Thresholds = {},
jsonCompareReport: CoverageReport | undefined
): string {
return oneLine`
<table>
<thead>
<tr>
<th align="center">Status</th>
<th align="left">Category</th>
<th align="right">Percentage</th>
<th align="right">Covered / Total</th>
</tr>
</thead>
<tbody>
<tr>
${generateTableRow({ reportNumbers: jsonReport.lines, category: 'Lines', threshold: thresholds.lines, reportCompareNumbers: jsonCompareReport?.lines })}
</tr>
<tr>
${generateTableRow({ reportNumbers: jsonReport.statements, category: 'Statements', threshold: thresholds.statements, reportCompareNumbers: jsonCompareReport?.statements })}
</tr>
<tr>
${generateTableRow({ reportNumbers: jsonReport.functions, category: 'Functions', threshold: thresholds.functions, reportCompareNumbers: jsonCompareReport?.functions })}
</tr>
<tr>
${generateTableRow({ reportNumbers: jsonReport.branches, category: 'Branches', threshold: thresholds.branches, reportCompareNumbers: jsonCompareReport?.branches })}
</tr>
</tbody>
</table>
`;
}
function generateTableRow({
reportNumbers,
category,
threshold,
reportCompareNumbers
}: {
reportNumbers: ReportNumbers;
category: string;
threshold?: number;
reportCompareNumbers?: ReportNumbers;
}): string {
let status = icons.blue;
let percent = `${reportNumbers.pct}%`;
if(threshold) {
percent = `${percent} (${icons.target} ${threshold}%)`;
status = reportNumbers.pct >= threshold ? icons.green : icons.red;
}
if(reportCompareNumbers) {
const percentDiff = reportNumbers.pct - reportCompareNumbers.pct;
const compareString = getCompareString(percentDiff);
percent = `${percent}<br/>${compareString}`;
}
return `
<td align="center">${status}</td>
<td align="left">${category}</td>
<td align="right">${percent}</td>
<td align="right">${reportNumbers.covered} / ${reportNumbers.total}</td>
`;
}
function getCompareString(percentDiff: number): string {
if(percentDiff === 0) {
return `${icons.equal} <em>±0%</em>`;
}
if(percentDiff > 0) {
return `${icons.increase} <em>+${percentDiff.toFixed(2)}%</em>`;
}
// The - char is already included in a negative number
return `${icons.decrease} <em>${percentDiff.toFixed(2)}%</em>`;
}
export {
generateSummaryTableHtml
};