CoverageCheck / web /src /lib /format.ts
TBM99's picture
Deploy coverage-check 3559250
23d418f verified
Raw History Blame Contribute Delete
1.45 kB
// Small display formats, in one place so every page says numbers the same way.
/** 4400 -> "4.4 s"; under 100 ms -> "<0.1 s"; null -> "". */
export function seconds(ms: number | null | undefined): string {
if (ms == null) return ''
if (ms < 100) return '<0.1 s'
return `${(ms / 1000).toFixed(ms < 10_000 ? 1 : 0)} s`
}
/** 0.00426 -> "$0.0043" (a question costs fractions of a cent). */
export function usd(x: number): string {
return `$${x < 0.01 ? x.toFixed(4) : x.toFixed(2)}`
}
/** 0.835 -> "84%" (two decimals of a proportion, as the report rounds them). */
export function percent(x: number): string {
return `${Math.round(x * 100)}%`
}
/** "2026-07-01", "2026-07-01" .. null -> "in force from 2026-07-01"; both null -> no dates. */
export function inForce(from: string | null, to: string | null): string {
if (!from && !to) return 'no in-force dates stated'
if (from && !to) return `in force from ${from}`
if (!from && to) return `in force until ${to}`
return `in force ${from} to ${to}`
}
/** The outcome words the eval uses, for people. */
export function outcomeLabel(outcome: string): string {
return (
{
pass: 'correct',
fail: 'wrong',
correct_abstain: 'correctly declined',
abstain: 'declined (it was answerable)',
error: 'error',
}[outcome] ?? outcome
)
}
export function isGood(outcome: string): boolean {
return outcome === 'pass' || outcome === 'correct_abstain'
}