diff options
Diffstat (limited to 'frontend/lib/utils.ts')
| -rw-r--r-- | frontend/lib/utils.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/frontend/lib/utils.ts b/frontend/lib/utils.ts new file mode 100644 index 0000000..7a1e30c --- /dev/null +++ b/frontend/lib/utils.ts | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | /** | ||
| 2 | * Formats a size in bytes into a human-readable string | ||
| 3 | * @param bytes Size in bytes | ||
| 4 | * @returns Formatted size string (e.g., "1.5 KB", "2.3 MB", "1.2 GB") | ||
| 5 | */ | ||
| 6 | export function formatSize(bytes: number | null): string { | ||
| 7 | if (bytes === null || bytes === 0) { | ||
| 8 | return '-' | ||
| 9 | } | ||
| 10 | |||
| 11 | const units = ['B', 'KB', 'MB', 'GB', 'TB'] | ||
| 12 | let size = bytes | ||
| 13 | let unitIndex = 0 | ||
| 14 | |||
| 15 | while (size >= 1024 && unitIndex < units.length - 1) { | ||
| 16 | size /= 1024 | ||
| 17 | unitIndex++ | ||
| 18 | } | ||
| 19 | |||
| 20 | // Format with appropriate decimal places | ||
| 21 | if (size < 10 && unitIndex > 0) { | ||
| 22 | return `${size.toFixed(1)} ${units[unitIndex]}` | ||
| 23 | } else { | ||
| 24 | return `${Math.round(size)} ${units[unitIndex]}` | ||
| 25 | } | ||
| 26 | } \ No newline at end of file | ||
