All files format.ts

100% Statements 18/18
100% Branches 12/12
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x     1x 17x 17x 17x     1x 7x 7x 7x     1x 5x 5x 5x 5x 5x     1x 5x 5x  
import { formatUnits } from 'viem';
 
/** Convert wei (bigint or string) to a JS number. Core primitive. */
export function weiToNumber(value: bigint | string, decimals = 18): number {
  const bi = typeof value === 'string' ? BigInt(value || '0') : (value ?? 0n);
  return Number(formatUnits(bi, decimals));
}
 
/** Format wei to fixed decimal string (e.g. "0.00123") */
export function formatWei(value: bigint | string, decimals = 18, digits = 5): string {
  const num = weiToNumber(value, decimals);
  return num === 0 ? '0' : num.toFixed(digits);
}
 
/** Format number to compact display (e.g. "1.23K", "4.56M") */
export function compactNumber(value: number): string {
  return Intl.NumberFormat('en-US', {
    notation: 'compact',
    maximumFractionDigits: 2,
  }).format(value);
}
 
/** Format number with commas (e.g. "1,234,567") */
export function commaNumber(value: number): string {
  return value ? value.toLocaleString('en-US') : '0';
}