diff --git a/inflection/__init__.py b/inflection/__init__.py index ffac5ce..a5337c0 100644 --- a/inflection/__init__.py +++ b/inflection/__init__.py @@ -277,7 +277,7 @@ def parameterize(string: str, separator: str = '-') -> str: if separator: re_sep = re.escape(separator) # No more than one of the separator in a row. - string = re.sub(r'%s{2,}' % re_sep, separator, string) + string = re.sub(r'(?:%s){2,}' % re_sep, separator, string) # Remove leading/trailing separator. string = re.sub(r"(?i)^{sep}|{sep}$".format(sep=re_sep), '', string) diff --git a/test_inflection.py b/test_inflection.py index f1e64a3..53c04d8 100644 --- a/test_inflection.py +++ b/test_inflection.py @@ -402,6 +402,13 @@ def test_parameterize_with_multi_character_separator( ) +def test_parameterize_with_multi_character_separator_partial_collapse() -> None: + # Regression test: multi-character separator should not consume + # non-separator characters that happen to overlap with separator chars. + # '__' is a 2-char separator, so '___' = one separator + one literal '_'. + assert 'test___case' == inflection.parameterize('test___case', '__') + + @pytest.mark.parametrize( ("some_string", "parameterized_string"), STRING_TO_PARAMETERIZE_WITH_NO_SEPARATOR