|
@@ -5,31 +5,47 @@
|
|
|
#"the-stealth-warrior" gets converted to "theStealthWarrior"
|
|
|
#"The_Stealth_Warrior" gets converted to "TheStealthWarrior"
|
|
|
|
|
|
+
|
|
|
+# 'if s else s' - 'защита от пустой строки'
|
|
|
+# title - преобразует символы в верхний регистр
|
|
|
+# translate - заменяет символы
|
|
|
def to_camel_case(s):
|
|
|
- return s[0] + s.title().translate(None, "-_")[1:] if s else s
|
|
|
+ #return s[0] + s.title().translate(None, "-_")[1:] if s else s # Так не работает
|
|
|
+ return s[0] + s.title().translate({ord('_'):None, ord('-'):None})[1:] if s else s
|
|
|
|
|
|
|
|
|
+'''
|
|
|
+# replace - замена
|
|
|
+# Интересный for по списку и join
|
|
|
def to_camel_case(text):
|
|
|
removed = text.replace('-', ' ').replace('_', ' ').split()
|
|
|
if len(removed) == 0:
|
|
|
return ''
|
|
|
return removed[0]+ ''.join([x.capitalize() for x in removed[1:]])
|
|
|
+'''
|
|
|
|
|
|
-
|
|
|
+'''
|
|
|
+# Как и в первой функции только без защиты от пустой строки
|
|
|
def to_camel_case(text):
|
|
|
return text[:1] + text.title()[1:].replace('_', '').replace('-', '')
|
|
|
+'''
|
|
|
|
|
|
-
|
|
|
+'''
|
|
|
+# Разобраться с регулярками
|
|
|
import re
|
|
|
def to_camel_case(text):
|
|
|
return re.sub('[_-](.)', lambda x: x.group(1).upper(), text)
|
|
|
+'''
|
|
|
|
|
|
-
|
|
|
+'''
|
|
|
+# Микс из первого и второго варианта
|
|
|
def to_camel_case(text):
|
|
|
words = text.replace('_', '-').split('-')
|
|
|
return words[0] + ''.join([x.title() for x in words[1:]])
|
|
|
+'''
|
|
|
|
|
|
-
|
|
|
+'''
|
|
|
+# Перебор по символу
|
|
|
def to_camel_case(text):
|
|
|
cap = False
|
|
|
newText = ''
|
|
@@ -43,8 +59,13 @@ def to_camel_case(text):
|
|
|
newText = newText + t
|
|
|
cap = False
|
|
|
return newText
|
|
|
+'''
|
|
|
|
|
|
-
|
|
|
+'''
|
|
|
+# Мозгодробительный вариант
|
|
|
def to_camel_case(text):
|
|
|
return "".join([i if n==0 else i.capitalize() for n,i in enumerate(text.replace("-","_").split("_"))])
|
|
|
+'''
|
|
|
|
|
|
+print(to_camel_case("the-stealth-warrior"))
|
|
|
+print(to_camel_case("The_Stealth_Warrior"))
|