Skip to content

Instantly share code, notes, and snippets.

@mutalis
Last active April 24, 2019 03:17
Show Gist options
  • Save mutalis/7fbec92a78c66c2231f161f8d99e094b to your computer and use it in GitHub Desktop.
Save mutalis/7fbec92a78c66c2231f161f8d99e094b to your computer and use it in GitHub Desktop.
const humanSize = bytes => {
if (bytes === 0) return 'n/a'
const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024))
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`
}
console.log(humanSize(0))
console.log(humanSize(-1000))
console.log(humanSize(1024))
console.log(humanSize(2048))
console.log(humanSize(9974327))
console.log(humanSize(10000000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment