From 0784df6a31a2bbe472f70e2172e31e04f9091093 Mon Sep 17 00:00:00 2001 From: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Date: Thu, 7 Mar 2024 13:34:18 +0000 Subject: [PATCH] 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> --- packages/lib/slugify.test.ts | 15 ++++++++++++--- packages/lib/slugify.ts | 9 ++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/lib/slugify.test.ts b/packages/lib/slugify.test.ts index 634e147c84..042ff7a0c5 100644 --- a/packages/lib/slugify.test.ts +++ b/packages/lib/slugify.test.ts @@ -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(""); }); }); diff --git a/packages/lib/slugify.ts b/packages/lib/slugify.ts index 63d1083681..0d6a742c04 100644 --- a/packages/lib/slugify.ts +++ b/packages/lib/slugify.ts @@ -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 };