mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-22 13:59:51 +03:00
Support leading underscore
This commit is contained in:
parent
e0d4bec2d8
commit
74c0d0e782
|
@ -4,10 +4,22 @@ import re
|
|||
# Adapted from this response in Stackoverflow
|
||||
# http://stackoverflow.com/a/19053800/1072990
|
||||
def to_camel_case(snake_str):
|
||||
|
||||
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("_")
|
||||
|
||||
# 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:])
|
||||
if leading_underscore:
|
||||
return "_" + _camel_case_convert(components)
|
||||
return _camel_case_convert(components)
|
||||
|
||||
|
||||
# From this response in Stackoverflow
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue
Block a user