All files / src/utils format.ts

54.16% Statements 13/24
28.57% Branches 2/7
100% Functions 2/2
54.16% Lines 13/24

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  2x 11x     11x 11x 11x     11x 11x         2x 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 {
  if (n >= 1_000_000) {
    return `${(n / 1_000_000).toFixed(1)}M`;
  }
  if (n >= 1_000) {
    const kValue = Number((n / 1_000).toFixed(1));
    if (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 {
  if (millicents === 0) {
    return "-";
  }
  const usd = millicents / 100_000;
  if (usd < 0.01) {
    const decimals = millicents < 5 ? 5 : 4;
    return `$${usd.toFixed(decimals)}`;
  }
  return `$${usd.toFixed(2)}`;
}