1function niceBytes(x){
2 const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
3 let l = 0, n = parseInt(x, 10) || 0;
4 while(n >= 1024 && ++l){
5 n = n/1024;
6 }
7 return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
8}
1function formatBytes(bytes, decimals = 2) {
2 if (bytes === 0) return '0 Bytes';
3
4 const k = 1024;
5 const dm = decimals < 0 ? 0 : decimals;
6 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
7
8 const i = Math.floor(Math.log(bytes) / Math.log(k));
9
10 return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
11}