Skip to content

Commit

Permalink
Merge pull request #17 from portfoliome/camel-case
Browse files Browse the repository at this point in the history
Camel case
  • Loading branch information
pmart123 authored Dec 20, 2016
2 parents 5883b43 + a0418aa commit f4d2f1a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion foil/_version.py
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))
18 changes: 18 additions & 0 deletions foil/strings.py
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:])
18 changes: 18 additions & 0 deletions tests/test_strings.py
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)

0 comments on commit f4d2f1a

Please sign in to comment.