Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Vacilando opened this issue Nov 24, 2021 · 8 comments
Open

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

Vacilando opened this issue Nov 24, 2021 · 8 comments

Comments

@Vacilando
Copy link

Vacilando commented Nov 24, 2021

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

Is the replacement of dashes configurable?

@Vacilando Vacilando changed the title How to change the replacement behaviour for underscore ("_") How to change the replacement behaviour for dash ("-") Nov 24, 2021
@Trott
Copy link
Collaborator

Trott commented Nov 25, 2021

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
Copy link
Author

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

  • The expected output of slugify() is "1952--".
  • The actual output is "1952".

@Trott
Copy link
Collaborator

Trott commented Nov 26, 2021

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
Copy link
Collaborator

Trott commented Nov 26, 2021

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
Copy link

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
Copy link
Collaborator

Trott commented Aug 31, 2022

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
Copy link

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

@stevenwoodson
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants