Skip to content

Commit fc92ffb

Browse files
authored
fix: fix kB/KiB confusion (#1541)
1 parent 3d690ea commit fc92ffb

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

app/composables/useNumberFormatter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ export const useBytesFormatter = () => {
1616
const decimalNumberFormatter = useNumberFormatter({
1717
maximumFractionDigits: 1,
1818
})
19+
const KB = 1000
20+
const MB = 1000 * 1000
1921

2022
return {
2123
format: (bytes: number) => {
22-
if (bytes < 1024)
24+
if (bytes < KB)
2325
return t('package.size.b', {
2426
size: decimalNumberFormatter.value.format(bytes),
2527
})
26-
if (bytes < 1024 * 1024)
28+
if (bytes < MB)
2729
return t('package.size.kb', {
28-
size: decimalNumberFormatter.value.format(bytes / 1024),
30+
size: decimalNumberFormatter.value.format(bytes / KB),
2931
})
3032
return t('package.size.mb', {
31-
size: decimalNumberFormatter.value.format(bytes / (1024 * 1024)),
33+
size: decimalNumberFormatter.value.format(bytes / MB),
3234
})
3335
},
3436
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { computed, ref } from 'vue'
3+
import { useBytesFormatter } from '../../../../app/composables/useNumberFormatter'
4+
5+
describe('useBytesFormatter', () => {
6+
beforeEach(() => {
7+
vi.stubGlobal('computed', computed)
8+
vi.stubGlobal('useI18n', () => ({
9+
locale: ref('en-US'),
10+
t: (key: string, params?: { size?: string }) => {
11+
const size = params?.size ?? ''
12+
13+
if (key === 'package.size.b') return `${size} B`
14+
if (key === 'package.size.kb') return `${size} kB`
15+
if (key === 'package.size.mb') return `${size} MB`
16+
17+
return key
18+
},
19+
}))
20+
})
21+
22+
afterEach(() => {
23+
vi.unstubAllGlobals()
24+
})
25+
26+
it('formats values below 1 kB in bytes', () => {
27+
const { format } = useBytesFormatter()
28+
29+
expect(format(0)).toBe('0 B')
30+
expect(format(999)).toBe('999 B')
31+
})
32+
33+
it('formats kB values using decimal base (1000)', () => {
34+
const { format } = useBytesFormatter()
35+
36+
expect(format(1000)).toBe('1 kB')
37+
expect(format(8414)).toBe('8.4 kB')
38+
})
39+
40+
it('formats MB values using decimal base (1000 * 1000)', () => {
41+
const { format } = useBytesFormatter()
42+
43+
expect(format(1_000_000)).toBe('1 MB')
44+
expect(format(1_500_000)).toBe('1.5 MB')
45+
})
46+
})

0 commit comments

Comments
 (0)