All files / src/utils format.ts

42.14% Statements 51/121
64.7% Branches 11/17
58.33% Functions 7/12
42.14% Lines 51/121

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                    1x 40x     40x 1x 1x 40x 4x 4x 35x 35x             1x 1x   1x 1x 1x 1x             1x                         1x 1x     1x 1x             1x 1x 1x             1x                           1x 2x       2x   2x   2x       2x 2x 2x 2x 2x 2x     2x             1x 23x 23x 23x 23x 23x 23x 23x             1x 8x 8x             1x                                                                   1x                                     1x                          
/**
 * Formatting utilities for odds, spreads, totals, dates, and times
 */
 
/**
 * Format American odds with + or - prefix
 * @param american - American odds number (can be null/undefined)
 * @param asDecimal - If true, convert to decimal odds format
 * @returns Formatted string like "+150" or "-110" (or "2.50" for decimal), or "N/A" if null
 */
export function formatOdds(american: number | null | undefined, asDecimal: boolean = false): string {
  if (american === null || american === undefined) {
    return 'N/A';
  }
  if (asDecimal) {
    return americanToDecimal(american).toFixed(2);
  }
  if (american > 0) {
    return `+${american}`;
  }
  return american.toString();
}
 
/**
 * Convert American odds to decimal odds
 * @param american - American odds number
 * @returns Decimal odds
 */
export function americanToDecimal(american: number): number {
  if (american > 0) {
    return (american / 100) + 1;
  } else {
    return (100 / Math.abs(american)) + 1;
  }
}
 
/**
 * Convert decimal odds to American odds
 * @param decimal - Decimal odds number
 * @returns American odds
 */
export function decimalToAmerican(decimal: number): number {
  if (decimal >= 2.0) {
    return Math.round((decimal - 1) * 100);
  } else {
    return Math.round(-100 / (decimal - 1));
  }
}
 
/**
 * Format spread with + or - prefix
 * @param spread - Spread number (positive means underdog, negative means favorite)
 * @returns Formatted string like "+3.5" or "-3.5"
 */
export function formatSpread(spread: number): string {
  if (spread > 0) {
    return `+${spread}`;
  }
  return spread.toString();
}
 
/**
 * Format total as plain number
 * @param total - Total number
 * @returns Formatted string like "220.5"
 */
export function formatTotal(total: number): string {
  return total.toString();
}
 
/**
 * Format time as local time string
 * @param date - ISO date string
 * @returns Formatted time like "7:30 PM"
 */
export function formatTime(date: string): string {
  const d = new Date(date);
  return d.toLocaleTimeString('en-US', {
    hour: 'numeric',
    minute: '2-digit',
    hour12: true
  });
}
 
/**
 * Format date as short date string
 * @param date - ISO date string (YYYY-MM-DD format)
 * @returns Formatted date like "Mon, Jan 8"
 */
export function formatDate(date: string | null | undefined): string {
  if (!date) {
    return 'Unknown Date';
  }
  
  try {
    // Handle both ISO datetime strings and simple date strings
    const d = new Date(date);
    
    if (isNaN(d.getTime())) {
      return 'Invalid Date';
    }
    
    return d.toLocaleDateString('en-US', {
      weekday: 'short',
      month: 'short',
      day: 'numeric'
    });
  } catch (error) {
    return 'Invalid Date';
  }
}
 
/**
 * Format currency as USD
 * @param amount - Dollar amount
 * @returns Formatted string like "$150.00"
 */
export function formatCurrency(amount: number): string {
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  }).format(amount);
}
 
/**
 * Format percentage
 * @param value - Percentage value (0-100)
 * @returns Formatted string like "54.2%"
 */
export function formatPercentage(value: number): string {
  return `${value.toFixed(1)}%`;
}
 
/**
 * Format relative time (e.g., "2 hours ago")
 * @param date - ISO date string
 * @returns Formatted relative time string
 */
export function formatRelativeTime(date: string | null | undefined): string {
  if (!date) {
    return 'Unknown';
  }
  
  const d = new Date(date);
  
  // Check if date is invalid
  if (isNaN(d.getTime())) {
    return 'Invalid Date';
  }
  
  const now = new Date();
  const diffMs = now.getTime() - d.getTime();
  const diffMins = Math.floor(diffMs / 60000);
  const diffHours = Math.floor(diffMins / 60);
  const diffDays = Math.floor(diffHours / 24);
 
  if (diffMins < 1) {
    return 'just now';
  } else if (diffMins < 60) {
    return `${diffMins} min${diffMins === 1 ? '' : 's'} ago`;
  } else if (diffHours < 24) {
    return `${diffHours} hour${diffHours === 1 ? '' : 's'} ago`;
  } else {
    return `${diffDays} day${diffDays === 1 ? '' : 's'} ago`;
  }
}
 
/**
 * Get sport display name from key
 * @param sportKey - Sport key like "basketball_nba"
 * @returns Display name like "NBA"
 */
export function getSportDisplayName(sportKey: string): string {
  const sportMap: Record<string, string> = {
    'basketball_nba': 'NBA',
    'americanfootball_nfl': 'NFL',
    'icehockey_nhl': 'NHL',
    'baseball_mlb': 'MLB',
    'americanfootball_ncaaf': 'NCAAF',
    'basketball_ncaab': 'NCAAB',
    'soccer_epl': 'EPL',
    'soccer_uefa_champs_league': 'UEFA CL'
  };
  return sportMap[sportKey] || sportKey.toUpperCase();
}
 
/**
 * Get sport color class for Tailwind
 * @param sportKey - Sport key like "basketball_nba"
 * @returns Tailwind color class
 */
export function getSportColorClass(sportKey: string): string {
  const colorMap: Record<string, string> = {
    'basketball_nba': 'bg-orange-500',
    'americanfootball_nfl': 'bg-blue-600',
    'icehockey_nhl': 'bg-cyan-500',
    'baseball_mlb': 'bg-red-500',
    'americanfootball_ncaaf': 'bg-purple-600',
    'basketball_ncaab': 'bg-orange-600',
    'soccer_epl': 'bg-green-600',
    'soccer_uefa_champs_league': 'bg-indigo-600'
  };
  return colorMap[sportKey] || 'bg-gray-500';
}