type CompactStringDecimals = Intl.NumberFormatOptions["minimumFractionDigits"] & BigIntToLocaleStringOptions["minimumFractionDigits"] /** * Formats a number or bigint into compact string with K, M, B, or T suffix. * @param value The number or bigint to format. * @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues). * @returns The formatted number as a string. */ export function formatNumberToCompactString( value: number | bigint, decimals: CompactStringDecimals = 2, ): string { return value.toLocaleString("en-US", { notation: "compact", compactDisplay: "short", maximumFractionDigits: decimals, }) } /** * Normalizes a bigint value to a target precision. * @param value The bigint value to normalize. * @param currentPrecision The current precision of the value. * @param targetPrecision The target precision to normalize to. * @returns The normalized bigint value. */ export const normalizePrecision = ( value: bigint, currentPrecision: number, targetPrecision = currentPrecision, ): bigint => { if (currentPrecision > targetPrecision) { return value / 10n ** BigInt(currentPrecision - targetPrecision) } if (currentPrecision < targetPrecision) { return value * 10n ** BigInt(targetPrecision - currentPrecision) } return value } /** * Utility function to find the maximum value from given `BigInt` values. * @param values Array of BigInt values to find the maximum from. * @returns The maximum value from the provided `BigInt` values. * @dev Works exactly like `Math.max` but for `BigInt` values. */ export const bigIntMax = (...values: bigint[]): bigint => values.reduce((max, value) => (value > max ? value : max))