eb71fcf209
- Delete unused adminEvents-enhanced.js, backupService.original.js, databaseBackup.example.js, s3Storage.example.js, ThemeCustomizer.tsx - Extract shared formatBytes to utils/formatBytes.js (was copied 4x) - Centralize formatNumberInTemplate + next-document-number logic in utils/documentSequences.js (was copied in invoice/quote/contract services)
19 lines
471 B
JavaScript
19 lines
471 B
JavaScript
/**
|
|
* Human-readable byte size, e.g. 1536 -> "1.5 KB".
|
|
* Falsy/undefined input returns '0 Bytes'.
|
|
*/
|
|
function formatBytes(bytes, decimals = 2) {
|
|
if (!bytes) {
|
|
return '0 Bytes';
|
|
}
|
|
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
|
}
|
|
|
|
module.exports = { formatBytes };
|