Separating functions to strings library

This commit is contained in:
ditatompel 2024-05-07 21:07:24 +07:00
parent 59da1cb7eb
commit 9271e67e7a
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
3 changed files with 41 additions and 41 deletions

View 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';
};

View file

@ -1,8 +1,9 @@
<script>
import { DataHandler } from '@vincjo/datatables/remote';
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 { formatHashes, formatBytes } from '$lib/utils/strings';
import {
DtSrRowsPerPage,
DtSrThSort,
@ -11,29 +12,6 @@
DtSrPagination
} 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 */
function parseRuntime(runtime) {
return runtime === null ? '' : runtime.toLocaleString(undefined) + 's';

View file

@ -14,23 +14,6 @@ export const loadNodeInfo = async (nodeId) => {
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 }) => {
let params = `page=${pageNumber}&limit=${rowsPerPage}`;