(str: string, options: NormalizeOptions = DEFAULT_OPTIONS)
| 46 | * @returns The normalized string |
| 47 | */ |
| 48 | export function normalizeString(str: string, options: NormalizeOptions = DEFAULT_OPTIONS): string { |
| 49 | const opts = { ...DEFAULT_OPTIONS, ...options } |
| 50 | let normalized = str |
| 51 | |
| 52 | // Replace smart quotes |
| 53 | if (opts.smartQuotes) { |
| 54 | for (const [smart, regular] of Object.entries(NORMALIZATION_MAPS.SMART_QUOTES)) { |
| 55 | normalized = normalized.replace(new RegExp(smart, "g"), regular) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Replace typographic characters |
| 60 | if (opts.typographicChars) { |
| 61 | for (const [typographic, regular] of Object.entries(NORMALIZATION_MAPS.TYPOGRAPHIC)) { |
| 62 | normalized = normalized.replace(new RegExp(typographic, "g"), regular) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Normalize whitespace |
| 67 | if (opts.extraWhitespace) { |
| 68 | normalized = normalized.replace(/\s+/g, " ") |
| 69 | } |
| 70 | |
| 71 | // Trim whitespace |
| 72 | if (opts.trim) { |
| 73 | normalized = normalized.trim() |
| 74 | } |
| 75 | |
| 76 | return normalized |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Unescapes common HTML entities in a string |
no test coverage detected