-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from portfoliome/camel-case
Camel case
- Loading branch information
Showing
3 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
version_info = (0, 1, 2) | ||
version_info = (0, 1, 3) | ||
|
||
__version__ = '.'.join(map(str, version_info)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |