misc_pterodactyl-panel/resources/scripts/lib/formatters.ts

39 lines
1 KiB
TypeScript
Raw Normal View History

const _CONVERSION_UNIT = 1024;
/**
* Given a value in megabytes converts it back down into bytes.
*/
function mbToBytes(megabytes: number): number {
return Math.floor(megabytes * _CONVERSION_UNIT * _CONVERSION_UNIT);
}
/**
2023-01-12 18:28:11 +00:00
* Given an amount of bytes, converts them into a human-readable string format
* using "1024" as the divisor.
*/
function bytesToString(bytes: number, decimals = 2): string {
const k = _CONVERSION_UNIT;
if (bytes < 1) return '0 Bytes';
decimals = Math.floor(Math.max(0, decimals));
const i = Math.floor(Math.log(bytes) / Math.log(k));
const value = Number((bytes / Math.pow(k, i)).toFixed(decimals));
2022-10-09 22:09:24 +00:00
return `${value} ${['Bytes', 'KiB', 'MiB', 'GiB', 'TiB'][i]}`;
}
/**
* Formats an IPv4 or IPv6 address.
*/
function ip(value: string): string {
2023-01-12 19:44:49 +00:00
// Check if the value is already formatted.
if (value.length > 0 && value[0] === '[') {
return value;
}
return /([a-f0-9:]+:+)+[a-f0-9]+/.test(value) ? `[${value}]` : value;
}
export { ip, mbToBytes, bytesToString };