Skip to content

Commit

Permalink
Fix inconsistencies in slugify between frontend and core implementa…
Browse files Browse the repository at this point in the history
…tion (#18297)
  • Loading branch information
Caius-Bonus authored Oct 23, 2023
1 parent 77e05de commit c48b620
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
36 changes: 23 additions & 13 deletions src/common/string/slugify.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
export const slugify = (value: string, delimiter = "_") => {
const a =
"àáäâãåăæąçćčđďèéěėëêęğǵḧìíïîįłḿǹńňñòóöôœøṕŕřßşśšșťțùúüûǘůűūųẃẍÿýźžż·/_,:;";
const b = `aaaaaaaaacccddeeeeeeegghiiiiilmnnnnooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}${delimiter}${delimiter}${delimiter}${delimiter}${delimiter}`;
"àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·";
const b = `aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}`;
const p = new RegExp(a.split("").join("|"), "g");

return value
.toString()
.toLowerCase()
.replace(/\s+/g, delimiter) // Replace spaces with delimiter
.replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, `${delimiter}and${delimiter}`) // Replace & with 'and'
.replace(/[^\w-]+/g, "") // Remove all non-word characters
.replace(/-/g, delimiter) // Replace - with delimiter
.replace(new RegExp(`(${delimiter})\\1+`, "g"), "$1") // Replace multiple delimiters with single delimiter
.replace(new RegExp(`^${delimiter}+`), "") // Trim delimiter from start of text
.replace(new RegExp(`${delimiter}+$`), ""); // Trim delimiter from end of text
let slugified;

if (value === "") {
slugified = "";
} else {
slugified = value
.toString()
.toLowerCase()
.replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/(?<=\d),(?=\d)/g, "") // Remove Commas between numbers
.replace(/[^a-z0-9]+/g, delimiter) // Replace all non-word characters
.replace(new RegExp(`(${delimiter})\\1+`, "g"), "$1") // Replace multiple delimiters with single delimiter
.replace(new RegExp(`^${delimiter}+`), "") // Trim delimiter from start of text
.replace(new RegExp(`${delimiter}+$`), ""); // Trim delimiter from end of text

if (slugified === "") {
slugified = "unknown";
}
}

return slugified;
};
9 changes: 7 additions & 2 deletions test/common/string/slugify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ describe("slugify", () => {
assert.strictEqual(slugify("abc"), "abc");
assert.strictEqual(slugify("ABC"), "abc");
assert.strictEqual(slugify("abc DEF"), "abc_def");
assert.strictEqual(slugify("abc.DEF"), "abc_def");
assert.strictEqual(slugify("1`-=~!@#$%^&*()_+[];',./{}:\"<>?\\| aA"), "1_aa");
assert.strictEqual(slugify("abc-DEF"), "abc_def");
assert.strictEqual(slugify("abc_DEF"), "abc_def");
assert.strictEqual(slugify("1,1"), "11");
assert.strictEqual(slugify("abc å DEF"), "abc_a_def");
assert.strictEqual(slugify("abc:DEF"), "abc_def");
assert.strictEqual(slugify("abc&DEF"), "abc_and_def");
assert.strictEqual(slugify("abc^^DEF"), "abcdef");
assert.strictEqual(slugify("abc&DEF"), "abc_def");
assert.strictEqual(slugify("abc^^DEF"), "abc_def");
assert.strictEqual(slugify("abc DEF"), "abc_def");
assert.strictEqual(slugify("_abc DEF"), "abc_def");
assert.strictEqual(slugify("abc DEF_"), "abc_def");
Expand All @@ -20,6 +23,8 @@ describe("slugify", () => {
assert.strictEqual(slugify("abc - DEF - ghi"), "abc_def_ghi");
assert.strictEqual(slugify("abc---DEF---ghi"), "abc_def_ghi");
assert.strictEqual(slugify("___abc___DEF___ghi___"), "abc_def_ghi");
assert.strictEqual(slugify("___"), "unknown");
assert.strictEqual(slugify(""), "");

// With custom delimiter
assert.strictEqual(slugify("abc def", "-"), "abc-def");
Expand Down

0 comments on commit c48b620

Please sign in to comment.