feat: add slugify utility
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
export function slugify(text: string): string {
|
||||||
|
return text
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[^a-z0-9\s-]/g, '')
|
||||||
|
.replace(/[\s]+/g, '-')
|
||||||
|
.replace(/-+/g, '-')
|
||||||
|
.replace(/^-|-$/g, '');
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { slugify } from '$lib/slugify';
|
||||||
|
|
||||||
|
describe('slugify', () => {
|
||||||
|
it('converts to lowercase with hyphens', () => {
|
||||||
|
expect(slugify('Hello World')).toBe('hello-world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes special characters', () => {
|
||||||
|
expect(slugify("Buddy's Adventure!")).toBe('buddys-adventure');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('collapses multiple hyphens', () => {
|
||||||
|
expect(slugify('foo bar---baz')).toBe('foo-bar-baz');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('trims leading and trailing hyphens', () => {
|
||||||
|
expect(slugify(' hello ')).toBe('hello');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user