Skip to content

Commit bd88f23

Browse files
committed
Added to_const str converter utility
1 parent 1f548f1 commit bd88f23

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

graphene/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .str_converters import to_camel_case, to_snake_case
1+
from .str_converters import to_camel_case, to_snake_case, to_const
22
from .proxy_snake_dict import ProxySnakeDict
33
from .caching import cached_property, memoize
44
from .maybe_func import maybe_func
@@ -7,6 +7,6 @@
77
from .lazylist import LazyList
88

99

10-
__all__ = ['to_camel_case', 'to_snake_case', 'ProxySnakeDict',
10+
__all__ = ['to_camel_case', 'to_snake_case', 'to_const', 'ProxySnakeDict',
1111
'cached_property', 'memoize', 'maybe_func', 'enum_to_graphql_enum',
1212
'resolve_only_args', 'LazyList']

graphene/utils/str_converters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ def to_camel_case(snake_str):
1515
def to_snake_case(name):
1616
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
1717
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
18+
19+
20+
def to_const(string):
21+
return re.sub('[\W|^(?=\d)]+', '_', string).upper()

graphene/utils/tests/test_str_converter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from ..str_converters import to_camel_case, to_snake_case
1+
# coding: utf-8
2+
from ..str_converters import to_camel_case, to_snake_case, to_const
23

34

45
def test_snake_case():
@@ -15,3 +16,8 @@ def test_camel_case():
1516
assert to_camel_case('snakes_on_a_plane') == 'snakesOnAPlane'
1617
assert to_camel_case('snakes_on_a__plane') == 'snakesOnA_Plane'
1718
assert to_camel_case('i_phone_hysteria') == 'iPhoneHysteria'
19+
20+
21+
def test_to_const():
22+
assert to_const('snakes on a plane') == 'SNAKES_ON_A_PLANE'
23+
assert to_const('weirdñáunicode$# word') == 'WEIRD_UNICODE_WORD'

0 commit comments

Comments
 (0)