mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
dea7d7701a
* docs(archive,cli,html,http,net,streams,text): remove unstable Markdown alert * update * fix * update * fmt * fix
29 lines
804 B
TypeScript
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();
|
|
}
|