webseite-grav/plugins/admin/themes/grav/app/utils/formatbytes.js

12 lines
368 B
JavaScript
Raw Normal View History

2021-04-30 11:23:26 +02:00
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
export default function formatBytes(bytes, decimals) {
if (bytes === 0) return '0 Byte';
let k = 1000;
let value = Math.floor(Math.log(bytes) / Math.log(k));
let decimal = decimals + 1 || 3;
return (bytes / Math.pow(k, value)).toPrecision(decimal) + ' ' + sizes[value];
}