JSFiddle - React, Tailwind, and code Playground
by Nicholas Berlette
JavaScript
function file_size(size) {
var i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}
function readableFileSize(size) {
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = 0;
while (size >= 1024) {
size /= 1024;
++i;
}
return size.toFixed(1) + ' ' + units[i];
}
var filesize = new function () {
const isIEC = false;
this.size = (a, b, c, d, e) => (
b = Math,
c = b.log,
d = this.isIEC === true ? 1024 : 1000,
e = c(a) / c(d) | 0,
a / b.pow(d, e)
).toFixed(2) + ' ' + (e ? 'kMGTPEZY'[--e] + 'B' : 'Bytes');
return {
// kB,MB,GB,TB,PB,EB,ZB,YB
SI: () => {
filesize.isIEC = false
return filesize.size(arguments)
},
// KiB,MiB,GiB,TiB,PiB,EiB,ZiB,YiB
IEC: () => {
filesize.isIEC = true
return filesize.size(arguments)
}
}
}();