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.
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
# 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: 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].lower() + ''.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 = 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))
from CaseConverter import Convert, Cases
# 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(["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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment