Skip to content

Commit 8d0a7fe

Browse files
authored
fix(rolldown): use decimal bytes for file sizes (#269)
1 parent 2f3263f commit 8d0a7fe

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

packages/rolldown/src/app/components/display/FileSizeBadge.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const props = withDefaults(
2020
},
2121
)
2222
23-
const KB = 1024
23+
const KB = 1000
2424
const MB = KB ** 2
2525
2626
const colorScale = [

packages/rolldown/src/app/utils/__tests__/format.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import { describe, expect, it } from 'vitest'
33
import { bytesToHumanSize, getContentByteSize, toTree } from '../format'
44

55
describe('bytesToHumanSize', () => {
6-
it('should return raw bytes (<1024)', () => {
6+
it('should return raw bytes (<1000)', () => {
77
expect(bytesToHumanSize(10)).toEqual([10, 'B'])
88
})
99

1010
it('should return kb with proper digits', () => {
11-
expect(bytesToHumanSize(1024)).toEqual(['1', 'KB'])
12-
expect(bytesToHumanSize(1024 * 1.5)).toEqual(['1.5', 'KB'])
13-
expect(bytesToHumanSize(1024 * 1.666, 1)).toEqual(['1.7', 'KB'])
11+
expect(bytesToHumanSize(1000)).toEqual(['1', 'kB'])
12+
expect(bytesToHumanSize(1000 * 1.5)).toEqual(['1.5', 'kB'])
13+
expect(bytesToHumanSize(1000 * 1.666, 1)).toEqual(['1.7', 'kB'])
1414
})
1515

1616
it('should return mb with proper digits', () => {
17-
expect(bytesToHumanSize(1024 * 1024)).toEqual(['1', 'MB'])
18-
expect(bytesToHumanSize(1024 * 1024 * 1.5)).toEqual(['1.5', 'MB'])
19-
expect(bytesToHumanSize(1024 * 1024 * 1.666, 1)).toEqual(['1.7', 'MB'])
17+
expect(bytesToHumanSize(1000 * 1000)).toEqual(['1', 'MB'])
18+
expect(bytesToHumanSize(1000 * 1000 * 1.5)).toEqual(['1.5', 'MB'])
19+
expect(bytesToHumanSize(1000 * 1000 * 1.666, 1)).toEqual(['1.7', 'MB'])
2020
})
2121

2222
// larger...

packages/rolldown/src/app/utils/format.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ModuleDest, ModuleTreeNode } from '../../shared/types'
22

33
export function bytesToHumanSize(bytes: number, digits = 2) {
4-
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
5-
const i = Math.floor(Math.log(bytes) / Math.log(1024))
4+
const sizes = ['Bytes', 'kB', 'MB', 'GB', 'TB']
5+
const i = Math.floor(Math.log(bytes) / Math.log(1000))
66
if (i === 0)
77
return [bytes, 'B']
8-
return [(+(bytes / 1024 ** i).toFixed(digits)).toLocaleString(), sizes[i]]
8+
return [(+(bytes / 1000 ** i).toFixed(digits)).toLocaleString(), sizes[i]]
99
}
1010

1111
export function getContentByteSize(content: string) {

0 commit comments

Comments
 (0)