All files / dashboard/src/utils format.ts

100% Statements 31/31
100% Branches 30/30
100% Functions 5/5
100% Lines 22/22

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  19x 17x 16x 13x 5x 5x 5x   8x 8x 8x 5x 5x       15x 11x 8x       12x 9x 6x 3x       3x                   3x 3x    
export function formatDuration(seconds: number | undefined | null): string {
  if (seconds === null || seconds === undefined) return '—';
  if (seconds === 0) return '0s';
  if (seconds < 60) return `${seconds}s`;
  if (seconds < 3600) {
    const m = Math.floor(seconds / 60);
    const s = seconds % 60;
    return s > 0 ? `${m}m ${s}s` : `${m}m`;
  }
  const h = Math.floor(seconds / 3600);
  const remainder = seconds % 3600;
  if (remainder === 0) return `${h}h`;
  const m = Math.floor(remainder / 60);
  return `${h}h ${m}m`;
}
 
export function formatCost(cost: number | undefined | null): string {
  if (cost === null || cost === undefined || cost === 0 || Number.isNaN(cost)) return '$0.00';
  if (cost < 0.01) return `$${cost.toFixed(4)}`;
  return `$${cost.toFixed(2)}`;
}
 
export function formatTokens(tokens: number | undefined | null): string {
  if (!tokens) return '0';
  if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`;
  if (tokens >= 1_000) return `${(tokens / 1_000).toFixed(1)}K`;
  return tokens.toString();
}
 
export function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleDateString('en-US', {
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    timeZone: 'UTC',
  });
}
 
export function formatDay(dateStr: string): string {
  const d = dateStr.includes('T') ? new Date(dateStr) : new Date(dateStr + 'T12:00:00');
  return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC' });
}