Skip to content

Instantly share code, notes, and snippets.

@romannmk
Created January 17, 2019 10:20
Show Gist options
  • Save romannmk/cb7261426d3ce2f434172cdd6e6636f3 to your computer and use it in GitHub Desktop.
Save romannmk/cb7261426d3ce2f434172cdd6e6636f3 to your computer and use it in GitHub Desktop.
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