sanitizeMultilineText()function

sanitizeMultilineText(str: string): string
ParamType
strstring
The string to sanitize. required
Return
string
The sanitized multi-line string.

Sanitize multiple lines of text.

  • Used when you're sanitising a multi-line input, e.g. a description for something.
  • Remove all control characters except \n newline.
  • Normalise weird characters like paragraph separator, line separator, \t tab, \r carriage return.
  • Normalise runs of whitespace to one space,
  • Normalise indentation to tabs (four or more spaces are a tab, three or fewer spaces are removed).
  • Allow spaces at the start of each line (for indentation) but trim the end of each line.
  • Trim excess newlines at the start and end of the string and runs of more than two newlines in a row.

Examples

sanitizeMultilineText("\x00Line one\n\n\n\nLine two   ") // "Line one\n\nLine two"