Skip to content

Commit

Permalink
feat: Slugify more characters TDE-1044 (#875)
Browse files Browse the repository at this point in the history
#### Motivation

Accept more common punctuation in place names.

#### Modification

- Replace apostrophes, slashes, and commas with hyphen.
- Replace ampersands with “and”.
- Collapse multiple hyphens.

#### Checklist

- [x] Tests updated
- [ ] Docs updated (N/A)
- [x] Issue linked in Title
  • Loading branch information
l0b0 authored Feb 19, 2024
1 parent 6cafae3 commit 66020a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/utils/__test__/slugify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ describe('slugify', () => {
it('should replace spaces with hyphens', () => {
assert.equal(slugify('Upper North Island'), 'upper-north-island');
});
it('should remove apostrophes', () => {
assert.equal(slugify("Hawke's Bay"), 'hawkes-bay');
});
it('should replace slashes with hyphens', () => {
assert.equal(slugify('Tikitapu/Blue Lake'), 'tikitapu-blue-lake');
});
it('should replace commas with hyphens', () => {
assert.equal(slugify('Omere, Janus or Toby Rock'), 'omere-janus-or-toby-rock');
});
it('should replace ampersands with "and"', () => {
assert.equal(slugify('Gore A&P Showgrounds'), 'gore-a-and-p-showgrounds');
});
it('should collapse multiple hyphens', () => {
assert.equal(slugify("Butlers 'V' Hut"), 'butlers-v-hut');
});
it('should remove diacritics', () => {
['á', 'Á', 'ä', 'Ä', 'ā', 'Ā'].forEach((value) => {
assert.equal(slugify(value), 'a');
Expand Down Expand Up @@ -45,8 +60,8 @@ describe('slugify', () => {
},
{
name: 'Error',
message: 'Unhandled characters: "\\n", "/", ";", "\\", "—", "“", "”"',
cause: { characters: ['\n', '/', ';', '\\', '—', '“', '”'] },
message: 'Unhandled characters: "\\n", ";", "\\", "—", "“", "”"',
cause: { characters: ['\n', ';', '\\', '—', '“', '”'] },
},
);
});
Expand Down
9 changes: 8 additions & 1 deletion src/utils/slugify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
* @returns String slug. See src/utils/__test__/slugify.test.ts for examples.
*/
export function slugify(input: string): string {
const result = removeDiacritics(input).replaceAll('ø', 'o').replaceAll('Ø', 'O').replaceAll(' ', '-').toLowerCase();
const result = removeDiacritics(input)
.replaceAll("'", '')
.replaceAll('ø', 'o')
.replaceAll('Ø', 'O')
.replaceAll(/[ ,/]/g, '-')
.replaceAll('&', '-and-')
.replaceAll(/--+/g, '-')
.toLowerCase();

const unhandledCharacters = result.match(/[^abcdefghijklmnopqrstuvwxyz0123456789_.-]/g);
if (unhandledCharacters) {
Expand Down

0 comments on commit 66020a4

Please sign in to comment.