Skip to content

Commit

Permalink
New underscore to camel options
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Nov 12, 2019
1 parent 6655330 commit de00397
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/quorum/__init__.py
Expand Up @@ -114,7 +114,7 @@
from .util import is_iterable, request_json, get_field, get_object, is_mobile, is_tablet,\
is_browser, is_bot, browser_info, resolve_alias, page_types, find_types, norm_object, set_object,\
leafs, load_form, load_locale, get_locale, get_langs, set_locale, reset_locale, anotate_async,\
anotate_secure, run_thread, camel_to_underscore, camel_to_readable, underscore_to_readable,\
anotate_secure, run_thread, camel_to_underscore, camel_to_readable, underscore_to_camel, underscore_to_readable,\
generate_identifier, to_locale, nl_to_br, nl_to_br_jinja, sp_to_nbsp, sp_to_nbsp_jinja, unset, date_time,\
quote, unquote, is_content_type, parse_content_type, verify, verify_equal, verify_not_equal, verify_many,\
execute, deprecated, JSONEncoder
Expand Down
90 changes: 90 additions & 0 deletions src/quorum/test/util.py
Expand Up @@ -289,6 +289,13 @@ def test_browser_info(self):

@quorum.secured
def test_camel_to_underscore(self):
result = quorum.camel_to_underscore(None)
self.assertEqual(result, None)

result = quorum.camel_to_underscore("")
self.assertEqual(type(result), str)
self.assertEqual(result, "")

result = quorum.camel_to_underscore("HelloWorld")
self.assertEqual(type(result), str)
self.assertEqual(result, "hello_world")
Expand All @@ -303,6 +310,13 @@ def test_camel_to_underscore(self):

@quorum.secured
def test_camel_to_readable(self):
result = quorum.camel_to_readable(None)
self.assertEqual(result, None)

result = quorum.camel_to_readable("")
self.assertEqual(type(result), str)
self.assertEqual(result, "")

result = quorum.camel_to_readable("HelloWorld")
self.assertEqual(type(result), str)
self.assertEqual(result, "Hello World")
Expand Down Expand Up @@ -350,8 +364,80 @@ def test_camel_to_readable(self):
self.assertEqual(type(result), str)
self.assertEqual(result, "Hello World Hello World")

@quorum.secured
def test_underscore_to_camel(self):
result = quorum.underscore_to_camel(None)
self.assertEqual(result, None)

result = quorum.underscore_to_camel("")
self.assertEqual(type(result), str)
self.assertEqual(result, "")

result = quorum.underscore_to_camel("hello_world")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWorld")

result = quorum.underscore_to_camel("hello_world", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWorld")

result = quorum.underscore_to_camel("hello_world_hello_world")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWorldHelloWorld")

result = quorum.underscore_to_camel("hello_world_hello_world", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWorldHelloWorld")

result = quorum.underscore_to_camel("hello_world_")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWorld")

result = quorum.underscore_to_camel("hello_world_", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWorld")

result = quorum.underscore_to_camel("__hello_world__")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWorld")

result = quorum.underscore_to_camel("__hello_world__", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWorld")

result = quorum.underscore_to_camel("__hello___world__")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWorld")

result = quorum.underscore_to_camel("__hello___world__", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWorld")

result = quorum.underscore_to_camel("__hello___WORLD__")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWORLD")

result = quorum.underscore_to_camel("__hello___WORLD__", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWORLD")

result = quorum.underscore_to_camel("HelloWorld")
self.assertEqual(type(result), str)
self.assertEqual(result, "HelloWorld")

result = quorum.underscore_to_camel("HelloWorld", lower = True)
self.assertEqual(type(result), str)
self.assertEqual(result, "helloWorld")

@quorum.secured
def test_underscore_to_readable(self):
result = quorum.underscore_to_readable(None)
self.assertEqual(result, None)

result = quorum.underscore_to_readable("")
self.assertEqual(type(result), str)
self.assertEqual(result, "")

result = quorum.underscore_to_readable("hello_world")
self.assertEqual(type(result), str)
self.assertEqual(result, "Hello world")
Expand Down Expand Up @@ -392,6 +478,10 @@ def test_underscore_to_readable(self):
self.assertEqual(type(result), str)
self.assertEqual(result, "Hello World")

result = quorum.underscore_to_readable("__hello___world__", capitalize = True, separator = "-")
self.assertEqual(type(result), str)
self.assertEqual(result, "Hello-World")

@quorum.secured
def test_generate_identifier(self):
identifier = quorum.generate_identifier(
Expand Down
34 changes: 32 additions & 2 deletions src/quorum/util.py
Expand Up @@ -813,6 +813,7 @@ def camel_to_underscore(camel, separator = "_", lower = True):
conversion of the provided camel cased one.
"""

if not camel: return camel
value = FIRST_CAP_REGEX.sub(r"\1" + separator + r"\2", camel)
value = ALL_CAP_REGEX.sub(r"\1" + separator + r"\2", value)
if lower: value = value.lower()
Expand Down Expand Up @@ -841,10 +842,35 @@ def camel_to_readable(camel, lower = False, capitalize = False):
used to display a value to an end user.
"""

if not camel: return camel
underscore = camel_to_underscore(camel, lower = lower)
return underscore_to_readable(underscore, capitalize = capitalize)

def underscore_to_readable(underscore, capitalize = False):
def underscore_to_camel(underscore, lower = False):
"""
Converts the provided underscore cased based value into
a normalized camel cased string.
An optional lower parameter may be provided to obtain a
lower came case version of the string.
This is useful as most of the python string standards
are compliant with the underscore strategy.
:type underscore: String
:param underscore: The underscore cased string that is going to be
converted into an camel case based string.
:type lower: bool
:param lower: If the the first letter of the resulting camel
case string should be lower case (lower camel case).
:rtype: String
:return: The camel case based string resulting from the
conversion of the provided underscore cased one.
"""

if not underscore: return underscore
camel = underscore_to_readable(underscore, capitalize = True, separator = "")
if not lower: return camel
return camel[0].lower() + camel[1:]

def underscore_to_readable(underscore, capitalize = False, separator = " "):
"""
Converts the given underscore oriented string value
into a readable one meaning that the returned value
Expand All @@ -859,16 +885,20 @@ def underscore_to_readable(underscore, capitalize = False):
:type capitalize: bool
:param capitalize: If all of the words should be capitalized
or if instead only the first one should.
:type separator: String
:param separator: The separator to be used to join the multiple
parts of the resulting readable tokens.
:rtype: String
:return: The final human readable string that may be
used to display a value to an end user.
"""

if not underscore: return underscore
parts = underscore.split("_")
parts = [part for part in parts if part]
if capitalize: parts = [part[0].upper() + part[1:] for part in parts]
else: parts[0] = parts[0][0].upper() + parts[0][1:]
return " ".join(parts)
return separator.join(parts)

def generate_identifier(size = 16, chars = string.ascii_uppercase + string.digits):
"""
Expand Down

0 comments on commit de00397

Please sign in to comment.