Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const props = withDefaults(
},
)

const KB = 1024
const KB = 1000
const MB = KB ** 2

const colorScale = [
Expand Down
14 changes: 7 additions & 7 deletions packages/rolldown/src/app/utils/__tests__/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { describe, expect, it } from 'vitest'
import { bytesToHumanSize, getContentByteSize, toTree } from '../format'

describe('bytesToHumanSize', () => {
it('should return raw bytes (<1024)', () => {
it('should return raw bytes (<1000)', () => {
expect(bytesToHumanSize(10)).toEqual([10, 'B'])
})

it('should return kb with proper digits', () => {
expect(bytesToHumanSize(1024)).toEqual(['1', 'KB'])
expect(bytesToHumanSize(1024 * 1.5)).toEqual(['1.5', 'KB'])
expect(bytesToHumanSize(1024 * 1.666, 1)).toEqual(['1.7', 'KB'])
expect(bytesToHumanSize(1000)).toEqual(['1', 'kB'])
expect(bytesToHumanSize(1000 * 1.5)).toEqual(['1.5', 'kB'])
expect(bytesToHumanSize(1000 * 1.666, 1)).toEqual(['1.7', 'kB'])
})

it('should return mb with proper digits', () => {
expect(bytesToHumanSize(1024 * 1024)).toEqual(['1', 'MB'])
expect(bytesToHumanSize(1024 * 1024 * 1.5)).toEqual(['1.5', 'MB'])
expect(bytesToHumanSize(1024 * 1024 * 1.666, 1)).toEqual(['1.7', 'MB'])
expect(bytesToHumanSize(1000 * 1000)).toEqual(['1', 'MB'])
expect(bytesToHumanSize(1000 * 1000 * 1.5)).toEqual(['1.5', 'MB'])
expect(bytesToHumanSize(1000 * 1000 * 1.666, 1)).toEqual(['1.7', 'MB'])
})

// larger...
Expand Down
6 changes: 3 additions & 3 deletions packages/rolldown/src/app/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ModuleDest, ModuleTreeNode } from '../../shared/types'

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

export function getContentByteSize(content: string) {
Expand Down
Loading