From: Michael Tremer Date: Mon, 7 Jul 2025 16:01:31 +0000 (+0000) Subject: frontend: Add some helper functions to render some basic things X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac68c214735c63f9319b576cc1cd83f2c0f268b8;p=pbs.git frontend: Add some helper functions to render some basic things Signed-off-by: Michael Tremer --- diff --git a/frontend/src/utils/format.ts b/frontend/src/utils/format.ts new file mode 100644 index 00000000..f8798fbb --- /dev/null +++ b/frontend/src/utils/format.ts @@ -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; + } +}