-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(text/unstable): add
slugify()
function (#5646)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
- Loading branch information
1 parent
88b35ec
commit 6677134
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* Converts a string into {@link https://en.wikipedia.org/wiki/Clean_URL#Slug a slug}. | ||
* | ||
* @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 | ||
* | ||
* @experimental | ||
*/ | ||
export function slugify(input: string): string { | ||
return input | ||
.trim() | ||
.normalize("NFD") | ||
.replaceAll(/[^a-zA-Z0-9\s-]/g, "") | ||
.replaceAll(/\s+|-+/g, "-") | ||
.replaceAll(/^-+|-+$/g, "") | ||
.toLowerCase(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
import { assertEquals } from "@std/assert/equals"; | ||
import { slugify } from "./slugify.ts"; | ||
|
||
Deno.test("slugify() returns kebabcase", () => { | ||
assertEquals(slugify("hello world"), "hello-world"); | ||
}); | ||
Deno.test("slugify() returns lowercase", () => { | ||
assertEquals(slugify("Hello World"), "hello-world"); | ||
}); | ||
|
||
Deno.test("slugify() handles whitespaces", () => { | ||
assertEquals(slugify(" Hello World "), "hello-world"); | ||
assertEquals(slugify("Hello\tWorld"), "hello-world"); | ||
assertEquals(slugify("Hello\nWorld"), "hello-world"); | ||
assertEquals(slugify("Hello\r\nWorld"), "hello-world"); | ||
}); | ||
|
||
Deno.test("slugify() replaces diacritic characters", () => { | ||
assertEquals(slugify("déjà vu"), "deja-vu"); | ||
assertEquals(slugify("Cliché"), "cliche"); | ||
assertEquals(slugify("façade"), "facade"); | ||
assertEquals(slugify("résumé"), "resume"); | ||
}); | ||
|
||
Deno.test("slugify() handles dashes", () => { | ||
assertEquals(slugify("-Hello-World-"), "hello-world"); | ||
assertEquals(slugify("--Hello--World--"), "hello-world"); | ||
}); | ||
|
||
Deno.test("slugify() handles empty String", () => { | ||
assertEquals(slugify(""), ""); | ||
}); | ||
|
||
Deno.test("slugify() removes unknown special characters", () => { | ||
assertEquals(slugify("hello ~ world"), "hello-world"); | ||
|
||
assertEquals( | ||
slugify("Elon Musk considers move to Mars"), | ||
"elon-musk-considers-move-to-mars", | ||
); | ||
assertEquals( | ||
slugify("Fintech startups raised $34B in 2019"), | ||
"fintech-startups-raised-34b-in-2019", | ||
); | ||
assertEquals( | ||
slugify("Shopify joins Facebook’s cryptocurrency Libra Association"), | ||
"shopify-joins-facebooks-cryptocurrency-libra-association", | ||
); | ||
assertEquals( | ||
slugify("What is a slug and how to optimize it?"), | ||
"what-is-a-slug-and-how-to-optimize-it", | ||
); | ||
assertEquals( | ||
slugify("Bitcoin soars past $33,000, its highest ever"), | ||
"bitcoin-soars-past-33000-its-highest-ever", | ||
); | ||
}); |