/** * Shared formatting utilities for tsview web components */ /** * Format a byte size into a human-readable string * @param bytes - Size in bytes (can be undefined for S3 folders) * @returns Formatted size string (e.g., "1.5 MB") */ export function formatSize(bytes?: number): string { if (bytes === undefined || bytes === null) return '-'; if (bytes === 0) return '0 B'; const units = ['B', 'KB', 'MB', 'GB', 'TB']; let size = bytes; let unitIndex = 0; while (size >= 1024 && unitIndex < units.length - 1) { size /= 1024; unitIndex++; } return `${size.toFixed(unitIndex > 0 ? 1 : 0)} ${units[unitIndex]}`; } /** * Format a count into a compact human-readable string * @param count - The count number (can be undefined) * @returns Formatted count string (e.g., "1.5K", "2.3M") */ export function formatCount(count?: number): string { if (count === undefined || count === null) return ''; if (count >= 1000000) return `${(count / 1000000).toFixed(1)}M`; if (count >= 1000) return `${(count / 1000).toFixed(1)}K`; return count.toString(); } /** * Extract the file name from a path * @param path - Full file path (e.g., "folder/subfolder/file.txt") * @returns File name (e.g., "file.txt") */ export function getFileName(path: string): string { const parts = path.replace(/\/$/, '').split('/'); return parts[parts.length - 1] || path; }