Skip to content

Commit

Permalink
Merge pull request #66 from sgcarstrends/65-fix-regex-vulnerability-i…
Browse files Browse the repository at this point in the history
…n-slugify

Fix regex vulnerability
  • Loading branch information
ruchernchong authored Nov 22, 2024
2 parents 0225fa1 + bd44404 commit c623a13
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/utils/slugify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
*/
export const slugify = (str: string): string => {
return str
.trim()
.toLowerCase()
.replace(/&/g, "and")
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+/g, "")
.replace(/-+$/g, "");
.replace(/[\W_]+/g, "-");
};

/**
* Converts a slug back to a readable title (basic version)
* Converts a slug back to a readable title
*
* @param {string} slug - The slug to be converted back to a title
* @returns {string} The readable title
*/
export const deslugify = (slug: string): string => {
return slug
.replace(/-+/g, "-")
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.trim()
.toLowerCase()
.replace(/-+/g, " ")
.replace(/\b\w/g, (c) => c.toUpperCase())
.trim();
};

0 comments on commit c623a13

Please sign in to comment.