mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-27 08:19:45 +03:00
Merge faf8a41118
into 38db32e4f2
This commit is contained in:
commit
4374f14a5f
|
@ -1,13 +1,38 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
# From this response in Stackoverflow
|
|
||||||
# http://stackoverflow.com/a/19053800/1072990
|
|
||||||
def to_camel_case(snake_str):
|
def to_camel_case(snake_str):
|
||||||
components = snake_str.split('_')
|
"""
|
||||||
# We capitalize the first letter of each component except the first one
|
Return a camel-cased version of a snake-cased string.
|
||||||
# with the 'title' method and join them together.
|
|
||||||
return components[0] + "".join(x.title() if x else '_' for x in components[1:])
|
Leading underscores and multiple consecutive underscores are kept
|
||||||
|
intact.
|
||||||
|
|
||||||
|
:param snake_str: A snake-cased string.
|
||||||
|
:return: A camel-cased string.
|
||||||
|
"""
|
||||||
|
# Find all subcomponents in a snake-cased string, including
|
||||||
|
# any trailing underscores, which are treated as a separate
|
||||||
|
# component.
|
||||||
|
snake_case_sub_strings = re.findall(r'(_*[a-zA-Z]+|_+$)', snake_str)
|
||||||
|
|
||||||
|
# The first variable is unchanged case-wise (and leading
|
||||||
|
# underscores preserved as-is).
|
||||||
|
camel_case_sub_strings = [snake_case_sub_strings[0]]
|
||||||
|
|
||||||
|
for s in snake_case_sub_strings[1:]:
|
||||||
|
# We reset the camel casing algorithm if more than one
|
||||||
|
# underscore is encountered. The endswith handles any
|
||||||
|
# trailing underscores in the original snake-cased
|
||||||
|
# variable.
|
||||||
|
if s.startswith('__') or s.endswith('_'):
|
||||||
|
camel_case_sub_strings.append(s)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Otherwise we replace '_name' with 'Name', for example.
|
||||||
|
camel_case_sub_strings.append(s[1:].title())
|
||||||
|
|
||||||
|
return ''.join(camel_case_sub_strings)
|
||||||
|
|
||||||
|
|
||||||
# From this response in Stackoverflow
|
# From this response in Stackoverflow
|
||||||
|
|
|
@ -14,8 +14,12 @@ def test_snake_case():
|
||||||
|
|
||||||
def test_camel_case():
|
def test_camel_case():
|
||||||
assert to_camel_case('snakes_on_a_plane') == 'snakesOnAPlane'
|
assert to_camel_case('snakes_on_a_plane') == 'snakesOnAPlane'
|
||||||
assert to_camel_case('snakes_on_a__plane') == 'snakesOnA_Plane'
|
assert to_camel_case('snakes_on_a__plane') == 'snakesOnA__plane'
|
||||||
assert to_camel_case('i_phone_hysteria') == 'iPhoneHysteria'
|
assert to_camel_case('i_phone_hysteria') == 'iPhoneHysteria'
|
||||||
|
assert to_camel_case('i_phone_hysteria_') == 'iPhoneHysteria_'
|
||||||
|
assert to_camel_case('_i_phone_hysteria') == '_iPhoneHysteria'
|
||||||
|
assert to_camel_case('__i_phone_hysteria') == '__iPhoneHysteria'
|
||||||
|
assert to_camel_case('__all__') == '__all__'
|
||||||
|
|
||||||
|
|
||||||
def test_to_const():
|
def test_to_const():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user