summaryrefslogtreecommitdiff
path: root/frontend/lib/utils.ts
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-11 11:51:39 +0100
committerdiogo464 <[email protected]>2025-08-11 11:51:39 +0100
commit4af66f418b6837b6441b4e8eaf2d8ede585238b9 (patch)
tree34a4e913a2848515166b2ac0489794419a33bfcc /frontend/lib/utils.ts
parent0d3488a3811c8d58bd570af64cc29840df9ba439 (diff)
snapshot
Diffstat (limited to 'frontend/lib/utils.ts')
-rw-r--r--frontend/lib/utils.ts26
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 */
6export 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