Skip to content

Commit c25a250

Browse files
committed
fix: show actual size for files and disk (i think)
1 parent c4ce5be commit c25a250

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

__tests__/utils/format.test.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ describe('formatSpeed', () => {
1919
expect(formatSpeed(1023)).toBe('1023 B/s')
2020
})
2121

22-
it('formats kilobytes per second', () => {
23-
expect(formatSpeed(1024)).toBe('1.0 KB/s')
24-
expect(formatSpeed(1536)).toBe('1.5 KB/s')
25-
expect(formatSpeed(1024 * 1024 - 1)).toBe('1024.0 KB/s')
22+
it('formats kibibytes per second', () => {
23+
expect(formatSpeed(1024)).toBe('1.0 KiB/s')
24+
expect(formatSpeed(1536)).toBe('1.5 KiB/s')
25+
expect(formatSpeed(1024 * 1024 - 1)).toBe('1024.0 KiB/s')
2626
})
2727

28-
it('formats megabytes per second', () => {
29-
expect(formatSpeed(1024 * 1024)).toBe('1.00 MB/s')
30-
expect(formatSpeed(1.5 * 1024 * 1024)).toBe('1.50 MB/s')
31-
expect(formatSpeed(100 * 1024 * 1024)).toBe('100.00 MB/s')
28+
it('formats mebibytes per second', () => {
29+
expect(formatSpeed(1024 * 1024)).toBe('1.00 MiB/s')
30+
expect(formatSpeed(1.5 * 1024 * 1024)).toBe('1.50 MiB/s')
31+
expect(formatSpeed(100 * 1024 * 1024)).toBe('100.00 MiB/s')
3232
})
3333

3434
it('returns dash when showZero is false and value is 0', () => {
@@ -43,24 +43,24 @@ describe('formatSize', () => {
4343
expect(formatSize(1023)).toBe('1023 B')
4444
})
4545

46-
it('formats kilobytes', () => {
47-
expect(formatSize(1024)).toBe('1.0 KB')
48-
expect(formatSize(1536)).toBe('1.5 KB')
46+
it('formats kibibytes', () => {
47+
expect(formatSize(1024)).toBe('1.0 KiB')
48+
expect(formatSize(1536)).toBe('1.5 KiB')
4949
})
5050

51-
it('formats megabytes', () => {
52-
expect(formatSize(1024 * 1024)).toBe('1.0 MB')
53-
expect(formatSize(500 * 1024 * 1024)).toBe('500.0 MB')
51+
it('formats mebibytes', () => {
52+
expect(formatSize(1024 * 1024)).toBe('1.0 MiB')
53+
expect(formatSize(500 * 1024 * 1024)).toBe('500.0 MiB')
5454
})
5555

56-
it('formats gigabytes', () => {
57-
expect(formatSize(1024 * 1024 * 1024)).toBe('1.00 GB')
58-
expect(formatSize(4.7 * 1024 * 1024 * 1024)).toBe('4.70 GB')
56+
it('formats gibibytes', () => {
57+
expect(formatSize(1024 * 1024 * 1024)).toBe('1.00 GiB')
58+
expect(formatSize(4.7 * 1024 * 1024 * 1024)).toBe('4.70 GiB')
5959
})
6060

61-
it('formats terabytes', () => {
62-
expect(formatSize(1024 * 1024 * 1024 * 1024)).toBe('1.00 TB')
63-
expect(formatSize(2.5 * 1024 * 1024 * 1024 * 1024)).toBe('2.50 TB')
61+
it('formats tebibytes', () => {
62+
expect(formatSize(1024 * 1024 * 1024 * 1024)).toBe('1.00 TiB')
63+
expect(formatSize(2.5 * 1024 * 1024 * 1024 * 1024)).toBe('2.50 TiB')
6464
})
6565
})
6666

@@ -73,14 +73,14 @@ describe('formatCompactSpeed', () => {
7373
expect(formatCompactSpeed(512)).toBe('512B')
7474
})
7575

76-
it('formats compact kilobytes', () => {
77-
expect(formatCompactSpeed(1024)).toBe('1K')
78-
expect(formatCompactSpeed(2048)).toBe('2K')
76+
it('formats compact kibibytes', () => {
77+
expect(formatCompactSpeed(1024)).toBe('1Ki')
78+
expect(formatCompactSpeed(2048)).toBe('2Ki')
7979
})
8080

81-
it('formats compact megabytes', () => {
82-
expect(formatCompactSpeed(1024 * 1024)).toBe('1.0M')
83-
expect(formatCompactSpeed(10.5 * 1024 * 1024)).toBe('10.5M')
81+
it('formats compact mebibytes', () => {
82+
expect(formatCompactSpeed(1024 * 1024)).toBe('1.0Mi')
83+
expect(formatCompactSpeed(10.5 * 1024 * 1024)).toBe('10.5Mi')
8484
})
8585
})
8686

@@ -89,16 +89,16 @@ describe('formatCompactSize', () => {
8989
expect(formatCompactSize(512)).toBe('512B')
9090
})
9191

92-
it('formats compact kilobytes', () => {
93-
expect(formatCompactSize(1024)).toBe('1K')
92+
it('formats compact kibibytes', () => {
93+
expect(formatCompactSize(1024)).toBe('1Ki')
9494
})
9595

96-
it('formats compact megabytes', () => {
97-
expect(formatCompactSize(1024 * 1024)).toBe('1M')
96+
it('formats compact mebibytes', () => {
97+
expect(formatCompactSize(1024 * 1024)).toBe('1Mi')
9898
})
9999

100-
it('formats compact gigabytes', () => {
101-
expect(formatCompactSize(1024 * 1024 * 1024)).toBe('1.0G')
100+
it('formats compact gibibytes', () => {
101+
expect(formatCompactSize(1024 * 1024 * 1024)).toBe('1.0Gi')
102102
})
103103
})
104104

@@ -300,25 +300,25 @@ describe('normalizeSearch', () => {
300300
describe('format edge cases', () => {
301301
describe('formatSpeed edge cases', () => {
302302
it('handles very large values', () => {
303-
expect(formatSpeed(1024 * 1024 * 1024)).toBe('1024.00 MB/s')
303+
expect(formatSpeed(1024 * 1024 * 1024)).toBe('1024.00 MiB/s')
304304
})
305305

306306
it('handles floating point precision', () => {
307-
expect(formatSpeed(1536)).toBe('1.5 KB/s')
307+
expect(formatSpeed(1536)).toBe('1.5 KiB/s')
308308
})
309309
})
310310

311311
describe('formatSize edge cases', () => {
312312
it('handles exact boundary values', () => {
313-
expect(formatSize(1024)).toBe('1.0 KB')
314-
expect(formatSize(1024 * 1024)).toBe('1.0 MB')
315-
expect(formatSize(1024 * 1024 * 1024)).toBe('1.00 GB')
316-
expect(formatSize(1024 * 1024 * 1024 * 1024)).toBe('1.00 TB')
313+
expect(formatSize(1024)).toBe('1.0 KiB')
314+
expect(formatSize(1024 * 1024)).toBe('1.0 MiB')
315+
expect(formatSize(1024 * 1024 * 1024)).toBe('1.00 GiB')
316+
expect(formatSize(1024 * 1024 * 1024 * 1024)).toBe('1.00 TiB')
317317
})
318318

319319
it('handles values just below boundaries', () => {
320320
expect(formatSize(1023)).toBe('1023 B')
321-
expect(formatSize(1024 * 1024 - 1)).toBe('1024.0 KB')
321+
expect(formatSize(1024 * 1024 - 1)).toBe('1024.0 KiB')
322322
})
323323
})
324324

src/utils/format.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
export function formatSpeed(bytes: number, showZero = true): string {
22
if (bytes === 0 && !showZero) return '—'
33
if (bytes < 1024) return `${bytes} B/s`
4-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB/s`
5-
return `${(bytes / 1024 / 1024).toFixed(2)} MB/s`
4+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KiB/s`
5+
return `${(bytes / 1024 / 1024).toFixed(2)} MiB/s`
66
}
77

88
export function formatSize(bytes: number): string {
99
if (bytes < 1024) return `${bytes} B`
10-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
11-
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`
12-
if (bytes < 1024 * 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024 / 1024).toFixed(2)} GB`
13-
return `${(bytes / 1024 / 1024 / 1024 / 1024).toFixed(2)} TB`
10+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KiB`
11+
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MiB`
12+
if (bytes < 1024 * 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024 / 1024).toFixed(2)} GiB`
13+
return `${(bytes / 1024 / 1024 / 1024 / 1024).toFixed(2)} TiB`
1414
}
1515

1616
export function formatCompactSpeed(bytes: number): string {
1717
if (bytes === 0) return '-'
1818
if (bytes < 1024) return `${bytes}B`
19-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)}K`
20-
return `${(bytes / 1024 / 1024).toFixed(1)}M`
19+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)}Ki`
20+
return `${(bytes / 1024 / 1024).toFixed(1)}Mi`
2121
}
2222

2323
export function formatCompactSize(bytes: number): string {
2424
if (bytes < 1024) return `${bytes}B`
25-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)}K`
26-
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(0)}M`
27-
return `${(bytes / 1024 / 1024 / 1024).toFixed(1)}G`
25+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)}Ki`
26+
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(0)}Mi`
27+
return `${(bytes / 1024 / 1024 / 1024).toFixed(1)}Gi`
2828
}
2929

3030
export function formatEta(seconds: number): string {

0 commit comments

Comments
 (0)