-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix inconsistencies in
slugify
between frontend and core implementa…
…tion (#18297)
- Loading branch information
1 parent
77e05de
commit c48b620
Showing
2 changed files
with
30 additions
and
15 deletions.
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
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; | ||
}; |
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