Created
January 17, 2019 10:20
-
-
Save romannmk/cb7261426d3ce2f434172cdd6e6636f3 to your computer and use it in GitHub Desktop.
This file contains 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
const units = ['bytes', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']; | |
export const getSizeFromBytes = x => { | |
let l = 0, | |
n = parseInt(x, 10) || 0; | |
while (n >= 1024 && ++l) n = n / 1024; | |
// include a decimal point and a tenths-place digit if presenting | |
// less than ten of KB or greater units | |
return n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment