fix: slugify from creating double+ dashes (#14002)

* fix: slugify

* fix: strip emojis

* fix: remove emojis

* fix: remove emojis

---------

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
This commit is contained in:
sean-brydon
2024-03-07 13:34:18 +00:00
committed by GitHub
co-authored by Udit Takkar
parent fc062878ed
commit 0784df6a31
2 changed files with 20 additions and 4 deletions
+12 -3
View File
@@ -45,9 +45,18 @@ describe("slugify", () => {
expect(slugify("hellothere.")).toEqual("hellothere");
});
it("Should replace consecutive dashes with a single dash", () => {
expect(slugify("Hello - World 123_ !@# Test 456 789")).toEqual("hello-world-123-test-456-789");
});
// This is failing, if we want to fix it, one approach is as used in getValidRhfFieldName
it.skip("should remove unicode and emoji characters", () => {
expect(slugify("Hello 📚🕯️®️ There")).toEqual("hello---------there");
expect(slugify("📚🕯️®️")).toEqual("");
it("should remove emoji characters", () => {
expect(slugify("Hello 📚🕯️ There")).toEqual("hello-there");
expect(slugify("📚🕯️")).toEqual("");
});
it.skip("should remove unicode", () => {
expect(slugify("Hello ®️ There")).toEqual("hello-there");
expect(slugify("®️")).toEqual("");
});
});
+8 -1
View File
@@ -5,6 +5,7 @@ export const slugify = (str: string, forDisplayingInput?: boolean) => {
if (!str) {
return "";
}
const s = str
.toLowerCase() // Convert to lowercase
.trim() // Remove whitespace from both sides
@@ -14,7 +15,13 @@ export const slugify = (str: string, forDisplayingInput?: boolean) => {
.replace(/[\s_#]+/g, "-") // Replace whitespace, # and underscores with a single dash
.replace(/^-+/, "") // Remove dashes from start
.replace(/\.{2,}/g, ".") // Replace consecutive periods with a single period
.replace(/^\.+/, ""); // Remove periods from the start
.replace(/^\.+/, "") // Remove periods from the start
.replace(
/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,
""
) // Removes emojis
.replace(/\s+/g, " ")
.replace(/-+/g, "-"); // Replace consecutive dashes with a single dash
return forDisplayingInput ? s : s.replace(/-+$/, "").replace(/\.*$/, ""); // Remove dashes and period from end
};