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

Consecutive underscores in camelize/underscore transformations aren't reversible #26

Open
levic opened this issue May 2, 2018 · 0 comments

Comments

@levic
Copy link

levic commented May 2, 2018

Every underscore/camelize cycle drops an underscore in the case of consecutive underscores.

Consider the following snippet:

import inflection
x = 'a____b_c'
for i in range(4):
	x = inflection.camelize(x)
	print(x)
	x = inflection.underscore(x)
	print(x)

Output:

A__bC
a__b_c
A_bC
a_b_c
ABC
abc
Abc
abc

I understand that not every transformation is going to be cleanly reversible.

Given that underscores are already word separators a number of python packages use double underscores as higher-level separators though (most notably django's ORM but also eg fixture generators factoryboy and model mommy). Preserving reversibility of consecutive underscores is a useful property.

The tests and docs don't seem cover the behaviour of consecutive underscores -- it seems to be undefined.

Would you accept a patch that changed the behaviour to preserve consecutive underscores? My proposal is:

  • For underscores in the middle of a word, camelizing drops one underscore:
camelize('a___b_c') == 'A__bC'
camelize('a__b_c') == 'A_bC'
camelize('a_b_c') == 'ABC'
underscore('A__bC') == 'a___b_c'
underscore('A_bC') == 'a__b_c'
underscore('ABC') == 'a_b_c'
  • For underscores at the start of a word, camelizing preserves the number of underscores and doesn't uppercase the first non-underscore letter
camelize('___a') == '___a'
camelize('__a') == '__a'
camelize('_a') == '_a'
underscore('___a') == '___a'
underscore('__a') == '__a'
underscore('_a') == '_a'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants