Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slugify selector: Remove leading & trailing dash #2202

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading