Skip to content

Commit 72f5452

Browse files
committed
Roll back changes to conversion unit (1000->1024)
closes pterodactyl#4183
1 parent 1b5d77d commit 72f5452

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

resources/scripts/lib/formatters.spec.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { bytesToString, ip, mbToBytes } from '@/lib/formatters';
33
describe('@/lib/formatters.ts', function () {
44
describe('mbToBytes()', function () {
55
it('should convert from MB to Bytes', function () {
6-
expect(mbToBytes(1)).toBe(1_000_000);
6+
expect(mbToBytes(1)).toBe(1_048_576);
77
expect(mbToBytes(0)).toBe(0);
8-
expect(mbToBytes(0.1)).toBe(100_000);
9-
expect(mbToBytes(0.001)).toBe(1000);
10-
expect(mbToBytes(1024)).toBe(1_024_000_000);
8+
expect(mbToBytes(0.1)).toBe(104_857);
9+
expect(mbToBytes(0.001)).toBe(1_048);
10+
expect(mbToBytes(1024)).toBe(1_073_741_824);
1111
});
1212
});
1313

@@ -20,18 +20,23 @@ describe('@/lib/formatters.ts', function () {
2020
[100.25, '100.25 Bytes'],
2121
[100.998, '101 Bytes'],
2222
[512, '512 Bytes'],
23-
[1000, '1 KB'],
24-
[1024, '1.02 KB'],
25-
[5068, '5.07 KB'],
26-
[10_000, '10 KB'],
27-
[11_864, '11.86 KB'],
28-
[1_000_000, '1 MB'],
29-
[1_356_000, '1.36 MB'],
30-
[1_024_000, '1.02 MB'],
31-
[1_000_000_000, '1 GB'],
32-
[1_024_000_000, '1.02 GB'],
33-
[1_678_342_000, '1.68 GB'],
34-
[1_000_000_000_000, '1 TB'],
23+
[1000, '1000 Bytes'],
24+
[1024, '1 KB'],
25+
[5068, '4.95 KB'],
26+
[10_000, '9.77 KB'],
27+
[10_240, '10 KB'],
28+
[11_864, '11.59 KB'],
29+
[1_000_000, '976.56 KB'],
30+
[1_024_000, '1000 KB'],
31+
[1_025_000, '1000.98 KB'],
32+
[1_048_576, '1 MB'],
33+
[1_356_000, '1.29 MB'],
34+
[1_000_000_000, '953.67 MB'],
35+
[1_070_000_100, '1020.43 MB'],
36+
[1_073_741_824, '1 GB'],
37+
[1_678_342_000, '1.56 GB'],
38+
[1_000_000_000_000, '931.32 GB'],
39+
[1_099_511_627_776, '1 TB'],
3540
])('should format %d bytes as "%s"', function (input, output) {
3641
expect(bytesToString(input)).toBe(output);
3742
});

resources/scripts/lib/formatters.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const _CONVERSION_UNIT = 1000;
1+
const _CONVERSION_UNIT = 1024;
22

33
/**
44
* Given a value in megabytes converts it back down into bytes.
@@ -9,13 +9,16 @@ function mbToBytes(megabytes: number): number {
99

1010
/**
1111
* Given an amount of bytes, converts them into a human readable string format
12-
* using "1000" as the divisor.
12+
* using "1024" as the divisor.
1313
*/
14-
function bytesToString(bytes: number): string {
14+
function bytesToString(bytes: number, decimals = 2): string {
15+
const k = _CONVERSION_UNIT;
16+
1517
if (bytes < 1) return '0 Bytes';
1618

17-
const i = Math.floor(Math.log(bytes) / Math.log(_CONVERSION_UNIT));
18-
const value = Number((bytes / Math.pow(_CONVERSION_UNIT, i)).toFixed(2));
19+
decimals = Math.floor(Math.max(0, decimals));
20+
const i = Math.floor(Math.log(bytes) / Math.log(k));
21+
const value = Number((bytes / Math.pow(k, i)).toFixed(decimals));
1922

2023
return `${value} ${['Bytes', 'KB', 'MB', 'GB', 'TB'][i]}`;
2124
}

0 commit comments

Comments
 (0)