Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created July 2, 2018 06:09
Show Gist options
  • Save andreasvirkus/ae9a9d8c98ea4af2a5514fc89ea3b09b to your computer and use it in GitHub Desktop.
Save andreasvirkus/ae9a9d8c98ea4af2a5514fc89ea3b09b to your computer and use it in GitHub Desktop.
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