@@ -148,22 +148,18 @@ export const truncate = (text: string, maxLength = 80, ellipses = true) => {
148148}
149149
150150export const slugify = ( text : string ) => {
151- const slug = text
151+ return text
152152 . normalize ( 'NFKD' ) // The normalize() using NFKD method returns the Unicode Normalization Form of a given string.
153153 . toLowerCase ( ) // Convert the string to lowercase letters
154- . trim ( ) // Remove whitespace from both sides of a string (optional)
155- . replace ( / [ ^ a - z 0 - 9 \s _ ] / g, '' ) // Remove characters that are NOT alphanumeric (a-z, 0-9), whitespace, or underscores
156- . replace ( / \s + / g, '-' ) // Replace spaces with -
157- . replace ( / _ / g, '-' ) // Replace _ with -
158- . replace ( / - { 2 , } / g, '-' ) // Replace multiple - with single -
159- . replace ( / - $ / g, '' ) // Remove trailing -
160-
161- return slug
154+ . trim ( ) // Remove whitespace from both sides of a string
155+ . replace ( / [ ^ a - z 0 - 9 \s _ - ] / g, '' ) // Remove characters that are NOT alphanumeric (a-z, 0-9), whitespace, hyphens, or underscores
156+ . replace ( / [ \s _ ] + / g, '-' ) // Replace sequences of one or more whitespace characters and/or underscores with a single hyphen
157+ . replace ( / - { 2 , } / g, '-' ) // Replace multiple consecutive hyphens with a single hyphen
158+ . replace ( / ^ - + | - + $ / g, '' ) // Remove any leading or trailing hyphens
162159}
163160
164161export const unslugify = ( text : string ) => {
165- const unslug = text . replace ( / - / g, ' ' ) // Replace - with spaces
166- return unslug
162+ return text . replace ( / - / g, ' ' ) // Replace hyphens with spaces
167163}
168164
169165// Not inspired by this, but useful reference for corner cases:
0 commit comments