Skip to content

Commit 5db178b

Browse files
committed
update to_snake_case function
1 parent 4ded9ef commit 5db178b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

linebot/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
"""linebot.http_client module."""
15+
"""linebot.utils module."""
1616

1717
from __future__ import unicode_literals
1818

1919
import logging
2020
import re
21+
2122
import sys
2223

2324
LOGGER = logging.getLogger('linebot')
@@ -31,8 +32,10 @@ def to_snake_case(text):
3132
:param str text:
3233
:rtype: str
3334
"""
34-
s1 = re.sub('(.)([A-Z0-9][a-z]+)', r'\1_\2', text)
35-
return re.sub('([a-z])([A-Z0-9])', r'\1_\2', s1).lower()
35+
s1 = re.sub('(.)([A-Z])', r'\1_\2', text)
36+
s2 = re.sub('(.)([0-9]+)', r'\1_\2', s1)
37+
s3 = re.sub('([0-9])([a-z])', r'\1_\2', s2)
38+
return s3.lower()
3639

3740

3841
def to_camel_case(text):

tests/test_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
class TestUtils(unittest.TestCase):
2323
def test_to_snake_case(self):
24-
self.assertEqual(to_snake_case('hogeBar'), 'hoge_bar')
24+
self.assertEqual(to_snake_case('hogeBarFuga'), 'hoge_bar_fuga')
2525
self.assertEqual(to_snake_case('uniqueMediaPlayed100Percent'), 'unique_media_played_100_percent')
26+
self.assertEqual(to_snake_case('festival20Days'), 'festival_20_days')
27+
self.assertEqual(to_snake_case('festival20days'), 'festival_20_days')
2628

2729
def test_to_camel_case(self):
2830
self.assertEqual(to_camel_case('hoge_bar'), 'hogeBar')
31+
self.assertEqual(to_camel_case('unique_media_played_100_percent'), 'uniqueMediaPlayed100Percent')
2932

3033
def test_safe_compare_digest_true(self):
3134
self.assertTrue(safe_compare_digest('/gg9a+LvFevTH1sd7', '/gg9a+LvFevTH1sd7'))

0 commit comments

Comments
 (0)