-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatters.ts
More file actions
135 lines (117 loc) Β· 3.45 KB
/
formatters.ts
File metadata and controls
135 lines (117 loc) Β· 3.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
/**
* @fileoverview Coverage output formatters.
*/
import { indentString } from '../strings/format'
import type { FormatCoverageOptions } from './types'
import { JSONStringify } from '../primordials/json'
import {
NumberIsNaN,
NumberParseFloat,
NumberPrototypeToFixed,
} from '../primordials/number'
/**
* Coverage emoji thresholds for visual feedback.
*/
// oxlint-disable socket/no-status-emoji -- coverage gamification UI; these emoji are part of the public output contract.
const COVERAGE_EMOJI_THRESHOLDS = [
{ emoji: ' π', threshold: 99 },
{ emoji: ' π―', threshold: 95 },
{ emoji: ' β¨', threshold: 90 },
{ emoji: ' π', threshold: 85 },
{ emoji: ' β
', threshold: 80 },
{ emoji: ' π’', threshold: 70 },
{ emoji: ' π‘', threshold: 60 },
{ emoji: ' π¨', threshold: 50 },
{ emoji: ' β οΈ', threshold: 0 },
]
// oxlint-enable socket/no-status-emoji
/**
* Calculate overall coverage percentage.
*/
export function calculateOverall(
code: FormatCoverageOptions['code'],
type: FormatCoverageOptions['type'],
): string {
const metrics = [
NumberParseFloat(code.statements.percent),
NumberParseFloat(code.branches.percent),
NumberParseFloat(code.functions.percent),
NumberParseFloat(code.lines.percent),
].map(val => (NumberIsNaN(val) ? 0 : val))
if (type) {
const typePercent = NumberParseFloat(type.percent)
metrics.push(NumberIsNaN(typePercent) ? 0 : typePercent)
}
const average = metrics.reduce((sum, val) => sum + val, 0) / metrics.length
return NumberPrototypeToFixed(average, 2)
}
/**
* Format coverage data for console output.
*
* @example
* ```typescript
* const output = formatCoverage({
* code: {
* statements: { percent: '85.00' },
* branches: { percent: '80.00' },
* functions: { percent: '90.00' },
* lines: { percent: '88.00' },
* },
* })
* ```
*/
export function formatCoverage(options: FormatCoverageOptions): string {
const opts = {
__proto__: null,
format: 'default' as const,
...options,
} as Required<FormatCoverageOptions>
const { code, format, type } = opts
if (format === 'json') {
return JSONStringify({ code, type }, undefined, 2)
}
const overall = calculateOverall(code, type)
if (format === 'simple') {
return overall
}
// Default format with emoji and details.
let output = ''
// Code coverage section.
output += 'Code Coverage:\n'
output += indentString(`Statements: ${code.statements.percent}%\n`, {
count: 2,
})
output += indentString(`Branches: ${code.branches.percent}%\n`, { count: 2 })
output += indentString(`Functions: ${code.functions.percent}%\n`, {
count: 2,
})
output += indentString(`Lines: ${code.lines.percent}%\n`, { count: 2 })
// Type coverage section.
if (type) {
output += '\nType Coverage:\n'
output += indentString(
`${type.percent}% (${type.covered}/${type.total})\n`,
{ count: 2 },
)
}
// Overall.
const overallValue = NumberParseFloat(overall)
const emoji = getCoverageEmoji(NumberIsNaN(overallValue) ? 0 : overallValue)
output += `\nOverall: ${overall}%${emoji}\n`
return output
}
/**
* Get emoji for coverage percentage.
*
* @example
* ```typescript
* getCoverageEmoji(95) // ' \u{1F3AF}'
* getCoverageEmoji(50) // ' \u{1F528}'
* ```
*/
export function getCoverageEmoji(percent: number): string {
const entry = COVERAGE_EMOJI_THRESHOLDS.find(
({ threshold }) => percent >= threshold,
)
return entry?.emoji || ''
}