From de473bebd590ef7fe733c859a9314cc9cfbb587d Mon Sep 17 00:00:00 2001 From: pmart123 Date: Tue, 20 Dec 2016 03:42:13 -0500 Subject: [PATCH 1/2] Specification for snake/camel string conversion. --- foil/strings.py | 18 ++++++++++++++++++ tests/test_strings.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 foil/strings.py create mode 100644 tests/test_strings.py diff --git a/foil/strings.py b/foil/strings.py new file mode 100644 index 0000000..f563b2a --- /dev/null +++ b/foil/strings.py @@ -0,0 +1,18 @@ +import re + + +CAMEL_CASE_RE = re.compile(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))') + + +def camel_to_snake(s: str) -> str: + """Convert string from camel case to snake case.""" + + return CAMEL_CASE_RE.sub(r'_\1', s).strip().lower() + + +def snake_to_camel(s: str) -> str: + """Convert string from snake case to camel case.""" + + fragments = s.split('_') + + return fragments[0] + ''.join(x.title() for x in fragments[1:]) diff --git a/tests/test_strings.py b/tests/test_strings.py new file mode 100644 index 0000000..454940f --- /dev/null +++ b/tests/test_strings.py @@ -0,0 +1,18 @@ +import unittest + +from foil.strings import camel_to_snake, snake_to_camel + + +class TestSnakeCamel(unittest.TestCase): + + def test_camel_to_snake_case(self): + expected = 'hello_world' + result = camel_to_snake('helloWorld') + + self.assertEqual(expected, result) + + def test_snake_to_camel_case(self): + expected = '123HelloWorld' + result = snake_to_camel('123_hello_world') + + self.assertEqual(expected, result) From a0418aa095c427cab3cb628350e2cbb4e0c41714 Mon Sep 17 00:00:00 2001 From: pmart123 Date: Tue, 20 Dec 2016 03:43:04 -0500 Subject: [PATCH 2/2] Bump version. --- foil/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/foil/_version.py b/foil/_version.py index a82d568..91eab20 100644 --- a/foil/_version.py +++ b/foil/_version.py @@ -1,3 +1,3 @@ -version_info = (0, 1, 2) +version_info = (0, 1, 3) __version__ = '.'.join(map(str, version_info))