mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-16 00:52:27 +00:00
20 lines
580 B
TypeScript
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`;
|
|
}
|