summaryrefslogtreecommitdiff
path: root/lib/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.ts')
-rw-r--r--lib/utils.ts12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/utils.ts b/lib/utils.ts
index c09589d5..33b5a0c2 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -182,4 +182,16 @@ export function formatDateToQuarter(dateString: string | null | undefined): stri
}
return `${year} ${quarter}Q`
+}
+
+export function formatBytes(bytes: number, decimals: number = 2): string {
+ if (bytes === 0) return "0 Bytes"
+
+ const k = 1024
+ const dm = decimals < 0 ? 0 : decimals
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
+
+ const i = Math.floor(Math.log(bytes) / Math.log(k))
+
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]
} \ No newline at end of file