Skip to content

Commit cec0601

Browse files
authored
Allow an explicit unit in prettierBytes (#84)
Sometimes we want to display bytes using a specific unit. To support this, `prettierBytes` now accepts the `unit` argument.
1 parent bf5c6ed commit cec0601

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

packages/prettier-bytes/src/prettierBytes.test.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,43 @@ import assert from 'node:assert'
22
import { describe, it } from 'node:test'
33
import { prettierBytes } from './prettierBytes'
44

5-
const testData = [
6-
[2, '2 B'],
7-
[9, '9 B'],
8-
[25, '25 B'],
9-
[235, '235 B'],
10-
[2335, '2.3 KB'],
11-
[23552, '23 KB'],
12-
[235520, '230 KB'],
13-
[2355520, '2.2 MB'],
14-
[23555520, '22 MB'],
15-
[235555520, '225 MB'],
16-
[2355555520, '2.2 GB'],
17-
[23555555520, '22 GB'],
18-
[235556555520, '219 GB'],
19-
[2355556655520, '2.1 TB'],
20-
[23555566655520, '21 TB'],
21-
[235555566665520, '214 TB'],
22-
] satisfies [input: number, output: string][]
5+
const testData: [number, string, string, string, string][] = [
6+
[2, '2 B', '2 B', '0.0 MB', '0.0 TB'],
7+
[9, '9 B', '9 B', '0.0 MB', '0.0 TB'],
8+
[25, '25 B', '25 B', '0.0 MB', '0.0 TB'],
9+
[235, '235 B', '235 B', '0.0 MB', '0.0 TB'],
10+
[2335, '2.3 KB', '2335 B', '0.0 MB', '0.0 TB'],
11+
[23552, '23 KB', '23552 B', '0.0 MB', '0.0 TB'],
12+
[235520, '230 KB', '235520 B', '0.2 MB', '0.0 TB'],
13+
[2355520, '2.2 MB', '2355520 B', '2.2 MB', '0.0 TB'],
14+
[23555520, '22 MB', '23555520 B', '22 MB', '0.0 TB'],
15+
[235555520, '225 MB', '235555520 B', '225 MB', '0.0 TB'],
16+
[2355555520, '2.2 GB', '2355555520 B', '2246 MB', '0.0 TB'],
17+
[23555555520, '22 GB', '23555555520 B', '22464 MB', '0.0 TB'],
18+
[235556555520, '219 GB', '235556555520 B', '224644 MB', '0.2 TB'],
19+
[2355556655520, '2.1 TB', '2355556655520 B', '2246434 MB', '2.1 TB'],
20+
[23555566655520, '21 TB', '23555566655520 B', '22464339 MB', '21 TB'],
21+
[235555566665520, '214 TB', '235555566665520 B', '224643294 MB', '214 TB'],
22+
]
2323

2424
describe('prettierBytes', () => {
25-
it('should convert the specified number of bytes to a human-readable string like 236 MB', () => {
26-
for (const [input, expected] of testData) {
25+
for (const [input, expected, expectedB, expectedMB, expectedTB] of testData) {
26+
it(`should convert ${input} to ${expected}`, () => {
2727
assert.strictEqual(prettierBytes(input), expected)
28-
}
29-
})
28+
})
29+
30+
it(`should convert ${input} to ${expectedB}`, () => {
31+
assert.strictEqual(prettierBytes(input, 'B'), expectedB)
32+
})
33+
34+
it(`should convert ${input} to ${expectedMB}`, () => {
35+
assert.strictEqual(prettierBytes(input, 'MB'), expectedMB)
36+
})
37+
38+
it(`should convert ${input} to ${expectedTB}`, () => {
39+
assert.strictEqual(prettierBytes(input, 'TB'), expectedTB)
40+
})
41+
}
3042

3143
it('throws on non-number', () => {
3244
assert.throws(() => {

packages/prettier-bytes/src/prettierBytes.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Adapted from https://github.com/Flet/prettier-bytes/
22
// Changing 1000 bytes to 1024, so we can keep uppercase KB vs kB
33
// ISC License (c) Dan Flettre https://github.com/Flet/prettier-bytes/blob/master/LICENSE
4-
export function prettierBytes(input: number): string {
4+
5+
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as const
6+
7+
export type Unit = (typeof units)[number]
8+
9+
export function prettierBytes(input: number, unit?: Unit): string {
510
if (typeof input !== 'number' || Number.isNaN(input)) {
611
throw new TypeError(`Expected a number, got ${typeof input}`)
712
}
@@ -17,10 +22,11 @@ export function prettierBytes(input: number): string {
1722
return '0 B'
1823
}
1924

20-
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
21-
const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1)
25+
const exponent = unit
26+
? units.indexOf(unit)
27+
: Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1)
2228
const value = Number(num / 1024 ** exponent)
23-
const unit = units[exponent]
29+
const displayUnit = units[exponent]
2430

25-
return `${value >= 10 || value % 1 === 0 ? Math.round(value) : value.toFixed(1)} ${unit}`
31+
return `${value >= 10 || value % 1 === 0 ? Math.round(value) : value.toFixed(1)} ${displayUnit}`
2632
}

0 commit comments

Comments
 (0)