(text: string)
| 4 | */ |
| 5 | |
| 6 | export function normalizeWhitespace(text: string): string { |
| 7 | if (!text || typeof text !== 'string') { |
| 8 | return ''; |
| 9 | } |
| 10 | |
| 11 | text = text.replace(/\n{3,}/g, '\n\n'); |
| 12 | |
| 13 | text = text.replace(/[ \t]+$/gm, ''); |
| 14 | |
| 15 | text = text.replace(/[ \t]{3,}/g, ' '); |
| 16 | |
| 17 | text = text.replace(/[ \t]*\n[ \t]*\n[ \t]*\n/g, '\n\n'); |
| 18 | |
| 19 | text = text.replace(/[ \t]+\n/g, '\n'); |
| 20 | |
| 21 | text = text.trim(); |
| 22 | |
| 23 | return text; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Specialized normalization for streaming tokens to prevent accumulation |
no outgoing calls
no test coverage detected