-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Add preserveTrailingDash
option
#57
Conversation
index.d.ts
Outdated
/** | ||
If your string ends with a dash, it will be preserved in the slugified string. | ||
|
||
Sometimes trailing dashes are intentional, for example, when using slugify on an input field used for setting a slug. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be clearer that the main use-case for this is to allow validation while not preventing the user to write a slug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how exactly to formulate it, could you suggest an edit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should give it a try first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about now? 2142006
@@ -133,6 +133,11 @@ test('leading underscore', t => { | |||
t.is(slugify('____-___foo__bar', {preserveLeadingUnderscore: true}), '_foo-bar'); | |||
}); | |||
|
|||
test('trailing dash', t => { | |||
t.is(slugify('foo bar-', {preserveTrailingDash: true}), 'foo-bar-'); | |||
t.is(slugify('foo-bar--', {preserveTrailingDash: true}), 'foo-bar-'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some more assertions. For example, foo-bar -
, foo-bar
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preserveTrailingDash
option
Looks good :) |
I'm using
slugify
on an input for the user to create aslug
like below:and so I want the user input to be slugified on the spot, but the problem is that the user cannot enter a dash in the input, since it will get removed.
With this PR, I should be able to do
slugify(value, {preserveTrailingDash: true})
and make my use case work.It seems like a pretty generic use case so I thought I'd make a PR (looked at #43 for a code style reference).
P. S. I will need to call
slugify
once more withoutpreserveTrailingDash
before submitting the form to make sure I don't have a trailing dash in the final slug, but that's ok.