2022-01-10 22:47:02 +00:00
|
|
|
export const slugify = (
|
|
|
|
text: string,
|
2022-01-10 23:52:03 +00:00
|
|
|
separator = '-',
|
2022-01-10 22:47:02 +00:00
|
|
|
maxLength = 35
|
|
|
|
): string => {
|
2022-01-10 21:07:57 +00:00
|
|
|
return text
|
|
|
|
.toString()
|
2022-01-10 23:52:03 +00:00
|
|
|
.normalize('NFD') // split an accented letter in the base letter and the acent
|
|
|
|
.replace(/[\u0300-\u036f]/g, '') // remove all previously split accents
|
2022-01-10 21:07:57 +00:00
|
|
|
.toLowerCase()
|
|
|
|
.trim()
|
2022-01-10 23:52:03 +00:00
|
|
|
.replace(/[^a-z0-9 ]/g, '') // remove all chars not letters, numbers and spaces (to be replaced)
|
2022-01-10 22:47:02 +00:00
|
|
|
.replace(/\s+/g, separator)
|
|
|
|
.substring(0, maxLength)
|
2022-01-10 23:52:03 +00:00
|
|
|
.replace(new RegExp(separator + '+$', 'g'), '') // remove terminal separators
|
|
|
|
}
|