normalizeWhitespace method

String normalizeWhitespace(
  1. String text
)

Implementation

String normalizeWhitespace(String text) {
  // Collapse spaces/tabs within each line
  String replaceSpaces(String lines) {
    return lines.replaceAll(_whitespaceRegex, ' ');
  }

  // Preserve newlines but collapse multiple spaces between words
  return text
      .split('\n') // Split on newlines to preserve them
      .map(replaceSpaces)
      .join('\n'); // Rejoin with newlines
}