blob: 3d9ecec649a8288082203dad9264dd9660f144c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
|
/** Turn a string into a slug by lowercasing and replacing whitespace. */
export const classify = (text: string, fallback?: string): string => {
const output = String(text)
.toLowerCase()
// .normalize('NFD') // split an accented letter in the base letter and the acent
// .replace(/[\u0300-\u036f]/g, '') // remove all previously split accents
.replace(/[\s/_.]+/g, '-') // replace spaces and _./ with '-'
.replace(/[^\w-]+/g, '') // remove all non-word chars
.replace(/--+/g, '-') // replace repeating '-' with single '-'
.replace(/^-+|-+$/g, ''); // trim '-' from edges
return output || fallback || '';
};
|