format.tsx
1 export function wrapText(text: string, width: number): string[] { 2 const lines: string[] = [] 3 let currentLine = '' 4 5 for (const char of text) { 6 // Important: we need the spread to properly count multi-plane UTF-8 characters (eg. 𑚖) 7 if ([...currentLine].length < width) { 8 currentLine += char 9 } else { 10 lines.push(currentLine) 11 currentLine = char 12 } 13 } 14 15 if (currentLine) lines.push(currentLine) 16 return lines 17 } 18 19 export function formatDuration(ms: number): string { 20 if (ms < 60000) { 21 return `${(ms / 1000).toFixed(1)}s` 22 } 23 24 const hours = Math.floor(ms / 3600000) 25 const minutes = Math.floor((ms % 3600000) / 60000) 26 const seconds = ((ms % 60000) / 1000).toFixed(1) 27 28 if (hours > 0) { 29 return `${hours}h ${minutes}m ${seconds}s` 30 } 31 if (minutes > 0) { 32 return `${minutes}m ${seconds}s` 33 } 34 return `${seconds}s` 35 } 36 37 export function formatNumber(number: number): string { 38 return new Intl.NumberFormat('en', { 39 notation: 'compact', 40 maximumFractionDigits: 1, 41 }) 42 .format(number) // eg. "1321" => "1.3K" 43 .toLowerCase() // eg. "1.3K" => "1.3k" 44 }