refactor: move formatting related code into separate file (#4568)

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
Ophelia Goldstein
2025-10-13 23:50:06 +03:00
committed by GitHub
co-authored by Alexandr
parent c4a59b5afa
commit 5df2e634e9
8 changed files with 58 additions and 63 deletions
+50
View File
@@ -0,0 +1,50 @@
/**
* Retrieves num with suffix k(thousands) precise to given decimal places.
*
* @param {number} num The number to format.
* @param {number=} precision The number of decimal places to include.
* @returns {string|number} The formatted number.
*/
const kFormatter = (num, precision) => {
const abs = Math.abs(num);
const sign = Math.sign(num);
if (typeof precision === "number" && !isNaN(precision)) {
return (sign * (abs / 1000)).toFixed(precision) + "k";
}
if (abs < 1000) {
return sign * abs;
}
return sign * parseFloat((abs / 1000).toFixed(1)) + "k";
};
/**
* Convert bytes to a human-readable string representation.
*
* @param {number} bytes The number of bytes to convert.
* @returns {string} The human-readable representation of bytes.
* @throws {Error} If bytes is negative or too large.
*/
const formatBytes = (bytes) => {
if (bytes < 0) {
throw new Error("Bytes must be a non-negative number");
}
if (bytes === 0) {
return "0 B";
}
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
const base = 1024;
const i = Math.floor(Math.log(bytes) / Math.log(base));
if (i >= sizes.length) {
throw new Error("Bytes is too large to convert to a human-readable string");
}
return `${(bytes / Math.pow(base, i)).toFixed(1)} ${sizes[i]}`;
};
export { kFormatter, formatBytes };
-1
View File
@@ -10,7 +10,6 @@ export {
ERROR_CARD_LENGTH,
renderError,
encodeHTML,
kFormatter,
parseBoolean,
parseArray,
clampValue,
-51
View File
@@ -76,28 +76,6 @@ const iconWithLabel = (icon, label, testid, iconSize) => {
return flexLayout({ items: [iconSvg, text], gap: 20 }).join("");
};
/**
* Retrieves num with suffix k(thousands) precise to given decimal places.
*
* @param {number} num The number to format.
* @param {number=} precision The number of decimal places to include.
* @returns {string|number} The formatted number.
*/
const kFormatter = (num, precision) => {
const abs = Math.abs(num);
const sign = Math.sign(num);
if (typeof precision === "number" && !isNaN(precision)) {
return (sign * (abs / 1000)).toFixed(precision) + "k";
}
if (abs < 1000) {
return sign * abs;
}
return sign * parseFloat((abs / 1000).toFixed(1)) + "k";
};
/**
* Returns boolean if value is either "true" or "false" else the value as it is.
*
@@ -399,40 +377,12 @@ const dateDiff = (d1, d2) => {
return Math.round(diff / (1000 * 60));
};
/**
* Convert bytes to a human-readable string representation.
*
* @param {number} bytes The number of bytes to convert.
* @returns {string} The human-readable representation of bytes.
* @throws {Error} If bytes is negative or too large.
*/
const formatBytes = (bytes) => {
if (bytes < 0) {
throw new Error("Bytes must be a non-negative number");
}
if (bytes === 0) {
return "0 B";
}
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
const base = 1024;
const i = Math.floor(Math.log(bytes) / Math.log(base));
if (i >= sizes.length) {
throw new Error("Bytes is too large to convert to a human-readable string");
}
return `${(bytes / Math.pow(base, i)).toFixed(1)} ${sizes[i]}`;
};
export {
ERROR_CARD_LENGTH,
renderError,
createLanguageNode,
iconWithLabel,
encodeHTML,
kFormatter,
parseBoolean,
parseArray,
clampValue,
@@ -445,5 +395,4 @@ export {
chunkArray,
parseEmojis,
dateDiff,
formatBytes,
};