A lightweight utility library for working with various string casing.
| Example | Case Names |
|---|---|
| HelloWorld | Pascal Case Capital Camel Case |
| helloWorld | Camel Case |
| helloworld | Flat Case Mumble Case |
| HELLOWORLD | Upper Flat Case |
| Hello World | Title Case |
| Hello world | Sentence Case |
| hello world | Lower Case |
| HELLO WORLD | Upper Case |
| hello_world | Snake Case C Case |
| HELLO_WORLD | Screaming Snake Case Constant Case Macro Case |
| Hello-World | Train Case |
| hello-world | Kebab Case Caterpillar Case CSS Case Lisp Case |
| HELLO-WORLD | COBOL Case |
Each of the above may be accessed within the Case Enum
from case_util import CaseThe Case Enum contains functions to format a str
Case.SNAKE_CASE.format("Hello World") # output = hello_worldand also to determine if a str is said format
Case.CAMEL_CASE.is_formatted("MyClassName") # output = False
Case.PASCAL_CASE.is_formatted("MyClassName") # output = TrueIn addition, a class method is available to detect the format of a str
Case.detect_format("my-test-string") # output = Case.KEBAB_CASEWhile not intended to be used externally, the following functions are available if they are of use.
import case_util
words = case_util.words_of("MyFormattedClassName")
# output = ["My", "Formatted", "Class", "Name"]If you already have a list of words, you may preform the following actions on each
case_util.capitalize(words)case_util.camel(words)- Capitalizes all but the 1st wordcase_util.sentence(words)- Capitalizes only the 1st wordcase_util.lower(words)case_util.upper(words)
Due to the nature of the various string formats, the correct case cannot always be detected.
A single word such as name will be detected as Flat Case. Similarly, NAME will be Upper Flat Case.
While this functionality may seem obvious, it gets more nuanced.
Perhaps the author wrote it in Camel Case or Snake Case which becomes clear when expanded: nameClass or var_name.
Without input from the original author, it is impossible to know for certain.
Acronyms/abbreviations are treated as a single word. So MyAPIClass converted to snake case would be my_api_class.
Related, acronym case is not preserved which means MyAPIClass converted to camel case would result in myApiClass.
This unfortunately means that the format of such strings will not be properly detected.
Some strings, such as the quick_brown-fox could be one of many formats depending on the intended delimiter.
In these scenarios, None is returned
This library was created with class/variable names in mind. Therefore, extensive test has not been conducted using special characters. When it comes to Camel Case and Pascal Case, numbers are designed to be seen as uppercase. This may result in unwanted behavior if placing numbers within the middle of words. All other special characters are intended to be treated as lowercase.
No design/testing has been conducted for characters outside of ASCII.