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

Add preserveTrailingDash option #57

Merged
merged 3 commits into from
May 13, 2021
Merged

Conversation

vhpoet
Copy link
Contributor

@vhpoet vhpoet commented Apr 21, 2021

I'm using slugify on an input for the user to create a slug like below:

const [slug, setSlug] = useState();

<input onChange={(value) => setSlug(slugify(value))} value={slug} />

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 without preserveTrailingDash before submitting the form to make sure I don't have a trailing dash in the final slug, but that's ok.

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

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.

Copy link
Contributor Author

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?

Copy link
Owner

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.

Copy link
Contributor Author

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-');
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus sindresorhus changed the title Add preserveTrailingDash option Add preserveTrailingDash option Apr 21, 2021
@sindresorhus sindresorhus merged commit 2aa5659 into sindresorhus:main May 13, 2021
@sindresorhus
Copy link
Owner

Looks good :)

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

Successfully merging this pull request may close these issues.

2 participants