Skip to content

[request] slugify() util #52

Description

@NamesMT

Clear and concise description of the problem

Would be amazing if the package includes a slugify util to convert a string to an URL-slug, it's pretty commonly used when building ECOM / content-centric web applications.

Suggested solution

Probably something like:

export function slugify(text: string): string {
  return text
    .toLowerCase()
    .normalize('NFD') // split accented chars
    .replace(/[\u0300-\u036F]/g, '') // remove accents
    .replace(/[^a-z0-9]+/g, '-') // replace non-alphanumerics with -
    .replace(/^-+|-+$/g, '') // trim leading/trailing -
}

I've been using the above snippet for a few projects, haven't hit any weird cases yet.

Alternative

No response

Additional context

No response

Validations

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions