INNER CODE UNIT · TypeScript
formatBytes
steipete/summarize · apps/chrome-extension/src/entrypoints/options/main.ts:244
function formatBytes(bytes: number): string {
if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
const units = ["B", "KB", "MB", "GB"];
let value = bytes;
let unitIndex = 0;
while (value >= 1024 && unitIndex < units.length - 1) {
value /= 1024;
unitIndex += 1;
}
const precision = value >= 10 || unitIndex === 0 ? 0 : 1;
return `${uiNumber(value, { minimumFractionDigits: precision, maximumFractionDigits: precision, useGrouping: false })} ${units[unitIndex]}`;
}
async function sendBrowserCacheMessage(type: "browser-cache:stats" | "browser-cache:clear") {
return (await chrome.runtime.sendMessage({ type })) as {
ok?: boolean;
stats?: CacheStats | null;
};