Files
pulse/frontend-modern/src/utils/throughputPresentation.ts
T
2026-03-19 09:24:59 +00:00

20 lines
580 B
TypeScript

const RATE_UNITS = [
{ threshold: 1e9, suffix: 'GB/s', precision: 1 },
{ threshold: 1e6, suffix: 'MB/s', precision: 1 },
{ threshold: 1e3, suffix: 'KB/s', precision: 0 },
] as const;
export function formatThroughputRate(bytesPerSecond: number): string {
if (!Number.isFinite(bytesPerSecond) || bytesPerSecond < 0) {
return '0 B/s';
}
for (const unit of RATE_UNITS) {
if (bytesPerSecond >= unit.threshold) {
return `${(bytesPerSecond / unit.threshold).toFixed(unit.precision)} ${unit.suffix}`;
}
}
return `${Math.round(bytesPerSecond)} B/s`;
}