Transform # character to - in slug (#10896)

Co-authored-by: Bailey Pumfleet <[email protected]>
This commit is contained in:
Ujwal KumarandBailey Pumfleet authored and GitHub committed 2023-08-24 11:19:10 +01:00
1 parent fde3e8bea9
commit 31d4cb4eb7
2 files changed
+5 -4

No files matched your search

+3 -1
View File
@@ -8,10 +8,12 @@ describe("slugify", () => {
expect(slugify("HELLO")).toEqual("hello");
});
it("should convert spaces, _, and any other special character to -", () => {
it("should convert spaces, _, +, # and any other special character to -", () => {
expect(slugify("hello there")).toEqual("hello-there");
expect(slugify("hello_there")).toEqual("hello-there");
expect(slugify("hello$there")).toEqual("hello-there");
expect(slugify("hello+there")).toEqual("hello-there");
expect(slugify("#hellothere")).toEqual("hellothere");
});
it("should keep numbers as is", () => {
+2 -3
View File
@@ -5,10 +5,9 @@ export const slugify = (str: string) => {
.normalize("NFD") // Normalize to decomposed form for handling accents
.replace(/\p{Diacritic}/gu, "") // Remove any diacritics (accents) from characters
.replace(/[^\p{L}\p{N}\p{Zs}\p{Emoji}]+/gu, "-") // Replace any non-alphanumeric characters (including Unicode) with a dash
.replace(/[\s_]+/g, "-") // Replace whitespace and underscores with a single dash
.replace(/[\s_#]+/g, "-") // Replace whitespace, # and underscores with a single dash
.replace(/^-+/, "") // Remove dashes from start
.replace(/-+$/, "") // Remove dashes from end
.replace(/\+/g, "-"); // Transform all + to -
.replace(/-+$/, ""); // Remove dashes from end
};
export default slugify;