Skip to content

Use the actual separator in code - #47

Closed
KlaasH wants to merge 3 commits into
un33k:masterfrom
KlaasH:feature/use-separator-in-code
Closed

KlaasH wants to merge 3 commits into
un33k:masterfrom
KlaasH:feature/use-separator-in-code

Conversation

@KlaasH

@KlaasH KlaasH commented Oct 21, 2017

Copy link
Copy Markdown

The underlying motivation for this is that I wanted to be able to slugify text that contains hyphens and keep the hyphens. It seemed like that should work with

slugify('hyphen-having text FTW!', separator='_', regex_pattern=r'[^-a-z0-9]')

i.e. that that should produce 'hyphen-having_text_ftw'. In fact it produced 'hyphen_having_text_ftw' because, behind the scenes, this was always using hyphen as the separator, just switching it out and the end if the separator was overridden, which means hyphen couldn't be an allowed character.

This changes it to use the specified separator from the beginning.

One side effect of this is that the test with a multi-character separator and max_length=20 produces a different result, because the multi-character separator counts against the length. This seems like an improvement to me.

@coveralls

coveralls commented Oct 21, 2017

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 93.976% when pulling 2c47a53 on KlaasH:feature/use-separator-in-code into e14bde2 on un33k:master.

Though the separator can be overridden, this has been using the default
separator ('-') behind the scenes then replacing it with the chosen
separator as the last step. This means, among other things, that hyphens
would always be replaced with the separator, even if you overrode the
allowed characters regex to try to allow them.

This changes `slugify` to use the provided `separator` throughout, which
required some changes to the allowed-characters and multiple-separators
logic.
Since `slugify` is no longer using '-' as a separator behind the scenes
and switching at the last minute, the "collapse multiple separators" and
"truncate" steps are now operating on the text with the chosen separator
rather than on one with hyphens. Which changes the expected output of a
couple tests.

- test_multi_character_separator is different because the multi-character
  separator now counts against the length calculation, so it had to drop
  one piece.
- test_regex_pattern_keep_underscore_with_underscore_as_separator now
  strips leading and trailing underscores, rather than just collapsing
  them.

To me the new behavior seems more correct.
Adds two tests, both with an overridden separator, to exercise the new
behavior around hyphens. 'test_separator_remove_hyphens' will behave the
same as it would have before, but for different reasons.
'test_regex_pattern_allow_hyphens' will now pass, when it would have
failed (replaced hyphens even though they're supposed to be allowed)
before.
@KlaasH
KlaasH force-pushed the feature/use-separator-in-code branch from 2c47a53 to 0bff01f Compare October 21, 2017 01:42
@coveralls

coveralls commented Oct 21, 2017

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 93.976% when pulling 0bff01f on KlaasH:feature/use-separator-in-code into e14bde2 on un33k:master.

@un33k

un33k commented Feb 19, 2018

Copy link
Copy Markdown
Owner

This is to generate a human-readable set of words. Dash-separated words that is. Anything else is out of the scope of this package. Feel free to fork and adapt for your own edge-case.

@un33k un33k closed this Feb 19, 2018
@KlaasH

KlaasH commented Feb 19, 2018

Copy link
Copy Markdown
Author

The separator and regex_pattern options are already there. I expected that they could be used together and didn't expect that - would be a mandatory excluded character. I.e. to me this change wouldn't expand the scope, just make the existing options work more predictably.

It also seems like a bug to me that max_length=20 means "No longer than 20 characters with a 1-character separator, possibly way longer than that if you use a long separator and the input has several short words."

@un33k

un33k commented Sep 8, 2026

Copy link
Copy Markdown
Owner

This is Dojo, posting a maintainer-authorized follow-up linking this PR to #191.

Partially addressed: modern mode budgets actual emitted separator width. Literal-hyphen preservation is NOT implemented; both algorithms retain global dash mapping.

The default algorithm remains legacy; improved output rules are opt-in. This note does not announce a published release, and no individual PR is being merged by this follow-up. Thank you for the contribution and discussion.

🚀 Generated with Dojo ⛩️

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.

3 participants