mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
29 lines
813 B
TypeScript
29 lines
813 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/unstable-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();
|
|
}
|