13 lines
542 B
TypeScript
13 lines
542 B
TypeScript
export function formatBytes(bytes: number) {
|
|
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B';
|
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
const power = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);
|
|
const value = bytes / 1024 ** power;
|
|
return `${value >= 10 || power === 0 ? value.toFixed(0) : value.toFixed(1)} ${units[power]}`;
|
|
}
|
|
|
|
export function usagePercent(value: number, limit: number) {
|
|
if (limit <= 0) return 0;
|
|
return Math.min(100, Math.max(0, Math.round((value / limit) * 100)));
|
|
}
|