Last active
April 11, 2019 01:07
-
-
Save TravisMullen/f36bb055b31e3555e42d977cecd84d11 to your computer and use it in GitHub Desktop.
Useful Util function for everyday life.
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
/** | |
* File size formatting. | |
* | |
* @param bytes | |
* @returns {*} | |
* @private | |
*/ | |
const _sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'] | |
export const bytesToSize = bytes => { | |
if (bytes === 0) { | |
return `${bytes} ${_sizes[bytes]}` | |
} | |
const size = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))) | |
const rounded = Math.round(bytes / Math.pow(1024, size), 2) | |
return `${rounded} ${_sizes[size]}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment