Last active
December 31, 2023 14:17
-
-
Save sovietscout/ff1d2bf4a3ecb941eef26d1208fc7429 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import string | |
| from enum import Enum | |
| from typing import List, Union | |
| 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 | |
| Plain = 9 | |
| # Aliases | |
| Caterpillar = 3 | |
| Hyphen = 3 | |
| Spinal = 3 | |
| Dash = 3 | |
| Lisp = 3 | |
| Css = 3 | |
| CapitalCamel = 7 | |
| C = 2 | |
| Lower = 2 | |
| Upper = 5 | |
| Train = 6 | |
| class FromCase(list): | |
| 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]*', normalizedText) | |
| elif case in ([Cases.Snake, Cases.Macro]): | |
| wordList = normalizedText.split('_') | |
| elif case in ([Cases.Kebab, Cases.Cobol]): | |
| wordList = normalizedText.split('-') | |
| elif case is Cases.Plain: | |
| wordList = normalizedText.split() | |
| elif case is Cases.Custom: | |
| wordList = normalizedText.split(delimiter) | |
| else: | |
| raise Exception("Case not supported") | |
| return 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) | |
| 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:]]) | |
| elif case is Cases.Snake: | |
| convertedStr = '_'.join(words) | |
| elif case is Cases.Kebab: | |
| convertedStr = '-'.join(words) | |
| elif case is Cases.Flat: | |
| convertedStr = ''.join(words) | |
| elif case is Cases.Macro: | |
| convertedStr = '_'.join(words).upper() | |
| elif case is Cases.Cobol: | |
| convertedStr = '-'.join(words).upper() | |
| elif case is Cases.Pascal: | |
| convertedStr = ''.join([word.capitalize() for word in words]) | |
| elif case is Cases.Plain: | |
| convertedStr = ' '.join(words) | |
| elif case is Cases.Custom: | |
| convertedStr = delimiter.join(words) | |
| else: | |
| raise Exception("Case not recognised") | |
| return super().__new__(cls, convertedStr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| # Convert 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 | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment