stringUtils.ts 796 B

12345678910111213141516171819202122232425
  1. /**
  2. * String utility functions
  3. */
  4. /**
  5. * Convert a string to a slug format
  6. * Removes special characters, replaces spaces with hyphens, and converts to lowercase
  7. *
  8. * @param text The text to convert to slug format
  9. * @returns The slugified text
  10. */
  11. export function slugify(text: string): string {
  12. return text
  13. .toString()
  14. .normalize('NFD') // Split accented characters
  15. .replace(/[\u0300-\u036F]/g, '') // Remove diacritics
  16. .toLowerCase()
  17. .trim()
  18. .replace(/\s+/g, '-') // Replace spaces with -
  19. .replace(/[^\w-]+/g, '') // Remove all non-word chars
  20. .replace(/--+/g, '-') // Replace multiple - with single -
  21. .replace(/^-+/, '') // Trim - from start of text
  22. .replace(/-+$/, '') // Trim - from end of text
  23. .substring(0, 30) // Limit to 30 characters
  24. }