slugify.tsx 583 B

1234567891011121314
  1. /**
  2. * Transforms the given string to a slugified version. (e.g. "My Project" => "my-project")
  3. *
  4. * Allows only lowercase alphanumeric values, hyphens, and underscores (should match backend validation rules).
  5. * Normalizes special characters to a-z where applicable (accents, ligatures, etc).
  6. * Converts spaces to hyphens.
  7. */
  8. export default function slugify(str: string): string {
  9. return str
  10. .normalize('NFKD') // Converts accents/ligatures/etc to latin alphabet
  11. .toLowerCase()
  12. .replace(' ', '-')
  13. .replace(/[^a-z0-9-_]/g, ''); // Remove all invalid characters
  14. }