17 lines
592 B
TypeScript
17 lines
592 B
TypeScript
export function slugify(value: string): string {
|
|
const ascii = value
|
|
.normalize("NFD")
|
|
.replace(/[\u0300-\u036f]/g, "")
|
|
.replace(/[\u2013\u2014]/g, "-")
|
|
const sanitized = ascii.replace(/[^a-zA-Z0-9\s-]/g, "")
|
|
const hyphenized = sanitized.replace(/[_\s]+/g, "-").replace(/-+/g, "-")
|
|
return hyphenized.replace(/^-+|-+$/g, "").toLowerCase()
|
|
}
|
|
|
|
export function normalizeSlug(input?: string | null): string | undefined {
|
|
if (!input) return undefined
|
|
const trimmed = input.trim()
|
|
if (!trimmed) return undefined
|
|
const slug = slugify(trimmed)
|
|
return slug || undefined
|
|
}
|