@reservoir0x/relay-kit-ui
Version:
Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.
171 lines • 6.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.truncateBalance = exports.formatNumber = exports.formatFixedLength = exports.formatBN = exports.formatDollar = void 0;
const viem_1 = require("viem");
const browser_js_1 = require("./browser.js");
const { format: formatUsdCurrency } = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
function formatDollar(price) {
if (price === undefined || price === null || price === 0) {
return '-';
}
if (Math.abs(price) >= 1000000000) {
return '>$1B';
}
const formatted = formatUsdCurrency(price);
if (formatted === '$0.00' && price && price > 0) {
return '< $0.01';
}
return formatted;
}
exports.formatDollar = formatDollar;
function formatNumber(amount, maximumFractionDigits = 2, compact) {
const { format } = new Intl.NumberFormat('en-US', {
maximumFractionDigits: maximumFractionDigits,
notation: compact ? 'compact' : 'standard'
});
if (!amount) {
return '-';
}
const numAmount = Number(amount);
if (numAmount >= 1000000000) {
return '>1B';
}
if (numAmount > 0) {
const threshold = Math.pow(10, -maximumFractionDigits);
if (numAmount < threshold) {
const thresholdStr = threshold.toFixed(maximumFractionDigits);
return `< ${thresholdStr}`;
}
}
return format(numAmount).replace(/\.?0+$/, '');
}
exports.formatNumber = formatNumber;
const truncateFractionAndFormat = (parts, digits) => {
return parts
.map(({ type, value }) => {
if (type !== 'fraction' || !value || value.length < digits) {
return value;
}
let formattedValue = '';
for (let idx = 0; idx < value.length && idx < digits; idx++) {
formattedValue += value[idx];
}
return formattedValue;
})
.reduce((string, part) => string + part);
};
function formatBN(amount, maximumFractionDigits, decimals = 18, compact = true, formatOptionsParam = {}) {
if (typeof amount === 'undefined' || amount === null)
return '-';
const amountToFormat = typeof amount === 'number'
? amount
: +(0, viem_1.formatUnits)(BigInt(amount), decimals ?? 18);
if (amountToFormat === 0) {
return `${amountToFormat}`;
}
const amountFraction = `${amount}`.split('.')[1];
const isSafari = (0, browser_js_1.isSafariBrowser)();
const formatOptions = {
minimumFractionDigits: 0,
maximumFractionDigits: 20,
useGrouping: true,
notation: compact ? 'compact' : 'standard',
compactDisplay: 'short',
...formatOptionsParam
};
if (isSafari) {
formatOptions.roundingPriority = 'lessPrecision';
}
const parts = new Intl.NumberFormat('en-US', formatOptions).formatToParts(amountToFormat);
if (isSafari) {
const partTypes = parts.map((part) => part.type);
const partsIncludesFraction = partTypes.includes('fraction');
const partsIncludeCompactIdentifier = partTypes.includes('compact');
if (amountFraction) {
if (!partsIncludesFraction && !partsIncludeCompactIdentifier) {
const integerIndex = parts.findIndex((part) => part.type === 'integer');
parts.splice(integerIndex + 1, 0, {
type: 'decimal',
value: '.'
}, {
type: 'fraction',
value: amountFraction
});
}
}
else if (!partsIncludesFraction && partsIncludeCompactIdentifier) {
const compactIdentifier = parts.find((part) => part.type === 'compact');
const integerIndex = parts.findIndex((part) => part.type === 'integer');
const integer = parts[integerIndex];
if (compactIdentifier?.value === 'K' && integer) {
const fraction = `${amount}`.replace(integer.value, '')[0];
if (fraction && Number(fraction) > 0) {
parts.splice(integerIndex + 1, 0, {
type: 'decimal',
value: '.'
}, {
type: 'fraction',
value: fraction
});
}
}
}
}
if (parts && parts.length > 0) {
const lowestValue = Number(`0.${new Array(maximumFractionDigits).join('0')}1`);
if (amountToFormat > 1000) {
return truncateFractionAndFormat(parts, 1);
}
else if (amountToFormat < 1 && amountToFormat < lowestValue) {
return `< ${lowestValue}`;
}
else {
return truncateFractionAndFormat(parts, maximumFractionDigits);
}
}
else {
return typeof amount === 'string' || typeof amount === 'number'
? `${amount}`
: '';
}
}
exports.formatBN = formatBN;
function truncateBalance(balance) {
let formattedBalance = parseFloat(balance ? balance.substring(0, 6) : '0');
if (formattedBalance === 0) {
formattedBalance = 0;
}
return formattedBalance;
}
exports.truncateBalance = truncateBalance;
function formatFixedLength(amount, maxLength) {
if (!/^[-+]?\d*\.?\d*$/.test(amount))
return 'Invalid number';
const isNegative = amount.startsWith('-');
let result = amount.replace(/^-/, '');
if (result.includes('.')) {
const parts = result.split('.');
const integerPart = parts[0];
const decimalPart = parts[1] || '';
const availableSpace = maxLength - integerPart.length;
if (integerPart.length >= maxLength) {
result = integerPart;
}
else {
result = integerPart + '.' + decimalPart.substring(0, availableSpace);
}
}
result = result
.replace(/\.0+$/, '')
.replace(/\.(\d*[^0])0+$/, '.$1')
.replace(/\.$/, '');
if (isNegative) {
result = '-' + result;
}
return result;
}
exports.formatFixedLength = formatFixedLength;
//# sourceMappingURL=numbers.js.map