dodo / node-slug

slugifies even utf-8 chars!
MIT License
1.08k stars 92 forks source link

Fixed two bugs with handling of the replacement character + tests #74

Open raydog opened 8 years ago

raydog commented 8 years ago

Fixes two bugs I found while reading the source code:

  1. The regexp that replaces multiple spaces/separators accidentally hard-codes '-' in the regexp, meaning that this behavior won't work with custom separators.
  2. The code that replaces trailing separators doesn't use the regexp constructor, so it will only remove a trailing separator when followed by a '$' character, which is strange. This fixes that behavior, as well as extends it to cover leading separators as well.

This is similar to PR #55, except that this code escapes the separator, so it should still work when the replacement character is a regexp character.

lillem4n commented 6 years ago

I added your test for multiple leading and trailing separators in my fork here: https://github.com/larvit/larvitslugify/commit/e1500dd83adeca0d10ee82ebc9621b44c10899ae

But since I rewrote the base code quite a bit for a lot of reasons, I made it an option to remove multiple whitespaces. That collides with hard coding something that removes multiple instances of the replace character as well... I have no pretty solution for this, do you?