All files / src/utils format.ts

60% Statements 9/15
41.66% Branches 5/12
100% Functions 2/2
60% Lines 9/15

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    11x     11x 11x 11x     11x             11x     11x 11x       11x    
/** Format a token count in compact notation (e.g. 1952 → "2.0k", 1234567 → "1.2M"). */
export function formatTokens(n: number): string {
  Iif (n >= 1_000_000) {
    return `${(n / 1_000_000).toFixed(1)}M`;
  }
  Eif (n >= 1_000) {
    const kValue = Number((n / 1_000).toFixed(1));
    Iif (kValue >= 1_000) {
      return `${(n / 1_000_000).toFixed(1)}M`;
    }
    return `${kValue.toFixed(1)}k`;
  }
  return String(n);
}
 
/** Format an integer millicent cost for display (e.g. 500 → "$0.0050", 123000 → "$1.23"). */
export function formatCost(millicents: number): string {
  Iif (millicents === 0) {
    return "-";
  }
  const usd = millicents / 100_000;
  Iif (usd < 0.01) {
    const decimals = millicents < 5 ? 5 : 4;
    return `$${usd.toFixed(decimals)}`;
  }
  return `$${usd.toFixed(2)}`;
}