Skip to content
This repository
Browse code

Clean-roomed the ``space_out_camel_case`` function.

  • Loading branch information...
commit efd598471cf0c75996facf7e2d9280c144192897 1 parent 89ee7f2
Daniel Lindsley authored January 31, 2012 issackelly committed September 09, 2012
15  servee/frontendadmin/tests/tests.py
... ...
@@ -1,3 +1,4 @@
  1
+from unittest import TestCase as UnittestTestCase
1 2
 from django.contrib.auth import authenticate, login
2 3
 from django.contrib.auth.models import User
3 4
 from django.contrib.flatpages.models import FlatPage
@@ -8,6 +9,20 @@
8 9
 from helpers import RequestFactory
9 10
 
10 11
 
  12
+class SpaceOutTestCase(UnittestTestCase):
  13
+    def test_variants(self):
  14
+        from servee.utils import space_out_camel_case
  15
+
  16
+        self.assertEqual(space_out_camel_case('nospacesanywhere'), 'nospacesanywhere')
  17
+        self.assertEqual(space_out_camel_case('A'), 'A')
  18
+        self.assertEqual(space_out_camel_case('AB'), 'AB')
  19
+        self.assertEqual(space_out_camel_case('Abc'), 'Abc')
  20
+        self.assertEqual(space_out_camel_case('ABc'), 'A Bc')
  21
+        self.assertEqual(space_out_camel_case('AbCd'), 'Ab Cd')
  22
+        self.assertEqual(space_out_camel_case('TypicalCamelCase'), 'Typical Camel Case')
  23
+        self.assertEqual(space_out_camel_case('DMLSServicesOtherBSTextLLC'), 'DMLS Services Other BS Text LLC')
  24
+
  25
+
11 26
 class TestTemplateTags(TestCase):
12 27
     def setUp(self):
13 28
         self.user = User.objects.create(username="test")
30  servee/utils.py
... ...
@@ -1,20 +1,16 @@
1  
-import re
2  
-
3  
-def space_out_camel_case(stringAsCamelCase):
  1
+def space_out_camel_case(camel):
4 2
     """
5  
-    @@TODO white-box implementation of this function from somebody who hasn't seen the code.
6  
-    
7  
-    Adds spaces to a camel case string.  Failure to space out string returns the original string.
8  
-    >>> space_out_camel_case('DMLSServicesOtherBSTextLLC')
9  
-    'DMLS Services Other BS Text LLC'
10  
-    
11  
-    Originally by Simon Hartley 12/27/2008, tbh, I'm not sure of the licensing presumptions of
12  
-    a site like the below...which means I probably don't have the right to use this.
13  
-    http://refactormycode.com/codes/675-camelcase-to-camel-case-python-newbie#refactor_139268
  3
+    Converts a "CamelCasedName" to "Camel Case Name".
14 4
     """
15  
-    
16  
-    if stringAsCamelCase is None:
17  
-        return None
  5
+    chars = []
  6
+
  7
+    for char in camel:
  8
+        if len(chars) >= 2 and chars[-1] != ' ':
  9
+            if char.isupper() and chars[-1].islower():
  10
+                chars.append(' ')
  11
+            elif char.islower() and chars[-1].isupper() and chars[-2].isupper():
  12
+                chars.insert(len(chars) - 1, ' ')
  13
+
  14
+        chars.append(char)
18 15
 
19  
-    pattern = re.compile('([A-Z][A-Z][a-z])|([a-z][A-Z])')
20  
-    return pattern.sub(lambda m: m.group()[:1] + " " + m.group()[1:], stringAsCamelCase)
  16
+    return ''.join(chars)

0 notes on commit efd5984

Please sign in to comment.
Something went wrong with that request. Please try again.