diff --git a/src/utils.js b/src/utils.js index 47de7412e0..8178e754eb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -257,21 +257,14 @@ export const isValidDate = (date) => { return Boolean(formattedValue.length <= 10); }; -export const getFileSizeToClosestByte = (fileSize, numberOfDivides = 0) => { - if (fileSize > 1000) { - const updatedSize = fileSize / 1000; - const incrementNumberOfDivides = numberOfDivides + 1; - return getFileSizeToClosestByte(updatedSize, incrementNumberOfDivides); - } - const fileSizeFixedDecimal = Number.parseFloat(fileSize).toFixed(2); - switch (numberOfDivides) { - case 1: - return `${fileSizeFixedDecimal} KB`; - case 2: - return `${fileSizeFixedDecimal} MB`; - case 3: - return `${fileSizeFixedDecimal} GB`; - default: - return `${fileSizeFixedDecimal} B`; +export const getFileSizeToClosestByte = (fileSize) => { + let divides = 0; + let size = fileSize; + while (size > 1000 && divides < 4) { + size /= 1000; + divides += 1; } + const units = ['B', 'KB', 'MB', 'GB', 'TB']; + const fileSizeFixedDecimal = Number.parseFloat(size).toFixed(2); + return `${fileSizeFixedDecimal} ${units[divides]}`; }; diff --git a/src/utils.test.js b/src/utils.test.js index f2e9995dd9..3c0ddabf7f 100644 --- a/src/utils.test.js +++ b/src/utils.test.js @@ -22,5 +22,15 @@ describe('FilesAndUploads utils', () => { const actualSize = getFileSizeToClosestByte(2034190000); expect(expectedSize).toEqual(actualSize); }); + it('should return file size with TB for terabytes', () => { + const expectedSize = '1.99 TB'; + const actualSize = getFileSizeToClosestByte(1988034190000); + expect(expectedSize).toEqual(actualSize); + }); + it('should return file size with TB for larger numbers', () => { + const expectedSize = '1234.56 TB'; + const actualSize = getFileSizeToClosestByte(1234560000000000); + expect(expectedSize).toEqual(actualSize); + }); }); });