simov / slugify

Slugifies a string
MIT License
1.47k stars 126 forks source link

How to change the replacement behaviour for dash ("-") #141

Open Vacilando opened 2 years ago

Vacilando commented 2 years ago

Dashes ("-") are simply removed by slugify. We need to have them replaced by something else.

Is the replacement of dashes configurable?

Trott commented 2 years ago

Dashes ("-") are simply removed by slugify.

Can you provide sample code or at least sample input and desired output? That is not the behavior I see. Dashes, at least in the middle of words and with a letter on either side, are preserved as far as I can tell.

> slugify('foo-bar')
'foo-bar'
>
Vacilando commented 2 years ago

In our case we have a string like "1952--", so the dashes are at the end.

Trott commented 2 years ago

By default, slugify will compress multiple occurrences of the separator and will also trim the separator at the start and end of the string.

To suppress the trimming behavior, set trim to false:

> slugify('1952--')
'1952'
> slugify('1952--', { trim: false})
'1952-'
>

That gets you part of the way there, but not quite.

Trott commented 2 years ago

Not sure about slugify on this one, but if using the slug module, you can add - to the charmap and then get the behavior you want:

> slug.charmap['-'] = '-'
'-'
> slug('1952--')
'1952--'

I'm not sure what the equivalent in slugify is but I'd be surprised if there wasn't one somewhere.

joshuabyler commented 1 year ago

If you look at slugify.js line 37 you'll see that each character gets checked against the replacement and if it's the same it gets replaced with a space.

If you pass josh- to slugify and replacement: '-' then you'll get josh as the output.

I'm running into this issue right now where I want - to be the replacement, but I don't want it to be replaced with a space

Trott commented 1 year ago

If you look at slugify.js line 37 you'll see that each character gets checked against the replacement and if it's the same it gets replaced with a space.

If you pass josh- to slugify and replacement: '-' then you'll get josh as the output.

I'm running into this issue right now where I want - to be the replacement, but I don't want it to be replaced with a space

Those spaces get turned into dashes again. I don't understand the problem you're describing. This seems to work as expected:

console.log(slugify('josh-', {trim: false})) // 'josh-'
joshuabyler commented 1 year ago

I'll double check and make sure this is still happening, hopefully the trim: false is the change I need to make

stevenwoodson commented 1 year ago

Here's another scenario to consider, if you pass either step -1 or step 1 you'll get step-1 as the response.

It'd be nice to have an option not to automatically replace instances of the replacement option with a space so we can choose to get step--1 for step -1 and step-1 for step 1 in the same example noted above. This will help avoid collisions of the same response from potentially different inputs.