Add TiB support for display, fix unit to correct one (#15201)

* Add TiB support for display, fix unit to correct one

Signed-off-by: Alexis <60alexis@gmail.com>

* Fix unit test

Signed-off-by: Alexis <60alexis@gmail.com>
This commit is contained in:
Alexis L 2021-06-29 04:40:30 +02:00 committed by GitHub
parent b789674ada
commit 573d97f15b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View File

@ -60,13 +60,15 @@ describe('functions in utils.ts should work', () => {
expect(getSizeNumber(10)).toEqual(10);
expect(getSizeNumber(456400)).toEqual('445.70');
expect(getSizeNumber(45640000)).toEqual('43.53');
expect(getSizeNumber(4564000000000)).toEqual('4.15');
});
it('function getSizeUnit() should work', () => {
expect(getSizeUnit).toBeTruthy();
expect(getSizeUnit(4564)).toEqual('KB');
expect(getSizeUnit(4564)).toEqual('KiB');
expect(getSizeUnit(10)).toEqual('Byte');
expect(getSizeUnit(4564000)).toEqual('MB');
expect(getSizeUnit(4564000000)).toEqual('GB');
expect(getSizeUnit(4564000)).toEqual('MiB');
expect(getSizeUnit(4564000000)).toEqual('GiB');
expect(getSizeUnit(4564000000000)).toEqual('TiB');
});
});

View File

@ -569,11 +569,13 @@ export const validateLimit = unitContrl => {
export function formatSize(tagSize: string): string {
let size: number = Number.parseInt(tagSize);
if (Math.pow(1024, 1) <= size && size < Math.pow(1024, 2)) {
return (size / Math.pow(1024, 1)).toFixed(2) + "KB";
return (size / Math.pow(1024, 1)).toFixed(2) + "KiB";
} else if (Math.pow(1024, 2) <= size && size < Math.pow(1024, 3)) {
return (size / Math.pow(1024, 2)).toFixed(2) + "MB";
return (size / Math.pow(1024, 2)).toFixed(2) + "MiB";
} else if (Math.pow(1024, 3) <= size && size < Math.pow(1024, 4)) {
return (size / Math.pow(1024, 3)).toFixed(2) + "GB";
return (size / Math.pow(1024, 3)).toFixed(2) + "GiB";
} else if (Math.pow(1024, 4) <= size && size < Math.pow(1024, 5)) {
return (size / Math.pow(1024, 4)).toFixed(2) + "TiB";
} else {
return size + "B";
}
@ -590,6 +592,8 @@ export function getSizeNumber(size: number): string | number {
return (size / Math.pow(1024, 2)).toFixed(2);
} else if (Math.pow(1024, 3) <= size && size < Math.pow(1024, 4)) {
return (size / Math.pow(1024, 3)).toFixed(2);
} else if (Math.pow(1024, 4) <= size && size < Math.pow(1024, 5)) {
return (size / Math.pow(1024, 4)).toFixed(2);
} else {
return size;
}
@ -601,11 +605,13 @@ export function getSizeNumber(size: number): string | number {
*/
export function getSizeUnit(size: number): string {
if (Math.pow(1024, 1) <= size && size < Math.pow(1024, 2)) {
return "KB";
return "KiB";
} else if (Math.pow(1024, 2) <= size && size < Math.pow(1024, 3)) {
return "MB";
return "MiB";
} else if (Math.pow(1024, 3) <= size && size < Math.pow(1024, 4)) {
return "GB";
return "GiB";
} else if (Math.pow(1024, 4) <= size && size < Math.pow(1024, 5)) {
return "TiB";
} else {
return "Byte";
}