Skip to content

Commit 3916565

Browse files
cap10morgandawsontoth
authored andcommitted
chore: Use lib/units code to implement humanFileSize
...and update tests
1 parent 683ebd3 commit 3916565

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/lib/humanFileSize.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ describe('humanFileSize', () => {
55
it('should return bytes for values less than 1024', () => {
66
expect(humanFileSize(500)).toBe('500 B');
77
expect(humanFileSize(0)).toBe('0 B');
8-
expect(humanFileSize(1023)).toBe('1023 B');
8+
expect(humanFileSize(1023)).toBe('1,023 B');
99
});
1010

11-
it('should convert to KB for values between 1024 and 1048575', () => {
12-
expect(humanFileSize(1024)).toBe('1 KB');
13-
expect(humanFileSize(2048)).toBe('2 KB');
14-
expect(humanFileSize(1000000)).toBe('977 KB');
11+
it('should convert to KiB for values between 1024 and 1048575', () => {
12+
expect(humanFileSize(1024)).toBe('1 KiB');
13+
expect(humanFileSize(2048)).toBe('2 KiB');
14+
expect(humanFileSize(1000000)).toBe('977 KiB');
15+
expect(humanFileSize(1023900)).toBe('1,000 KiB')
1516
});
1617

17-
it('should convert to MB for appropriate values', () => {
18-
expect(humanFileSize(1048576)).toBe('1 MB');
19-
expect(humanFileSize(2097152)).toBe('2 MB');
18+
it('should convert to MiB for appropriate values', () => {
19+
expect(humanFileSize(1048576)).toBe('1 MiB');
20+
expect(humanFileSize(2097152)).toBe('2 MiB');
2021
});
2122

22-
it('should convert to GB for appropriate values', () => {
23-
expect(humanFileSize(1073741824)).toBe('1 GB');
23+
it('should convert to GiB for appropriate values', () => {
24+
expect(humanFileSize(1073741824)).toBe('1 GiB');
2425
});
2526

2627
it('should apply the multiplier correctly', () => {
27-
expect(humanFileSize(2, 1024)).toBe('2 KB');
28-
expect(humanFileSize(2, 1048576)).toBe('2 MB');
28+
expect(humanFileSize(2, 1024)).toBe('2 KiB');
29+
expect(humanFileSize(2, 1048576)).toBe('2 MiB');
2930
});
3031
});

src/lib/humanFileSize.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
1+
import { scaleValueToUnits, determineUnits } from '@/lib/units.ts';
22

33
export function humanFileSize(input: number, multiplierFromBytes: number = 1) {
4-
const thresh = 1024;
5-
const bytes = input * multiplierFromBytes;
6-
let value = bytes;
4+
const initialValue = input * multiplierFromBytes;
5+
const units = determineUnits('bytes', initialValue);
6+
const scaled = scaleValueToUnits(initialValue, 'bytes', units);
77

8-
if (Math.abs(value) < thresh) {
9-
return value + ' B';
10-
}
11-
12-
let u = -1;
13-
const r = 10;
14-
15-
do {
16-
value /= thresh;
17-
++u;
18-
} while (Math.round(Math.abs(value) * r) / r >= thresh && u < units.length - 1);
19-
20-
return `${new Intl.NumberFormat().format(Math.round(value))} ${units[u]}`;
8+
const value = new Intl.NumberFormat().format(Math.round(scaled));
9+
return `${value} ${units}`;
2110
}

0 commit comments

Comments
 (0)