From 863009dcaaf89f71e2be25165e8555731c85bf04 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Wed, 16 Oct 2024 15:55:35 +0200 Subject: [PATCH] Refactor size formatting for readability --- util/common/format.go | 22 +++++++++------------- web/assets/js/util/common.js | 30 ++++++++++++------------------ 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/util/common/format.go b/util/common/format.go index 1ea10877..c73e3a01 100644 --- a/util/common/format.go +++ b/util/common/format.go @@ -4,18 +4,14 @@ import ( "fmt" ) -func FormatTraffic(trafficBytes int64) (size string) { - if trafficBytes < 1024 { - return fmt.Sprintf("%.2fB", float64(trafficBytes)/float64(1)) - } else if trafficBytes < (1024 * 1024) { - return fmt.Sprintf("%.2fKB", float64(trafficBytes)/float64(1024)) - } else if trafficBytes < (1024 * 1024 * 1024) { - return fmt.Sprintf("%.2fMB", float64(trafficBytes)/float64(1024*1024)) - } else if trafficBytes < (1024 * 1024 * 1024 * 1024) { - return fmt.Sprintf("%.2fGB", float64(trafficBytes)/float64(1024*1024*1024)) - } else if trafficBytes < (1024 * 1024 * 1024 * 1024 * 1024) { - return fmt.Sprintf("%.2fTB", float64(trafficBytes)/float64(1024*1024*1024*1024)) - } else { - return fmt.Sprintf("%.2fEB", float64(trafficBytes)/float64(1024*1024*1024*1024*1024)) +func FormatTraffic(trafficBytes int64) string { + units := []string{"B", "KB", "MB", "GB", "TB", "PB"} + unitIndex := 0 + size := float64(trafficBytes) + + for size >= 1024 && unitIndex < len(units)-1 { + size /= 1024 + unitIndex++ } + return fmt.Sprintf("%.2f%s", size, units[unitIndex]) } diff --git a/web/assets/js/util/common.js b/web/assets/js/util/common.js index 5f20d7d9..6e77dea4 100644 --- a/web/assets/js/util/common.js +++ b/web/assets/js/util/common.js @@ -1,25 +1,19 @@ const ONE_KB = 1024; -const ONE_MB = ONE_KB * 1024; -const ONE_GB = ONE_MB * 1024; -const ONE_TB = ONE_GB * 1024; -const ONE_PB = ONE_TB * 1024; +const units = ["B", "KB", "MB", "GB", "TB", "PB"]; function sizeFormat(size) { if (size < 0) { return "0 B"; - } else if (size < ONE_KB) { - return size.toFixed(0) + " B"; - } else if (size < ONE_MB) { - return (size / ONE_KB).toFixed(2) + " KB"; - } else if (size < ONE_GB) { - return (size / ONE_MB).toFixed(2) + " MB"; - } else if (size < ONE_TB) { - return (size / ONE_GB).toFixed(2) + " GB"; - } else if (size < ONE_PB) { - return (size / ONE_TB).toFixed(2) + " TB"; - } else { - return (size / ONE_PB).toFixed(2) + " PB"; } + + let index = 0; + + while (size >= ONE_KB && index < units.length - 1) { + size /= ONE_KB; + index++; + } + + return `${size.toFixed(index === 0 ? 0 : 2)} ${units[index]}`; } function cpuSpeedFormat(speed) { @@ -59,7 +53,7 @@ function formatSecond(second) { return (second / 3600).toFixed(0) + 'h'; } else { day = Math.floor(second / 3600 / 24); - remain = ((second/3600) - (day*24)).toFixed(0); + remain = ((second / 3600) - (day * 24)).toFixed(0); return day + 'd' + (remain > 0 ? ' ' + remain + 'h' : ''); } } @@ -149,7 +143,7 @@ function userExpiryColor(threshold, client, isDark = false) { return isDark ? '#2c3950' : '#bcbcbc'; } now = new Date().getTime(), - expiry = client.expiryTime; + expiry = client.expiryTime; switch (true) { case expiry === null: return "#7a316f"; // purple