embedchainjs
26 строк · 1.1 Кб
1/**
2* This function takes in a string and performs a series of text cleaning operations.
3* @param {str} text: The text to be cleaned. This is expected to be a string.
4* @returns {str}: The cleaned text after all the cleaning operations have been performed.
5*/
6export function cleanString(text: string): string {7// Replacement of newline characters:8let cleanedText = text.replace(/\n/g, ' ');9
10// Stripping and reducing multiple spaces to single:11cleanedText = cleanedText.trim().replace(/\s+/g, ' ');12
13// Removing backslashes:14cleanedText = cleanedText.replace(/\\/g, '');15
16// Replacing hash characters:17cleanedText = cleanedText.replace(/#/g, ' ');18
19// Eliminating consecutive non-alphanumeric characters:20// This regex identifies consecutive non-alphanumeric characters (i.e., not a word character [a-zA-Z0-9_] and not a whitespace) in the string21// and replaces each group of such characters with a single occurrence of that character.22// For example, "!!! hello !!!" would become "! hello !".23cleanedText = cleanedText.replace(/([^\w\s])\1*/g, '$1');24
25return cleanedText;26}
27