Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Last active April 11, 2019 01:07
Show Gist options
  • Save TravisMullen/f36bb055b31e3555e42d977cecd84d11 to your computer and use it in GitHub Desktop.
Save TravisMullen/f36bb055b31e3555e42d977cecd84d11 to your computer and use it in GitHub Desktop.
Useful Util function for everyday life.
/**
* 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