diff --git a/config.yml b/config.yml index c4e810b..ef7dfb4 100644 --- a/config.yml +++ b/config.yml @@ -69,7 +69,7 @@ nodes_settings: details: true # enable nodes details i.e memory and disk usage servers: true location: true - unit: "mb" # Allowed values- "gb", "mb", "tb", or "%" + unit: "mb" # Allowed values: "byte" or "percentage" limit: 100 # Panel Users and Servers Settings diff --git a/handlers/convertUnits.js b/handlers/convertUnits.js index d75d46c..8c9690b 100644 --- a/handlers/convertUnits.js +++ b/handlers/convertUnits.js @@ -1,20 +1,15 @@ -module.exports = function convertUnits(value1, value2, unit) { - unit = unit.toUpperCase(); - const units = ['MB', 'GB', 'TB']; - const conversionFactors = { - 'MB': 1, - 'GB': 1 / 1024, - 'TB': 1 / (1024 * 1024) - }; - - if (unit === '%' || unit === "PERCENTAGE" || unit === "PERCENT") { - const percentage = Math.floor((value1 / value2) * 100); - return `${!percentage ? 0 : percentage}%`; - } else if (units.includes(unit)) { - const formatValue = (value) => Number.isInteger(value) ? value.toLocaleString() : value.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }); - - return `${formatValue(value1 * conversionFactors[unit])} ${unit}/${(value2 === 0 || value2 < 1) ? "Unlimited" : formatValue(value2 * conversionFactors[unit]) + " " + unit}`; - } else { - return `${value1.toLocaleString()} MB/${value2.toLocaleString()} MB`; - } +const prettyBytes = require('prettier-bytes'); + +module.exports = function convertUnits(value1, value2, unit) { + unit = unit.toUpperCase(); + switch (unit) { + case 'PERCENTAGE': + case 'PERCENT': + const percentage = Math.floor((value1 / value2) * 100); + return `${!percentage ? 0 : percentage}%`; + case 'BYTE': + return `${prettyBytes(value1)} / ${prettyBytes(value2)}`; + default: + return `${value1.toLocaleString()} ${unit}/${value2.toLocaleString()} ${unit}`; + } } \ No newline at end of file diff --git a/package.json b/package.json index c9b69ca..daf333f 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "cli-color": "2.0.4", "discord.js": "14.16.1", "dotenv": "16.4.5", - "js-yaml": "4.1.0" + "js-yaml": "4.1.0", + "prettier-bytes": "1.0.4" }, "engines": { "node": ">=18"