/ src / lib / utils.ts
utils.ts
  1  // Format large numbers with commas
  2  export function formatNumber(num: number): string {
  3    return new Intl.NumberFormat('en-US').format(num);
  4  }
  5  
  6  // Format large numbers with abbreviations (K, M, B)
  7  export function formatCompact(num: number): string {
  8    if (num >= 1e9) return (num / 1e9).toFixed(1) + 'B';
  9    if (num >= 1e6) return (num / 1e6).toFixed(1) + 'M';
 10    if (num >= 1e3) return (num / 1e3).toFixed(1) + 'K';
 11    return num.toString();
 12  }
 13  
 14  // Truncate hash for display
 15  export function truncateHash(hash: string, startChars: number = 8, endChars: number = 6): string {
 16    if (hash.length <= startChars + endChars) return hash;
 17    return `${hash.slice(0, startChars)}...${hash.slice(-endChars)}`;
 18  }
 19  
 20  // Format timestamp to relative time
 21  export function formatRelativeTime(timestamp: number): string {
 22    const now = Date.now();
 23    const diff = now - timestamp;
 24  
 25    const seconds = Math.floor(diff / 1000);
 26    if (seconds < 60) return `${seconds} sec${seconds !== 1 ? 's' : ''} ago`;
 27  
 28    const minutes = Math.floor(seconds / 60);
 29    if (minutes < 60) return `${minutes} min${minutes !== 1 ? 's' : ''} ago`;
 30  
 31    const hours = Math.floor(minutes / 60);
 32    if (hours < 24) return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
 33  
 34    const days = Math.floor(hours / 24);
 35    return `${days} day${days !== 1 ? 's' : ''} ago`;
 36  }
 37  
 38  // Format timestamp to date string
 39  export function formatDate(timestamp: number): string {
 40    return new Date(timestamp).toLocaleString('en-US', {
 41      year: 'numeric',
 42      month: 'short',
 43      day: 'numeric',
 44      hour: '2-digit',
 45      minute: '2-digit',
 46      second: '2-digit',
 47    });
 48  }
 49  
 50  // Format AX amount
 51  export function formatAX(value: string | number): string {
 52    const num = typeof value === 'string' ? parseFloat(value) : value;
 53    if (isNaN(num)) return '0 AX';
 54    return `${formatNumber(parseFloat(num.toFixed(4)))} AX`;
 55  }
 56  
 57  // Format gas
 58  export function formatGas(gas: number): string {
 59    return formatNumber(gas);
 60  }
 61  
 62  // Format percentage
 63  export function formatPercent(value: number, decimals: number = 2): string {
 64    const sign = value >= 0 ? '+' : '';
 65    return `${sign}${value.toFixed(decimals)}%`;
 66  }
 67  
 68  // Format USD
 69  export function formatUSD(value: number): string {
 70    return new Intl.NumberFormat('en-US', {
 71      style: 'currency',
 72      currency: 'USD',
 73      minimumFractionDigits: 2,
 74      maximumFractionDigits: 2,
 75    }).format(value);
 76  }
 77  
 78  // Detect search type
 79  export type SearchType = 'block' | 'transaction' | 'address' | 'unknown';
 80  
 81  export function detectSearchType(query: string): SearchType {
 82    const trimmed = query.trim();
 83  
 84    // Block height (numeric)
 85    if (/^\d+$/.test(trimmed)) {
 86      return 'block';
 87    }
 88  
 89    // Transaction hash (0x + 64 hex chars)
 90    if (/^0x[a-fA-F0-9]{64}$/.test(trimmed)) {
 91      return 'transaction';
 92    }
 93  
 94    // Address (0x + 40 hex chars)
 95    if (/^0x[a-fA-F0-9]{40}$/.test(trimmed)) {
 96      return 'address';
 97    }
 98  
 99    return 'unknown';
100  }