From 2bb73ce8280c3e71d4c95f87324a7d92b42d8ef2 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 16 May 2023 16:51:39 +0000 Subject: [PATCH] fix account network usage Signed-off-by: Arthur Lu --- scripts/account.js | 36 +++++++++++++++++++++++------------- scripts/config.js | 6 +++--- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/scripts/account.js b/scripts/account.js index 90aa7e0..6377897 100644 --- a/scripts/account.js +++ b/scripts/account.js @@ -1,16 +1,25 @@ -import {requestAPI, goToPage, getCookie, setTitleAndHeader} from "./utils.js"; +import { requestAPI, goToPage, getCookie, setTitleAndHeader } from "./utils.js"; window.addEventListener("DOMContentLoaded", init); -let SIPrefix = [ - "", - "Ki", - "Mi", - "Gi", - "Ti" -] +let prefixes = { + 1024: [ + "", + "Ki", + "Mi", + "Gi", + "Ti" + ], + 1000: [ + "", + "K", + "M", + "G", + "T" + ] +} -async function init () { +async function init() { setTitleAndHeader(); let cookie = document.cookie; if (cookie === "") { @@ -26,7 +35,7 @@ async function init () { buildResourceTable(resources, "#resource-table"); } -function buildResourceTable (resources, tableid) { +function buildResourceTable(resources, tableid) { if (resources instanceof Object) { let table = document.querySelector(tableid); @@ -48,15 +57,16 @@ function buildResourceTable (resources, tableid) { function parseNumber(value, unitData) { let compact = unitData.compact; let multiplier = unitData.multiplier; + let base = unitData.base; let unit = unitData.unit; value = multiplier * value; if (value <= 0) { return `0 ${unit}`; } else if (compact) { - let exponent = Math.floor(Math.log2(value) / 10); - value = value / 1024 ** exponent; - let unitPrefix = SIPrefix[exponent]; + let exponent = Math.floor(Math.log(value) / Math.log(base)); + value = value / base ** exponent; + let unitPrefix = prefixes[base][exponent]; return `${value} ${unitPrefix}${unit}` } else { diff --git a/scripts/config.js b/scripts/config.js index 47fdb4f..586a223 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -442,17 +442,17 @@ function populateNetworks () { }); let ordered_keys = getOrdered(networks); ordered_keys.forEach(element => { - addNetworkLine("networks", `${prefix}${element}`, networks[element]); + addNetworkLine("networks", prefix, element, networks[element]); }); } -function addNetworkLine (fieldset, netID, netDetails) { +function addNetworkLine (fieldset, prefix, netID, netDetails) { let field = document.querySelector(`#${fieldset}`); let icon = document.createElement("img"); icon.src = "images/resources/network.svg"; icon.alt = netID; - icon.dataset.network = netID; + icon.dataset.network = `${prefix}${netID}`; icon.dataset.netvals = netDetails; field.appendChild(icon);