2021-03-10 22:11:05 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class String:
|
2021-03-14 16:06:23 +01:00
|
|
|
"""
|
|
|
|
Useful functions for strings
|
|
|
|
"""
|
2021-03-10 22:11:05 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def convert_to_snake_case(name: str) -> str:
|
2021-03-14 16:06:23 +01:00
|
|
|
"""
|
|
|
|
Converts string to snake case
|
|
|
|
:param name:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-04-10 11:01:56 +02:00
|
|
|
# convert to train-case to CamelCase
|
|
|
|
if '-' in name:
|
|
|
|
name = ''.join(word.title() for word in name.split('-'))
|
|
|
|
|
2021-03-10 22:11:05 +01:00
|
|
|
pattern1 = re.compile(r'(.)([A-Z][a-z]+)')
|
|
|
|
pattern2 = re.compile(r'([a-z0-9])([A-Z])')
|
|
|
|
file_name = re.sub(pattern1, r'\1_\2', name)
|
2021-04-10 11:01:56 +02:00
|
|
|
return re.sub(pattern2, r'\1_\2', file_name).lower()
|
2021-03-12 15:32:37 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def first_to_upper(string: str) -> str:
|
2021-03-14 16:06:23 +01:00
|
|
|
"""
|
|
|
|
Converts first char to upper
|
|
|
|
:param string:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-12 15:32:37 +01:00
|
|
|
return f'{string[0].upper()}{string[1:]}'
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def first_to_lower(string: str) -> str:
|
2021-03-14 16:06:23 +01:00
|
|
|
"""
|
|
|
|
Converts first char to lower
|
|
|
|
:param string:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-12 15:32:37 +01:00
|
|
|
return f'{string[0].lower()}{string[1:]}'
|