mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-10 21:05:00 +00:00
Separating functions to strings
library
This commit is contained in:
parent
59da1cb7eb
commit
9271e67e7a
3 changed files with 41 additions and 41 deletions
39
frontend/src/lib/utils/strings.js
Normal file
39
frontend/src/lib/utils/strings.js
Normal file
|
@ -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';
|
||||||
|
};
|
|
@ -1,8 +1,9 @@
|
||||||
<script>
|
<script>
|
||||||
import { DataHandler } from '@vincjo/datatables/remote';
|
import { DataHandler } from '@vincjo/datatables/remote';
|
||||||
import { format, formatDistance } from 'date-fns';
|
import { format, formatDistance } from 'date-fns';
|
||||||
import { loadData, loadNodeInfo, formatBytes } from './api-handler';
|
import { loadData, loadNodeInfo } from './api-handler';
|
||||||
import { onMount, onDestroy } from 'svelte';
|
import { onMount, onDestroy } from 'svelte';
|
||||||
|
import { formatHashes, formatBytes } from '$lib/utils/strings';
|
||||||
import {
|
import {
|
||||||
DtSrRowsPerPage,
|
DtSrRowsPerPage,
|
||||||
DtSrThSort,
|
DtSrThSort,
|
||||||
|
@ -11,29 +12,6 @@
|
||||||
DtSrPagination
|
DtSrPagination
|
||||||
} from '$lib/components/datatables/server';
|
} from '$lib/components/datatables/server';
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {number} n
|
|
||||||
* @param {number} p
|
|
||||||
*/
|
|
||||||
function maxPrecision(n, p) {
|
|
||||||
return parseFloat(n.toFixed(p));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {number} h
|
|
||||||
*/
|
|
||||||
function 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';
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @param {number | null } runtime */
|
/** @param {number | null } runtime */
|
||||||
function parseRuntime(runtime) {
|
function parseRuntime(runtime) {
|
||||||
return runtime === null ? '' : runtime.toLocaleString(undefined) + 's';
|
return runtime === null ? '' : runtime.toLocaleString(undefined) + 's';
|
||||||
|
|
|
@ -14,23 +14,6 @@ export const loadNodeInfo = async (nodeId) => {
|
||||||
return json.data;
|
return json.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @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]}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getParams = ({ pageNumber, rowsPerPage, sort, filters }) => {
|
const getParams = ({ pageNumber, rowsPerPage, sort, filters }) => {
|
||||||
let params = `page=${pageNumber}&limit=${rowsPerPage}`;
|
let params = `page=${pageNumber}&limit=${rowsPerPage}`;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue