]> git.ipfire.org Git - pbs.git/commitdiff
frontend: Add some helper functions to render some basic things
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Jul 2025 16:01:31 +0000 (16:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Jul 2025 16:01:31 +0000 (16:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
frontend/src/utils/format.ts [new file with mode: 0644]

diff --git a/frontend/src/utils/format.ts b/frontend/src/utils/format.ts
new file mode 100644 (file)
index 0000000..f8798fb
--- /dev/null
@@ -0,0 +1,25 @@
+export function formatSize(bytes: number, decimals = 1): string {
+       const base = 1024;
+
+       // Shortcut so we won"t divide by zero
+       if (bytes === 0)
+               return "0 B";
+
+       const units = ["B", "KiB", "MiB", "GiB", "TiB", "EiB", "PiB"];
+
+       // Find the most appropriate unit
+       const unit = Math.floor(Math.log(bytes) / Math.log(base));
+
+       // Format the number
+       const size = parseFloat((bytes / Math.pow(base, unit)).toFixed(decimals));
+
+       return `${size} ${units[unit]}`;
+}
+
+export function formatHostname(url: string): string {
+       try {
+               return new URL(url).hostname;
+       } catch {
+               return url;
+       }
+}