Skip to content

normalizeWhitespace()

normalizeWhitespace(input, options?): string

Defined in: utils/string/normalizeWhitespace.ts:58

Normalize whitespace in a string.

Can be used to:

  • collapse repeated spaces / tabs / newlines,
  • trim leading and trailing whitespace,
  • optionally preserve line breaks while normalizing spaces on each line.

When NormalizeWhitespaceOptions.preserveLineBreaks is true, Windows-style \r\n sequences are normalized to \n.

Parameters

input

string

The string to normalize.

options?

NormalizeWhitespaceOptions = {}

Optional configuration.

Returns

string

A whitespace-normalized string.

Example

ts
normalizeWhitespace('  hello   world  ');
// 'hello world'

normalizeWhitespace('  a\n   b  ', { preserveLineBreaks: true });
// 'a\nb'

Released under the MIT License.