-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(text/unstable): add slugify()
function
#5646
Merged
+90
−0
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5a28482
initial commit
timreichen bba6824
Merge branch 'main' into add-slugify
timreichen efe1ff9
add slugify export
timreichen 6bcd670
add return type
timreichen b3f0390
add jsdocs
timreichen a87367f
add export
timreichen 2542bcf
Merge branch 'main' into add-slugify
timreichen dd646d2
Merge branch 'main' into add-slugify
timreichen b76d014
Merge branch 'main' into add-slugify
timreichen b4f51e8
Merge branch 'main' into add-slugify
timreichen a6ae83d
fix
iuioiua bebca89
Merge branch 'main' into add-slugify
timreichen d77556b
Merge branch 'main' into add-slugify
timreichen a48a313
remove char map replacement
timreichen edc07bb
mark API unstable
kt3k e841146
improve docs
kt3k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", | ||
); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reference, this being the first paragraph means it's used as the summary for this symbol. Please ensure the description is always first for symbols.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as what CLI does. https://docs.deno.com/api/deno/all_symbols
If you suggest something different, I think you should also suggest the same to CLI docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
denoland/deno#25103