Commit ab2d8ec0 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Merge branch 'tor/defect/proper-negative-human-sizes' into 'master'

Fix humanized size numbers for negative values

See merge request gitlab-org/gitlab!62441
parents 38661b31 69f1f173
...@@ -72,11 +72,13 @@ export function bytesToGiB(number) { ...@@ -72,11 +72,13 @@ export function bytesToGiB(number) {
* @returns {String} * @returns {String}
*/ */
export function numberToHumanSize(size) { export function numberToHumanSize(size) {
if (size < BYTES_IN_KIB) { const abs = Math.abs(size);
if (abs < BYTES_IN_KIB) {
return sprintf(__('%{size} bytes'), { size }); return sprintf(__('%{size} bytes'), { size });
} else if (size < BYTES_IN_KIB * BYTES_IN_KIB) { } else if (abs < BYTES_IN_KIB ** 2) {
return sprintf(__('%{size} KiB'), { size: bytesToKiB(size).toFixed(2) }); return sprintf(__('%{size} KiB'), { size: bytesToKiB(size).toFixed(2) });
} else if (size < BYTES_IN_KIB * BYTES_IN_KIB * BYTES_IN_KIB) { } else if (abs < BYTES_IN_KIB ** 3) {
return sprintf(__('%{size} MiB'), { size: bytesToMiB(size).toFixed(2) }); return sprintf(__('%{size} MiB'), { size: bytesToMiB(size).toFixed(2) });
} }
return sprintf(__('%{size} GiB'), { size: bytesToGiB(size).toFixed(2) }); return sprintf(__('%{size} GiB'), { size: bytesToGiB(size).toFixed(2) });
......
...@@ -80,18 +80,22 @@ describe('Number Utils', () => { ...@@ -80,18 +80,22 @@ describe('Number Utils', () => {
describe('numberToHumanSize', () => { describe('numberToHumanSize', () => {
it('should return bytes', () => { it('should return bytes', () => {
expect(numberToHumanSize(654)).toEqual('654 bytes'); expect(numberToHumanSize(654)).toEqual('654 bytes');
expect(numberToHumanSize(-654)).toEqual('-654 bytes');
}); });
it('should return KiB', () => { it('should return KiB', () => {
expect(numberToHumanSize(1079)).toEqual('1.05 KiB'); expect(numberToHumanSize(1079)).toEqual('1.05 KiB');
expect(numberToHumanSize(-1079)).toEqual('-1.05 KiB');
}); });
it('should return MiB', () => { it('should return MiB', () => {
expect(numberToHumanSize(10485764)).toEqual('10.00 MiB'); expect(numberToHumanSize(10485764)).toEqual('10.00 MiB');
expect(numberToHumanSize(-10485764)).toEqual('-10.00 MiB');
}); });
it('should return GiB', () => { it('should return GiB', () => {
expect(numberToHumanSize(10737418240)).toEqual('10.00 GiB'); expect(numberToHumanSize(10737418240)).toEqual('10.00 GiB');
expect(numberToHumanSize(-10737418240)).toEqual('-10.00 GiB');
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment