std/text/slugify.ts
Asher Gomez dea7d7701a
docs(archive,assert,cache,cli,encoding,html,http,net,streams,text): remove unstable Markdown alert (#5672)
* docs(archive,cli,html,http,net,streams,text): remove unstable Markdown alert

* update

* fix

* update

* fmt

* fix
2024-08-22 02:55:17 -04:00

29 lines
804 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
* Converts a string into a {@link https://en.wikipedia.org/wiki/Clean_URL#Slug | slug}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { slugify } from "@std/text/slugify";
* import { assertEquals } from "@std/assert";
*
* assertEquals(slugify("hello world"), "hello-world");
* assertEquals(slugify("déjà vu"), "deja-vu");
* ```
*
* @param input The string that is going to be converted into a slug
* @returns The string as a slug
*/
export function slugify(input: string): string {
return input
.trim()
.normalize("NFD")
.replaceAll(/[^a-zA-Z0-9\s-]/g, "")
.replaceAll(/\s+|-+/g, "-")
.replaceAll(/^-+|-+$/g, "")
.toLowerCase();
}