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