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

Support leading underscore in to_camel_case #1284

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions graphene/utils/str_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
# Adapted from this response in Stackoverflow
# http://stackoverflow.com/a/19053800/1072990
def to_camel_case(snake_str):
components = snake_str.split("_")

# We capitalize the first letter of each component except the first one
# with the 'capitalize' method and join them together.
return components[0] + "".join(x.capitalize() if x else "_" for x in components[1:])
def _camel_case_convert(components):
return components[0] + "".join(x.capitalize() if x else "_" for x in components[1:])

leading_underscore = False
if snake_str.startswith('_'):
leading_underscore = True
snake_str = snake_str[1:]

components = snake_str.split("_")

if leading_underscore:
return "_" + _camel_case_convert(components)
return _camel_case_convert(components)


# From this response in Stackoverflow
Expand Down
1 change: 1 addition & 0 deletions graphene/utils/tests/test_str_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ def test_camel_case():
assert to_camel_case("snakes_on_a__plane") == "snakesOnA_Plane"
assert to_camel_case("i_phone_hysteria") == "iPhoneHysteria"
assert to_camel_case("field_i18n") == "fieldI18n"
assert to_camel_case("_private_field") == "_privateField"