Created
July 2, 2018 06:09
-
-
Save andreasvirkus/ae9a9d8c98ea4af2a5514fc89ea3b09b to your computer and use it in GitHub Desktop.
Modified version of https://stackoverflow.com/a/14919494/2803743
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (bytes) => { | |
const thresh = 1024; | |
if (Math.abs(bytes) < thresh) { | |
return bytes + ' B'; | |
} | |
const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
let u = -1; | |
do { | |
bytes /= thresh; | |
++u; | |
} while (Math.abs(bytes) >= thresh && u < units.length - 1); | |
return `${bytes.toFixed(1)} ${units[u]}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment