normalize-string.ts 583 B

12345678910111213141516
  1. /**
  2. * Normalizes a string to replace non-alphanumeric and diacritical marks with
  3. * plain equivalents.
  4. * Based on https://stackoverflow.com/a/37511463/772859
  5. */
  6. export function normalizeString(input: string, spaceReplacer = ' '): string {
  7. return (input || '')
  8. .normalize('NFD')
  9. .replace(/[\u00df]/g, 'ss')
  10. .replace(/[\u1e9e]/g, 'SS')
  11. .replace(/[\u0308]/g, 'e')
  12. .replace(/[\u0300-\u036f]/g, '')
  13. .toLowerCase()
  14. .replace(/[!"£$%^&*()+[\]{};:@#~?\\/,|><`¬'=‘’©®™]/g, '')
  15. .replace(/\s+/g, spaceReplacer);
  16. }