simov / slugify

Slugifies a string
MIT License
1.5k stars 129 forks source link

"remove" property on options aren't working correctly #158

Closed dhanielsales closed 2 years ago

dhanielsales commented 2 years ago

Hello guys,

I'm using the property remove on options and i haven't has the correct result. I will post the situation below.

import slugify from 'slugify'

const text = 'Natação foo % ® sdf'

const foo = slugify(text, {
  replacement: ' ',
  lower: false,
  remove: /[*+~.'"!?;:@%$#&=§ªº°·─®]/g,
})

console.log(foo)

// Console Output: 
// Natacao foo percent (r) sdf

Even putting in remove a regex with % it is still replaced by the word percent. The same result for ®.

If I'm doing something wrong, please let me know and I'll retract.

The version i'm using is 1.6.5

Trott commented 2 years ago

remove takes effect after all the characters in the string have been processed. It doesn't operate directly on the input.

You probably want to use extend instead:

slugify.extend({ '%': '', '®': '' })

const foo = slugify(text, {
  replacement: ' ',
  lower: false,
})

Another option would be to use the slug module instead of slugify, as the current version (5.3.0) has the behavior you want (ignoring symbols like % and ®) by default.

import slug from 'slug'

const text = 'Natação foo % ® sdf'

const foo = slug(text, {
    replacement: ' ',
    lower: false,
})

console.log(foo) // Natacao foo sdf
dhanielsales commented 2 years ago

I understand now thanks for the explanation :smile: