Improve byte formatting across the UI
This commit is contained in:
@@ -13,10 +13,16 @@ describe('formatBytes', () => {
|
||||
expect(formatBytes(-50)).toBe('0 B');
|
||||
});
|
||||
|
||||
it('formats megabytes and gigabytes with stable precision', () => {
|
||||
it('formats kilobytes, megabytes, and gigabytes with stable precision', () => {
|
||||
expect(formatBytes(122589)).toBe('119.7 KB');
|
||||
expect(formatBytes(10 * 1024 * 1024)).toBe('10.0 MB');
|
||||
expect(formatBytes(2.5 * 1024 * 1024 * 1024)).toBe('2.50 GB');
|
||||
});
|
||||
|
||||
it('scales cleanly through terabytes and petabytes', () => {
|
||||
expect(formatBytes(3.2 * 1024 ** 4)).toBe('3.20 TB');
|
||||
expect(formatBytes(1.25 * 1024 ** 5)).toBe('1.25 PB');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildProxyLink', () => {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const MB = 1024 * 1024;
|
||||
const GB = MB * 1024;
|
||||
const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] as const;
|
||||
|
||||
export type ServiceState = 'live' | 'warn' | 'fail' | 'idle' | 'paused';
|
||||
|
||||
@@ -8,15 +7,20 @@ export function formatBytes(value: number): string {
|
||||
return '0 B';
|
||||
}
|
||||
|
||||
if (value >= GB) {
|
||||
return `${(value / GB).toFixed(2)} GB`;
|
||||
if (value < 1024) {
|
||||
return `${Math.round(value)} B`;
|
||||
}
|
||||
|
||||
if (value >= MB) {
|
||||
return `${(value / MB).toFixed(1)} MB`;
|
||||
let unitIndex = 0;
|
||||
let scaled = value;
|
||||
|
||||
while (scaled >= 1024 && unitIndex < BYTE_UNITS.length - 1) {
|
||||
scaled /= 1024;
|
||||
unitIndex += 1;
|
||||
}
|
||||
|
||||
return `${Math.round(value)} B`;
|
||||
const decimals = scaled >= 10 ? 1 : 2;
|
||||
return `${scaled.toFixed(decimals)} ${BYTE_UNITS[unitIndex]}`;
|
||||
}
|
||||
|
||||
export function buildProxyLink(
|
||||
|
||||
Reference in New Issue
Block a user