From 9271e67e7a1d62b6cc50febb9dc9eab16ae8ea3f Mon Sep 17 00:00:00 2001 From: ditatompel Date: Tue, 7 May 2024 21:07:24 +0700 Subject: [PATCH] Separating functions to `strings` library --- frontend/src/lib/utils/strings.js | 39 +++++++++++++++++++ .../(front)/remote-nodes/logs/+page.svelte | 26 +------------ .../(front)/remote-nodes/logs/api-handler.js | 17 -------- 3 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 frontend/src/lib/utils/strings.js diff --git a/frontend/src/lib/utils/strings.js b/frontend/src/lib/utils/strings.js new file mode 100644 index 0000000..9a68d93 --- /dev/null +++ b/frontend/src/lib/utils/strings.js @@ -0,0 +1,39 @@ +/** + * @param {number} bytes + * @param {number} decimals + * @returns {string} + */ +export const formatBytes = (bytes, decimals = 2) => { + if (!+bytes) return '0 Bytes'; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; +}; + +/** + * @param {number} n + * @param {number} p + */ +const maxPrecision = (n, p) => { + return parseFloat(n.toFixed(p)); +}; + +/** + * @param {number} h + */ +export const formatHashes = (h) => { + if (h < 1e-12) return '0 H'; + else if (h < 1e-9) return maxPrecision(h * 1e12, 0) + ' pH'; + else if (h < 1e-6) return maxPrecision(h * 1e9, 0) + ' nH'; + else if (h < 1e-3) return maxPrecision(h * 1e6, 0) + ' μH'; + else if (h < 1) return maxPrecision(h * 1e3, 0) + ' mH'; + else if (h < 1e3) return h + ' H'; + else if (h < 1e6) return maxPrecision(h * 1e-3, 2) + ' KH'; + else if (h < 1e9) return maxPrecision(h * 1e-6, 2) + ' MH'; + else return maxPrecision(h * 1e-9, 2) + ' GH'; +}; diff --git a/frontend/src/routes/(front)/remote-nodes/logs/+page.svelte b/frontend/src/routes/(front)/remote-nodes/logs/+page.svelte index 597eb6e..e50d158 100644 --- a/frontend/src/routes/(front)/remote-nodes/logs/+page.svelte +++ b/frontend/src/routes/(front)/remote-nodes/logs/+page.svelte @@ -1,8 +1,9 @@