1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-25 10:32:31 +01:00

Roll back changes to conversion unit (1000->1024)

closes #4183
This commit is contained in:
DaneEveritt 2022-06-27 18:46:36 -04:00
parent 1b5d77dc17
commit 72f545259f
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 29 additions and 21 deletions

View File

@ -3,11 +3,11 @@ import { bytesToString, ip, mbToBytes } from '@/lib/formatters';
describe('@/lib/formatters.ts', function () {
describe('mbToBytes()', function () {
it('should convert from MB to Bytes', function () {
expect(mbToBytes(1)).toBe(1_000_000);
expect(mbToBytes(1)).toBe(1_048_576);
expect(mbToBytes(0)).toBe(0);
expect(mbToBytes(0.1)).toBe(100_000);
expect(mbToBytes(0.001)).toBe(1000);
expect(mbToBytes(1024)).toBe(1_024_000_000);
expect(mbToBytes(0.1)).toBe(104_857);
expect(mbToBytes(0.001)).toBe(1_048);
expect(mbToBytes(1024)).toBe(1_073_741_824);
});
});
@ -20,18 +20,23 @@ describe('@/lib/formatters.ts', function () {
[100.25, '100.25 Bytes'],
[100.998, '101 Bytes'],
[512, '512 Bytes'],
[1000, '1 KB'],
[1024, '1.02 KB'],
[5068, '5.07 KB'],
[10_000, '10 KB'],
[11_864, '11.86 KB'],
[1_000_000, '1 MB'],
[1_356_000, '1.36 MB'],
[1_024_000, '1.02 MB'],
[1_000_000_000, '1 GB'],
[1_024_000_000, '1.02 GB'],
[1_678_342_000, '1.68 GB'],
[1_000_000_000_000, '1 TB'],
[1000, '1000 Bytes'],
[1024, '1 KB'],
[5068, '4.95 KB'],
[10_000, '9.77 KB'],
[10_240, '10 KB'],
[11_864, '11.59 KB'],
[1_000_000, '976.56 KB'],
[1_024_000, '1000 KB'],
[1_025_000, '1000.98 KB'],
[1_048_576, '1 MB'],
[1_356_000, '1.29 MB'],
[1_000_000_000, '953.67 MB'],
[1_070_000_100, '1020.43 MB'],
[1_073_741_824, '1 GB'],
[1_678_342_000, '1.56 GB'],
[1_000_000_000_000, '931.32 GB'],
[1_099_511_627_776, '1 TB'],
])('should format %d bytes as "%s"', function (input, output) {
expect(bytesToString(input)).toBe(output);
});

View File

@ -1,4 +1,4 @@
const _CONVERSION_UNIT = 1000;
const _CONVERSION_UNIT = 1024;
/**
* Given a value in megabytes converts it back down into bytes.
@ -9,13 +9,16 @@ function mbToBytes(megabytes: number): number {
/**
* Given an amount of bytes, converts them into a human readable string format
* using "1000" as the divisor.
* using "1024" as the divisor.
*/
function bytesToString(bytes: number): string {
function bytesToString(bytes: number, decimals = 2): string {
const k = _CONVERSION_UNIT;
if (bytes < 1) return '0 Bytes';
const i = Math.floor(Math.log(bytes) / Math.log(_CONVERSION_UNIT));
const value = Number((bytes / Math.pow(_CONVERSION_UNIT, i)).toFixed(2));
decimals = Math.floor(Math.max(0, decimals));
const i = Math.floor(Math.log(bytes) / Math.log(k));
const value = Number((bytes / Math.pow(k, i)).toFixed(decimals));
return `${value} ${['Bytes', 'KB', 'MB', 'GB', 'TB'][i]}`;
}