Skip to content

Instantly share code, notes, and snippets.

@T99
Last active February 2, 2023 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T99/cc73b268c7ed7e4f9ee451b835b85cee to your computer and use it in GitHub Desktop.
Save T99/cc73b268c7ed7e4f9ee451b835b85cee to your computer and use it in GitHub Desktop.
A TypeScript function that returns a modified version of the input string, having collapsed all horizontal whitespace down to single space characters.
/**
* Returns a modified version of the input string, having collapsed all
* horizontal whitespace down to single space characters.
*
* @param input {string} The string that should be modified to have it's
* whitespaces collapsed.
* @returns {string} A modified version of the input string, having collapsed
* all horizontal whitespace down to single space characters.
*/
export function collapseWhitespace(input: string): string {
return input.replace(/[^\S\r\n]+/gui, " ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment