-
Notifications
You must be signed in to change notification settings - Fork 10
/
format.js
95 lines (77 loc) · 2.52 KB
/
format.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
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
const { createHTMLTableFromArray } = require('./utils');
const LETTER_LABEL = {
S: 'Statements',
B: 'Branches',
F: 'Functions',
L: 'Lines',
};
const LETTER_PERCENT = {
S: (data) => formatPercentWithIndicator(data.statements.pct),
B: (data) => formatPercentWithIndicator(data.branches.pct),
F: (data) => formatPercentWithIndicator(data.functions.pct),
L: (data) => formatPercentWithIndicator(data.lines.pct),
};
const COVERAGE_LEVEL_IMAGE = {
low: 'https://user-images.githubusercontent.com/11299391/159445221-fe3dc085-8c56-4e03-9642-219784c88fe7.svg',
medium:
'https://user-images.githubusercontent.com/11299391/159445212-f135c6d7-f354-4e8c-9a9f-28bb3ff1b7b5.svg',
high: 'https://user-images.githubusercontent.com/11299391/159445220-d88b3624-0814-4664-80c8-09f0f2b8e68b.svg',
};
function formatFilesCoverageDataToHTMLTable(filesCoverageData, options = {}) {
const { order = 'SBFL', filePrefix = '' } = options;
const [o1, o2, o3, o4] = order.split('');
const headers = [
'File',
LETTER_LABEL[o1],
LETTER_LABEL[o2],
LETTER_LABEL[o3],
LETTER_LABEL[o4],
].filter(Boolean);
const rows = filesCoverageData.map(([file, data]) => {
const fileCellValue = filePrefix ? createLink(filePrefix + file, file) : file;
return [
fileCellValue,
LETTER_PERCENT[o1]?.(data),
LETTER_PERCENT[o2]?.(data),
LETTER_PERCENT[o3]?.(data),
LETTER_PERCENT[o4]?.(data),
].filter(Boolean);
});
return createHTMLTableFromArray([headers, ...rows]);
}
function formatPercentWithIndicator(percent) {
if (!Number.isFinite(percent)) {
return '';
}
const imageSrc = getCoverageLevelImage(percent);
const imageHTML = `<img src="${imageSrc}">`;
return imageHTML + ' ' + percent + '%';
}
function formatPercentDiff(percent) {
if (!Number.isFinite(percent)) {
return '';
}
const roundedPercent = `${Number(percent.toFixed(2))}%`;
if (percent >= 0) {
return '+' + roundedPercent;
}
return roundedPercent;
}
function getCoverageLevelImage(percent) {
// https://github.com/istanbuljs/istanbuljs/blob/c1559005b3bb318da01f505740adb0e782aaf14e/packages/istanbul-lib-report/lib/watermarks.js
if (percent >= 80) {
return COVERAGE_LEVEL_IMAGE.high;
} else if (percent >= 50) {
return COVERAGE_LEVEL_IMAGE.medium;
} else {
return COVERAGE_LEVEL_IMAGE.low;
}
}
function createLink(link, label) {
return `<a href="${link}">${label}</a>`;
}
module.exports = {
formatFilesCoverageDataToHTMLTable,
formatPercentWithIndicator,
formatPercentDiff,
};