Skip to content

Instantly share code, notes, and snippets.

@sovietscout
Last active December 31, 2023 14:17
Show Gist options
  • Select an option

  • Save sovietscout/ff1d2bf4a3ecb941eef26d1208fc7429 to your computer and use it in GitHub Desktop.

Select an option

Save sovietscout/ff1d2bf4a3ecb941eef26d1208fc7429 to your computer and use it in GitHub Desktop.

Revisions

  1. sovietscout revised this gist Dec 31, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ def __init__(self, text: str, case: Cases, delimiter: str = ' ', preserve_punctu
    super().__init__(wordList)

    class Convert(str):
    def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: Optional[str] = ' ') -> str:
    if isinstance(text, str):
    text = FromCase(text, Cases.Plain)

  2. sovietscout revised this gist Apr 19, 2022. 2 changed files with 19 additions and 13 deletions.
    19 changes: 11 additions & 8 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Macro = 5
    Cobol = 6
    Pascal = 7
    Custom = 8
    Plain = 9
    Plain = 8
    Custom = 9

    # Aliases
    Caterpillar = 3
    @@ -21,15 +21,17 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Dash = 3
    Lisp = 3
    Css = 3
    CapitalCamel = 7
    C = 2
    Lower = 2
    Upper = 5
    Train = 6
    CapitalCamel = 7
    Sentence = 8

    class FromCase(list):
    def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]:
    normalizedText = text.translate(str.maketrans('', '', string.punctuation.replace(delimiter, '')))
    def __init__(self, text: str, case: Cases, delimiter: str = ' ', preserve_punctuation: bool = False) -> List[str]:
    removables = string.punctuation if not preserve_punctuation else ''
    normalizedText = text.translate(str.maketrans('', '', removables.replace(delimiter, '')))

    if case in ([Cases.Camel, Cases.Pascal]):
    wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedText)
    @@ -44,13 +46,14 @@ def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]:
    else:
    raise Exception("Case not supported")

    return super().__init__(wordList)
    super().__init__(wordList)

    class Convert(str):
    def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    if isinstance(text, str):
    text = FromCase(text, Cases.Plain)

    # Lowercases string, replaces spaces with nothing, removes element if it is empty
    words = [word for word in [word.lower().replace(' ', '') for word in text] if word]

    if case is Cases.Camel:
    @@ -66,9 +69,9 @@ def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Came
    elif case is Cases.Cobol:
    convertedStr = '-'.join(words).upper()
    elif case is Cases.Pascal:
    convertedStr = ''.join([word.capitalize() for word in words])
    convertedStr = ''.join(words).title()
    elif case is Cases.Plain:
    convertedStr = ' '.join(words)
    convertedStr = words[0].capitalize() + ' ' + ' '.join(words[1:])
    elif case is Cases.Custom:
    convertedStr = delimiter.join(words)
    else:
    13 changes: 8 additions & 5 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,19 @@
    from CaseConverter import Convert, Cases, FromCase

    # Convert to snake case
    # Convert a sentence to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

    # Supports other languages too!
    Convert("Конец близок", Cases.Kebab) # конец-близок

    # Convert a list of words to camel case
    Convert(["It", "was", "all", "a", "mistake"], Cases.Camel) # itWasAllAMistake
    Convert(["The", "fog", "is", "coming"], Cases.Camel) # theFogIsComing

    # Convert to a custom case (delimiter required)
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimiter="|") # what|do|the|numbers|mean|mason

    # Convert from one case to another
    Convert(FromCase("ThisIsDefinitelyNotFun", Cases.Pascal), Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN
    Convert(FromCase("ItWasAllAMistake", Cases.Pascal), Cases.Cobol) # IT-WAS-ALL-A-MISTAKE

    # Custom cases (delimiters necessary)
    Convert(FromCase("the,fog,is,coming", Cases.Custom, ','), Cases.Custom, '^') # the^fog^is^coming
    # Convert from a custom case (delimiter necessary) to another case
    Convert(FromCase("ratio+L+cope", Cases.Custom, '+'), Cases.Custom, '~') # ratio~l~cope
  3. sovietscout revised this gist Apr 8, 2022. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Came
    if isinstance(text, str):
    text = FromCase(text, Cases.Plain)

    words = [word for word in [word.replace(' ', '') for word in text] if word]
    words = [word for word in [word.lower().replace(' ', '') for word in text] if word]

    if case is Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
    2 changes: 1 addition & 1 deletion test.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # Convert to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

    # Conve a list of words to camel case
    # Convert a list of words to camel case
    Convert(["It", "was", "all", "a", "mistake"], Cases.Camel) # itWasAllAMistake

    # Convert to a custom case (delimiter required)
  4. sovietscout revised this gist Apr 7, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -47,11 +47,11 @@ def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]:
    return super().__init__(wordList)

    class Convert(str):
    def __new__(cls, text: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    if isinstance(text, str):
    text = FromCase(text, Cases.Plain)

    words = [word.lower().replace(' ', '') for word in text]
    words = [word for word in [word.replace(' ', '') for word in text] if word]

    if case is Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
  5. sovietscout revised this gist Apr 7, 2022. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -28,30 +28,30 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Train = 6

    class FromCase(list):
    def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ') -> List[str]:
    normalizedQuery = query.translate(str.maketrans('', '', string.punctuation.replace(delimiter, '')))
    def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]:
    normalizedText = text.translate(str.maketrans('', '', string.punctuation.replace(delimiter, '')))

    if case in ([Cases.Camel, Cases.Pascal]):
    wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery)
    wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedText)
    elif case in ([Cases.Snake, Cases.Macro]):
    wordList = normalizedQuery.split('_')
    wordList = normalizedText.split('_')
    elif case in ([Cases.Kebab, Cases.Cobol]):
    wordList = normalizedQuery.split('-')
    wordList = normalizedText.split('-')
    elif case is Cases.Plain:
    wordList = normalizedQuery.split()
    wordList = normalizedText.split()
    elif case is Cases.Custom:
    wordList = normalizedQuery.split(delimiter)
    wordList = normalizedText.split(delimiter)
    else:
    raise Exception("Case not supported")

    return super().__init__(wordList)

    class Convert(str):
    def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    if isinstance(query, str):
    query = FromCase(query, Cases.Plain)
    def __new__(cls, text: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    if isinstance(text, str):
    text = FromCase(text, Cases.Plain)

    words = [word.lower() for word in query]
    words = [word.lower().replace(' ', '') for word in text]

    if case is Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
  6. sovietscout revised this gist Apr 7, 2022. 2 changed files with 2 additions and 5 deletions.
    6 changes: 1 addition & 5 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    from typing import List, Union

    class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Plain = 10
    Camel = 1
    Snake = 2
    Kebab = 3
    @@ -13,6 +12,7 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Cobol = 6
    Pascal = 7
    Custom = 8
    Plain = 9

    # Aliases
    Caterpillar = 3
    @@ -31,8 +31,6 @@ class FromCase(list):
    def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ') -> List[str]:
    normalizedQuery = query.translate(str.maketrans('', '', string.punctuation.replace(delimiter, '')))

    wordList = []

    if case in ([Cases.Camel, Cases.Pascal]):
    wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery)
    elif case in ([Cases.Snake, Cases.Macro]):
    @@ -55,8 +53,6 @@ def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Ca

    words = [word.lower() for word in query]

    convertedStr: str = ''

    if case is Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
    elif case is Cases.Snake:
    1 change: 1 addition & 0 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    from CaseConverter import Convert, Cases, FromCase

    # Convert to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

  7. sovietscout revised this gist Apr 7, 2022. 2 changed files with 6 additions and 7 deletions.
    10 changes: 5 additions & 5 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import re
    import string
    from enum import Enum
    from typing import List, Optional, Union
    from typing import List, Union

    class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Plain = 10
    @@ -48,14 +48,14 @@ def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ')

    return super().__init__(wordList)

    class Convert:
    def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: Optional[str] = None) -> str:
    class Convert(str):
    def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: str = ' ') -> str:
    if isinstance(query, str):
    query = FromCase(query, Cases.Plain)

    words = [word.lower() for word in query]

    convertedStr = ""
    convertedStr: str = ''

    if case is Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
    @@ -78,4 +78,4 @@ def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Ca
    else:
    raise Exception("Case not recognised")

    return convertedStr
    return super().__new__(cls, convertedStr)
    3 changes: 1 addition & 2 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    from CaseConverter import Convert, Cases, FromCase

    # Convert to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

    @@ -13,4 +12,4 @@
    Convert(FromCase("ThisIsDefinitelyNotFun", Cases.Pascal), Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN

    # Custom cases (delimiters necessary)
    Convert(FromCase("the,fog,is,coming", Cases.Custom, ','), Cases.Custom, '^') # the^fog^is^coming
    Convert(FromCase("the,fog,is,coming", Cases.Custom, ','), Cases.Custom, '^') # the^fog^is^coming
  8. sovietscout revised this gist Apr 7, 2022. 2 changed files with 44 additions and 41 deletions.
    76 changes: 38 additions & 38 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    import re
    import string
    from enum import Enum
    from typing import List, Union
    from typing import List, Optional, Union

    class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Plain = 10
    Camel = 1
    Snake = 2
    Kebab = 3
    @@ -26,56 +27,55 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Upper = 5
    Train = 6

    class FromCase(list):
    def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ') -> List[str]:
    normalizedQuery = query.translate(str.maketrans('', '', string.punctuation.replace(delimiter, '')))

    wordList = []

    if case in ([Cases.Camel, Cases.Pascal]):
    wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery)
    elif case in ([Cases.Snake, Cases.Macro]):
    wordList = normalizedQuery.split('_')
    elif case in ([Cases.Kebab, Cases.Cobol]):
    wordList = normalizedQuery.split('-')
    elif case is Cases.Plain:
    wordList = normalizedQuery.split()
    elif case is Cases.Custom:
    wordList = normalizedQuery.split(delimiter)
    else:
    raise Exception("Case not supported")

    return super().__init__(wordList)

    class Convert:
    def __new__(cls, query: Union[str, List[str]], case: Cases = Cases.Camel, delimiter: str = '') -> str:
    def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: Optional[str] = None) -> str:
    if isinstance(query, str):
    words = cls.__normalizeString(query).lower().split()
    elif isinstance(query, list):
    words = query
    query = FromCase(query, Cases.Plain)

    words = [word.lower() for word in query]

    convertedStr = ""

    if case == Cases.Camel:
    convertedStr = words[0].lower() + ''.join([word.capitalize() for word in words[1:]])
    elif case == Cases.Snake:
    if case is Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
    elif case is Cases.Snake:
    convertedStr = '_'.join(words)
    elif case == Cases.Kebab:
    elif case is Cases.Kebab:
    convertedStr = '-'.join(words)
    elif case == Cases.Flat:
    elif case is Cases.Flat:
    convertedStr = ''.join(words)
    elif case == Cases.Macro:
    elif case is Cases.Macro:
    convertedStr = '_'.join(words).upper()
    elif case == Cases.Cobol:
    elif case is Cases.Cobol:
    convertedStr = '-'.join(words).upper()
    elif case == Cases.Pascal:
    elif case is Cases.Pascal:
    convertedStr = ''.join([word.capitalize() for word in words])
    elif case == Cases.Custom:
    elif case is Cases.Plain:
    convertedStr = ' '.join(words)
    elif case is Cases.Custom:
    convertedStr = delimiter.join(words)
    else:
    raise Exception("Case not recognised")

    return convertedStr


    @classmethod
    def fromCase(cls, query: str, fromCase: Cases, toCase: Cases, delimiter: str = ''):
    normalizedQuery = cls.__normalizeString(query)

    normalizedList = []

    if fromCase == Cases.Camel or Cases.Pascal:
    normalizedList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery)
    elif fromCase == Cases.Snake or Cases.Macro:
    normalizedList = normalizedQuery.split('_')
    elif fromCase == Cases.Kebab or Cases.Cobol:
    normalizedList = normalizedQuery.split('-')
    elif fromCase == Cases.Custom:
    raise Exception("Custom case not supported")
    else:
    normalizedList = normalizedQuery.split()

    return cls(normalizedList, toCase, delimiter)

    @staticmethod
    def __normalizeString(query: str) -> str:
    return query.translate(str.maketrans('', '', string.punctuation))
    9 changes: 6 additions & 3 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    from CaseConverter import Convert, Cases
    from CaseConverter import Convert, Cases, FromCase

    # Convert to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help
    @@ -9,5 +9,8 @@
    # Convert to a custom case (delimiter required)
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimiter="|") # what|do|the|numbers|mean|mason

    # Convert from one case to another (from custom case type not supported)
    Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN
    # Convert from one case to another
    Convert(FromCase("ThisIsDefinitelyNotFun", Cases.Pascal), Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN

    # Custom cases (delimiters necessary)
    Convert(FromCase("the,fog,is,coming", Cases.Custom, ','), Cases.Custom, '^') # the^fog^is^coming
  9. sovietscout revised this gist Apr 7, 2022. 2 changed files with 20 additions and 10 deletions.
    21 changes: 14 additions & 7 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import re
    import string
    from enum import Enum
    from typing import List, Union

    class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Camel = 1
    @@ -26,12 +27,16 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Train = 6

    class Convert:
    def __new__(cls, query: str, case: Cases = Cases.Camel, delimeter: str = '') -> str:
    words = cls.__normalizeString(query).lower().split()
    def __new__(cls, query: Union[str, List[str]], case: Cases = Cases.Camel, delimiter: str = '') -> str:
    if isinstance(query, str):
    words = cls.__normalizeString(query).lower().split()
    elif isinstance(query, list):
    words = query

    convertedStr = ""

    if case == Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
    convertedStr = words[0].lower() + ''.join([word.capitalize() for word in words[1:]])
    elif case == Cases.Snake:
    convertedStr = '_'.join(words)
    elif case == Cases.Kebab:
    @@ -45,15 +50,15 @@ def __new__(cls, query: str, case: Cases = Cases.Camel, delimeter: str = '') ->
    elif case == Cases.Pascal:
    convertedStr = ''.join([word.capitalize() for word in words])
    elif case == Cases.Custom:
    convertedStr = delimeter.join(words)
    convertedStr = delimiter.join(words)
    else:
    raise Exception("Case not recognised")

    return convertedStr


    @classmethod
    def fromCase(cls, query: str, fromCase: Cases, toCase: Cases):
    def fromCase(cls, query: str, fromCase: Cases, toCase: Cases, delimiter: str = ''):
    normalizedQuery = cls.__normalizeString(query)

    normalizedList = []
    @@ -64,11 +69,13 @@ def fromCase(cls, query: str, fromCase: Cases, toCase: Cases):
    normalizedList = normalizedQuery.split('_')
    elif fromCase == Cases.Kebab or Cases.Cobol:
    normalizedList = normalizedQuery.split('-')
    elif fromCase == Cases.Custom:
    raise Exception("Custom case not supported")
    else:
    normalizedList = normalizedQuery.split()

    actualQuery = ' '.join(normalizedList)
    return cls(actualQuery, toCase)
    return cls(normalizedList, toCase, delimiter)

    @staticmethod
    def __normalizeString(query: str) -> str:
    return query.translate(str.maketrans('', '', string.punctuation))
    9 changes: 6 additions & 3 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,11 @@
    # Convert to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

    # Convert to a custom case (delimeter required)
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason
    # Conve a list of words to camel case
    Convert(["It", "was", "all", "a", "mistake"], Cases.Camel) # itWasAllAMistake

    # Convert to a custom case (delimiter required)
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimiter="|") # what|do|the|numbers|mean|mason

    # Convert from one case to another (from custom case type not supported)
    Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN
    Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN
  10. sovietscout revised this gist Apr 7, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.py
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason

    # Convert from one case to another (from custom case type not supported)
    Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # IT-IS-DEFINITELY-NOT-FUN
    Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN
  11. sovietscout revised this gist Apr 6, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    from CaseConverter import Convert, Cases

    # Convert to snake
    # Convert to snake case
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

    # Convert to custom type (delimeter required)
    # Convert to a custom case (delimeter required)
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason

    # Convert from one case to another (from custom case type not supported)
  12. sovietscout created this gist Apr 6, 2022.
    74 changes: 74 additions & 0 deletions CaseConverter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    import re
    import string
    from enum import Enum

    class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161
    Camel = 1
    Snake = 2
    Kebab = 3
    Flat = 4
    Macro = 5
    Cobol = 6
    Pascal = 7
    Custom = 8

    # Aliases
    Caterpillar = 3
    Hyphen = 3
    Spinal = 3
    Dash = 3
    Lisp = 3
    Css = 3
    CapitalCamel = 7
    C = 2
    Lower = 2
    Upper = 5
    Train = 6

    class Convert:
    def __new__(cls, query: str, case: Cases = Cases.Camel, delimeter: str = '') -> str:
    words = cls.__normalizeString(query).lower().split()
    convertedStr = ""

    if case == Cases.Camel:
    convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]])
    elif case == Cases.Snake:
    convertedStr = '_'.join(words)
    elif case == Cases.Kebab:
    convertedStr = '-'.join(words)
    elif case == Cases.Flat:
    convertedStr = ''.join(words)
    elif case == Cases.Macro:
    convertedStr = '_'.join(words).upper()
    elif case == Cases.Cobol:
    convertedStr = '-'.join(words).upper()
    elif case == Cases.Pascal:
    convertedStr = ''.join([word.capitalize() for word in words])
    elif case == Cases.Custom:
    convertedStr = delimeter.join(words)
    else:
    raise Exception("Case not recognised")

    return convertedStr


    @classmethod
    def fromCase(cls, query: str, fromCase: Cases, toCase: Cases):
    normalizedQuery = cls.__normalizeString(query)

    normalizedList = []

    if fromCase == Cases.Camel or Cases.Pascal:
    normalizedList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery)
    elif fromCase == Cases.Snake or Cases.Macro:
    normalizedList = normalizedQuery.split('_')
    elif fromCase == Cases.Kebab or Cases.Cobol:
    normalizedList = normalizedQuery.split('-')
    else:
    normalizedList = normalizedQuery.split()

    actualQuery = ' '.join(normalizedList)
    return cls(actualQuery, toCase)

    def __normalizeString(query: str) -> str:
    return query.translate(str.maketrans('', '', string.punctuation))
    10 changes: 10 additions & 0 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    from CaseConverter import Convert, Cases

    # Convert to snake
    Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help

    # Convert to custom type (delimeter required)
    Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason

    # Convert from one case to another (from custom case type not supported)
    Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # IT-IS-DEFINITELY-NOT-FUN