Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

445 B
Raw Blame
export function formatFileSize(bytes: number): string {
  if (!Number.isFinite(bytes) || bytes < 0) return '';
  if (bytes < 1024) return `${bytes} B`;
  const units = ['KiB', 'MiB', 'GiB', 'TiB'];
  let value = bytes / 1024;
  let unitIdx = 0;
  while (value >= 1024 && unitIdx < units.length - 1) {
    value /= 1024;
    unitIdx += 1;
  }
  const formatted = value.toFixed(1).replace(/\.0$/, '');
  return `${formatted} ${units[unitIdx]}`;
}