Skip to content

[request] slugify() util #52

@NamesMT

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

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