const numberFormat = new Intl.NumberFormat('en-US', { maximumFractionDigits: 1 }); export function metricNumber(value: number): string { if (!Number.isFinite(value)) return '—'; if (value > 0 && value < 0.1) return '<0.1'; return numberFormat.format(value); } export function metricPercent(value: number, total: number): string { if (!Number.isFinite(value) || !Number.isFinite(total) || total <= 0) return '—'; const percent = 100 * value / total; return percent > 0 && percent < 0.1 ? '<0.1%' : `${percent.toFixed(1)}%`; } /** Input is hours; retain short, real observations instead of rounding them to zero. */ export function metricDuration(hours: number): string { if (!Number.isFinite(hours) || hours < 0) return '—'; if (hours === 0) return '0 h'; const seconds = hours * 3600; if (seconds < 1) return '<1 s'; if (seconds < 60) return `${numberFormat.format(seconds)} s`; if (hours < 0.1) return `${numberFormat.format(seconds / 60)} min`; return `${numberFormat.format(hours)} h`; } /** Missing evidence always sorts last, in either direction. */ export function compareMetricValues(left: number | null, right: number | null, direction: 'asc' | 'desc'): number { const a = left !== null && Number.isFinite(left) ? left : null; const b = right !== null && Number.isFinite(right) ? right : null; if (a === null || b === null) return a === b ? 0 : a === null ? 1 : -1; return (a - b) * (direction === 'asc' ? 1 : -1); }