Skip to content

Commit 6efea92

Browse files
cap10morgandawsontoth
authored andcommitted
feat: Switch to decimal units for data sizes
...to better align with our billing system
1 parent 3916565 commit 6efea92

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

src/lib/humanFileSize.test.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@ import { describe, expect, it } from 'vitest';
22
import { humanFileSize } from './humanFileSize';
33

44
describe('humanFileSize', () => {
5-
it('should return bytes for values less than 1024', () => {
5+
it('should return bytes for values less than 1000', () => {
66
expect(humanFileSize(500)).toBe('500 B');
77
expect(humanFileSize(0)).toBe('0 B');
8-
expect(humanFileSize(1023)).toBe('1,023 B');
8+
expect(humanFileSize(999)).toBe('999 B');
99
});
1010

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')
11+
it('should convert to KiB for values between 1000 and 1000000', () => {
12+
expect(humanFileSize(1000)).toBe('1 KB');
13+
expect(humanFileSize(2000)).toBe('2 KB');
14+
expect(humanFileSize(900000)).toBe('900 KB');
1615
});
1716

1817
it('should convert to MiB for appropriate values', () => {
19-
expect(humanFileSize(1048576)).toBe('1 MiB');
20-
expect(humanFileSize(2097152)).toBe('2 MiB');
18+
expect(humanFileSize(1000000)).toBe('1 MB');
19+
expect(humanFileSize(2000000)).toBe('2 MB');
2120
});
2221

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

2726
it('should apply the multiplier correctly', () => {
28-
expect(humanFileSize(2, 1024)).toBe('2 KiB');
29-
expect(humanFileSize(2, 1048576)).toBe('2 MiB');
27+
expect(humanFileSize(2, 1000)).toBe('2 KB');
28+
expect(humanFileSize(2, 1000000)).toBe('2 MB');
3029
});
3130
});

src/lib/units.test.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ describe('determineUnits', () => {
66
expect(determineUnits('foos', 0)).toBe('foos');
77
});
88

9-
it("should return 'B' for bytes < 1,024", () => {
9+
it("should return 'B' for bytes < 1,000", () => {
1010
expect(determineUnits('bytes', 0)).toBe('B');
1111
expect(determineUnits('bytes', 100)).toBe('B');
12-
expect(determineUnits('bytes', 1023)).toBe('B');
12+
expect(determineUnits('bytes', 999)).toBe('B');
1313
});
1414

15-
it("should return 'KiB' for bytes >= 1,024 && < 1,048,576", () => {
16-
expect(determineUnits('bytes', 1024)).toBe('KiB');
17-
expect(determineUnits('bytes', 2048)).toBe('KiB');
18-
expect(determineUnits('bytes', 1048575)).toBe('KiB');
15+
it("should return 'KiB' for bytes >= 1,000 && < 1,000,000", () => {
16+
expect(determineUnits('bytes', 1000)).toBe('KB');
17+
expect(determineUnits('bytes', 2000)).toBe('KB');
18+
expect(determineUnits('bytes', 900_000)).toBe('KB');
1919
});
2020

21-
it("should return 'MiB' for bytes >= 1,048,576 && < 1,073,741,824", () => {
22-
expect(determineUnits('bytes', 1048576)).toBe('MiB');
23-
expect(determineUnits('bytes', 10000000)).toBe('MiB');
24-
expect(determineUnits('bytes', 1073741823)).toBe('MiB');
21+
it("should return 'MiB' for bytes >= 1,000,000 && < 1,000,000,000", () => {
22+
expect(determineUnits('bytes', 1_000_000)).toBe('MB');
23+
expect(determineUnits('bytes', 10_000_000)).toBe('MB');
24+
expect(determineUnits('bytes', 900_000_000)).toBe('MB');
2525
});
2626

2727
it("should return 'secs' for seconds < 60", () => {
@@ -55,20 +55,19 @@ describe('scaleValueToUnits', () => {
5555
expect(scaleValueToUnits(1000000, 'bytes', 'B')).toBe(1000000);
5656
});
5757

58-
it('should scale bytes to KiB', () => {
59-
expect(scaleValueToUnits(0, 'bytes', 'KiB')).toBe(0);
60-
expect(scaleValueToUnits(1023, 'bytes', 'KiB')).toBeCloseTo(1);
61-
expect(scaleValueToUnits(1024, 'bytes', 'KiB')).toBe(1);
62-
expect(scaleValueToUnits(1024 * 2, 'bytes', 'KiB')).toBe(2);
63-
expect(scaleValueToUnits(1024 * 10, 'bytes', 'KiB')).toBe(10);
58+
it('should scale bytes to KB', () => {
59+
expect(scaleValueToUnits(0, 'bytes', 'KB')).toBe(0);
60+
expect(scaleValueToUnits(1000, 'bytes', 'KB')).toBe(1);
61+
expect(scaleValueToUnits(1000 * 2, 'bytes', 'KB')).toBe(2);
62+
expect(scaleValueToUnits(1000 * 10, 'bytes', 'KB')).toBe(10);
6463
});
6564

66-
it('should scale bytes to MiB', () => {
67-
expect(scaleValueToUnits(0, 'bytes', 'MiB')).toBe(0);
68-
expect(scaleValueToUnits(1024, 'bytes', 'MiB')).toBeCloseTo(0.001);
69-
expect(scaleValueToUnits(1024 * 1024, 'bytes', 'MiB')).toBe(1);
70-
expect(scaleValueToUnits(1024 * 1024 * 2, 'bytes', 'MiB')).toBe(2);
71-
expect(scaleValueToUnits(1024 * 1024 * 10, 'bytes', 'MiB')).toBe(10);
65+
it('should scale bytes to MB', () => {
66+
expect(scaleValueToUnits(0, 'bytes', 'MB')).toBe(0);
67+
expect(scaleValueToUnits(1000, 'bytes', 'MB')).toBeCloseTo(0.001);
68+
expect(scaleValueToUnits(1000 * 1000, 'bytes', 'MB')).toBe(1);
69+
expect(scaleValueToUnits(1000 * 1000 * 2, 'bytes', 'MB')).toBe(2);
70+
expect(scaleValueToUnits(1000 * 1000 * 10, 'bytes', 'MB')).toBe(10);
7271
});
7372

7473
it('should leave secs scaled to secs as-is', () => {

src/lib/units.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export type Units = 'bytes' | 'secs';
22

3-
const dataUnits = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
3+
const dataUnits = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
44
const timeUnits = ['secs', 'mins', 'hrs'];
55
const unitsMultipliers = {
6-
bytes: 1024,
6+
bytes: 1000,
77
secs: 60,
88
};
99

0 commit comments

Comments
 (0)