normalize-string.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { describe, expect, it } from 'vitest';
  2. import { normalizeString } from './normalize-string';
  3. describe('normalizeString()', () => {
  4. it('lowercases the string', () => {
  5. expect(normalizeString('FOO')).toBe('foo');
  6. expect(normalizeString('Foo Bar')).toBe('foo bar');
  7. });
  8. it('replaces diacritical marks with plain equivalents', () => {
  9. expect(normalizeString('thé')).toBe('the');
  10. expect(normalizeString('curaçao')).toBe('curacao');
  11. expect(normalizeString('dấu hỏi')).toBe('dau hoi');
  12. expect(normalizeString('el niño')).toBe('el nino');
  13. expect(normalizeString('genkō yōshi')).toBe('genko yoshi');
  14. expect(normalizeString('việt nam')).toBe('viet nam');
  15. });
  16. it('replaces spaces with the spaceReplacer', () => {
  17. expect(normalizeString('old man', '_')).toBe('old_man');
  18. expect(normalizeString('old man', '_')).toBe('old_man');
  19. });
  20. it('strips non-alphanumeric characters', () => {
  21. expect(normalizeString('hi!!!')).toBe('hi');
  22. expect(normalizeString('who? me?')).toBe('who me');
  23. expect(normalizeString('!"£$%^&*()+[]{};:@#~?/,|\\><`¬\'=©®™')).toBe('');
  24. });
  25. it('allows a subset of non-alphanumeric characters to pass through', () => {
  26. expect(normalizeString('-_.')).toBe('-_.');
  27. });
  28. // https://github.com/vendure-ecommerce/vendure/issues/679
  29. it('replaces single quotation marks', () => {
  30. expect(normalizeString('Capture d’écran')).toBe('capture decran');
  31. expect(normalizeString('Capture d‘écran')).toBe('capture decran');
  32. });
  33. it('replaces eszett with double-s digraph', () => {
  34. expect(normalizeString('KONGREẞ im Straßennamen')).toBe('kongress im strassennamen');
  35. });
  36. // works for German language, might not work for e.g. Finnish language
  37. it('replaces combining diaeresis with e', () => {
  38. expect(normalizeString('Ja quäkt Schwyz Pöbel vor Gmünd')).toBe('ja quaekt schwyz poebel vor gmuend');
  39. });
  40. it('contracts multiple sequential replacers to a single replacer', () => {
  41. expect(normalizeString('foo - bar', '-')).toBe('foo-bar');
  42. expect(normalizeString('foo--bar', '-')).toBe('foo-bar');
  43. expect(normalizeString('foo - bar', '_')).toBe('foo_-_bar');
  44. expect(normalizeString('foo _ bar', '_')).toBe('foo_bar');
  45. });
  46. });