Skip to content

Instantly share code, notes, and snippets.

@celsobessa
Created November 8, 2020 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celsobessa/ccafc42e323f9880bb0558b123089563 to your computer and use it in GitHub Desktop.
Save celsobessa/ccafc42e323f9880bb0558b123089563 to your computer and use it in GitHub Desktop.
Convert byte units to reasonable units
// https://medium.com/javascript-in-plain-english/7-javascript-utility-functions-to-improve-your-efficiency-79d27132f186
function bytesToSize (bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
var i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment