Added to_const str converter utility

This commit is contained in:
Syrus Akbary 2016-04-01 23:39:10 -07:00
parent 1f548f188d
commit bd88f2327e
3 changed files with 13 additions and 3 deletions

View File

@ -1,4 +1,4 @@
from .str_converters import to_camel_case, to_snake_case from .str_converters import to_camel_case, to_snake_case, to_const
from .proxy_snake_dict import ProxySnakeDict from .proxy_snake_dict import ProxySnakeDict
from .caching import cached_property, memoize from .caching import cached_property, memoize
from .maybe_func import maybe_func from .maybe_func import maybe_func
@ -7,6 +7,6 @@ from .resolve_only_args import resolve_only_args
from .lazylist import LazyList from .lazylist import LazyList
__all__ = ['to_camel_case', 'to_snake_case', 'ProxySnakeDict', __all__ = ['to_camel_case', 'to_snake_case', 'to_const', 'ProxySnakeDict',
'cached_property', 'memoize', 'maybe_func', 'enum_to_graphql_enum', 'cached_property', 'memoize', 'maybe_func', 'enum_to_graphql_enum',
'resolve_only_args', 'LazyList'] 'resolve_only_args', 'LazyList']

View File

@ -15,3 +15,7 @@ def to_camel_case(snake_str):
def to_snake_case(name): def to_snake_case(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def to_const(string):
return re.sub('[\W|^(?=\d)]+', '_', string).upper()

View File

@ -1,4 +1,5 @@
from ..str_converters import to_camel_case, to_snake_case # coding: utf-8
from ..str_converters import to_camel_case, to_snake_case, to_const
def test_snake_case(): def test_snake_case():
@ -15,3 +16,8 @@ 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'
def test_to_const():
assert to_const('snakes on a plane') == 'SNAKES_ON_A_PLANE'
assert to_const('weirdñáunicode$# word') == 'WEIRD_UNICODE_WORD'