Skip to content

Instantly share code, notes, and snippets.

@e7d
Last active April 11, 2017 15:30
Show Gist options
  • Save e7d/48dabcb095cb4a27f1124944527b05c6 to your computer and use it in GitHub Desktop.
Save e7d/48dabcb095cb4a27f1124944527b05c6 to your computer and use it in GitHub Desktop.
Human readable file size, extending Number type prototype
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