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}