Last active
April 24, 2019 03:17
-
-
Save mutalis/7fbec92a78c66c2231f161f8d99e094b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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