The string to normalize.
Whether to preserve case sensitivity.
The normalized string.
export function normalizeString(text: string, caseSensitive = false): string {
if (!text) return "";
// Replace special characters and normalize spacing
const replaced = text
.replaceAll(/[^\w\s]/gi, " ") // Replace special chars with space
.replaceAll(/\s+/g, " ") // Collapse multiple spaces
.trim();
return caseSensitive ? replaced : replaced.toLowerCase();
}
Cleans and normalizes a string for comparison.