Last active
April 11, 2017 15:30
-
-
Save e7d/48dabcb095cb4a27f1124944527b05c6 to your computer and use it in GitHub Desktop.
Human readable file size, extending Number type prototype
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
Object.defineProperty(Number.prototype, 'fileSize', { | |
value: function humanFileSize(si = false) { | |
const divider = si ? 1e3 : 1024; | |
const i = Math.floor(Math.log(this) / Math.log(divider)); | |
const fileSize = | |
(this / Math.pow(divider, i)).toFixed(2) + | |
' ' + ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'][i] + | |
(si ? 'i' : ''); | |
return fileSize.toString(); | |
}, | |
writable: false, | |
enumerable: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment