bytes and percentage

This commit is contained in:
Alex Holliday committed 2026-02-06 19:25:53 +00:00
1 parent 9caa6c830f
commit 1558b27497
5 files changed
+62 -61

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
/**
* Intl.NumberFormat instance for percentage formatting.
* Reused across all formatting calls for performance.
*/
const percentageFormatter = new Intl.NumberFormat("en-US", {
style: "percent",
minimumFractionDigits: 1,
maximumFractionDigits: 1,
});
/**
* Formats a decimal value as a percentage string.
* @param value - Decimal value (e.g., 0.75 for 75%)
* @returns Formatted percentage string (e.g., "75.0%")
* @example
* formatPercentage(0.75) // "75.0%"
* formatPercentage(1) // "100.0%"
* formatPercentage(0.5432) // "54.3%"
*/
export const formatPercentage = (value: number): string => {
if (typeof value !== "number" || Number.isNaN(value)) {
return "0.0%";
}
return percentageFormatter.format(value);
};
/**
* Formats a whole number percentage value as a percentage string.
* @param value - Whole number percentage (e.g., 75 for 75%)
* @returns Formatted percentage string (e.g., "75.0%")
* @example
* formatPercentageFromWhole(75) // "75.0%"
* formatPercentageFromWhole(100) // "100.0%"
* formatPercentageFromWhole(54.32) // "54.3%"
*/
export const formatPercentageFromWhole = (value: number): string => {
return formatPercentage(value / 100);
};
export const getPercentage = (value: number, total: number) => {
if (!value || !total) return 0;
return (value / total) * 100;
};
+1 -27
View File
@@ -1,4 +1,4 @@
import type { CheckDiskInfo, CheckHostInfo } from "@/Types/Check";
import type { CheckHostInfo } from "@/Types/Check";
export const getFrequency = (frequency: number | undefined): string => {
if (!frequency) return "N/A";
@@ -18,32 +18,6 @@ export const getAvgTemp = (temps: number[]): string => {
return `${avgTemp?.toFixed(2)} °C`;
};
export const getGbs = (bytes: number | undefined): string => {
if (!bytes) {
return "N/A";
}
if (bytes === 0) {
return "0 GB";
}
const GB = bytes / (1024 * 1024 * 1024);
const MB = bytes / (1024 * 1024);
if (GB >= 1) {
return GB.toFixed(2) + " GB";
} else {
return MB.toFixed(2) + " MB";
}
};
export const getDiskTotalGbs = (disk?: Partial<CheckDiskInfo>[]): string => {
if (!disk) {
return getGbs(0);
}
const totalBytes = disk?.reduce((acc, disk) => acc + (disk.total_bytes || 0), 0) || 0;
return getGbs(totalBytes);
};
export const getOsAndPlatform = (hostInfo: CheckHostInfo | undefined): string => {
if (!hostInfo) {
return "N/A";