Skip to content

Commit

Permalink
Slugify selector: Remove leading & trailing dash (#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored Jan 20, 2025
1 parent 6326203 commit 04c1528
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/test/utils/slugify.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { expect } = require('chai');
const { slugify } = require('../../utils/slugify');

describe('slugify', () => {
it('should return new slug', () => {
const slug = slugify('This is a test slug, and this is fine');
expect(slug).to.equal('this-is-a-test-slug-and-this-is-fine');
});
it('should return new slug without trailing dash', () => {
const slug = slugify('This is a test slug, and this is fine |');
expect(slug).to.equal('this-is-a-test-slug-and-this-is-fine');
});
it('should return new slug without leading dash', () => {
const slug = slugify('| This is a test slug, and this is fine |');
expect(slug).to.equal('this-is-a-test-slug-and-this-is-fine');
});
it('should return new slug without emoji', () => {
const slug = slugify('😀 Test emoji test 😀');
expect(slug).to.equal('test-emoji-test');
});
});
8 changes: 8 additions & 0 deletions server/utils/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ function slugify(str) {
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes

// Remove trailing and leading dashes if present
if (newString.startsWith('-')) {
newString = newString.slice(1);
}
if (newString.endsWith('-')) {
newString = newString.slice(0, -1);
}

return newString;
}

Expand Down

0 comments on commit 04c1528

Please sign in to comment.