formatters.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const formatter = new Intl.NumberFormat("en-US", {
  2. style: "currency",
  3. currency: "USD",
  4. })
  5. export const formatCurrency = (amount: number) => formatter.format(amount)
  6. export const formatDuration = (durationMs: number) => {
  7. const seconds = Math.floor(durationMs / 1000)
  8. const hours = Math.floor(seconds / 3600)
  9. const minutes = Math.floor((seconds % 3600) / 60)
  10. const remainingSeconds = seconds % 60
  11. // Format as H:MM:SS
  12. const mm = minutes.toString().padStart(2, "0")
  13. const ss = remainingSeconds.toString().padStart(2, "0")
  14. return `${hours}:${mm}:${ss}`
  15. }
  16. export const formatTokens = (tokens: number) => {
  17. if (tokens < 1000) {
  18. return tokens.toString()
  19. }
  20. if (tokens < 1000000) {
  21. // No decimal for thousands (e.g., 72k not 72.5k)
  22. const rounded = Math.round(tokens / 1000)
  23. // If rounding crosses the boundary to 1000k, show as 1.0M instead
  24. if (rounded >= 1000) {
  25. return "1.0M"
  26. }
  27. return `${rounded}k`
  28. }
  29. if (tokens < 1000000000) {
  30. // Keep decimal for millions (e.g., 3.2M)
  31. const rounded = Math.round(tokens / 100000) / 10 // Round to 1 decimal
  32. // If rounding crosses the boundary to 1000M, show as 1.0B instead
  33. if (rounded >= 1000) {
  34. return "1.0B"
  35. }
  36. return `${rounded.toFixed(1)}M`
  37. }
  38. return `${(tokens / 1000000000).toFixed(1)}B`
  39. }
  40. export const formatToolUsageSuccessRate = (usage: { attempts: number; failures: number }) =>
  41. usage.attempts === 0 ? "0%" : `${Math.round(((usage.attempts - usage.failures) / usage.attempts) * 100)}%`
  42. export const formatDateTime = (date: Date) => {
  43. return new Intl.DateTimeFormat("en-US", {
  44. month: "short",
  45. day: "numeric",
  46. hour: "numeric",
  47. minute: "2-digit",
  48. hour12: true,
  49. }).format(date)
  50. }