Detect and convert string cases
Project description
Str Case Util
A lightweight utility library for working with various string casing.
Supported Cases
| 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 |
Features
Each of the above may be accessed within the Case Enum
from case_util import Case
The Case Enum contains functions to format a str
Case.SNAKE_CASE.format("Hello World") # output = hello_world
and also to determine if a str is said format
Case.CAMEL_CASE.is_formatted("MyClassName") # output = False
Case.PASCAL_CASE.is_formatted("MyClassName") # output = True
In addition, a class method is available to detect the format of a str
Case.detect_format("my-test-string") # output = Case.KEBAB_CASE
Additional Functionality
While not intended to be used externally, the following functions are available if they are of use.
Split String Into Words
import case_util
words = case_util.words_of("MyFormattedClassName")
# output = ["My", "Formatted", "Class", "Name"]
Word List Manipulation
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)
Caveats
Due to the nature of the various string formats, the correct case cannot always be detected.
Flat Case Trumps All
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
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.
Multiple Cases 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
Special Characters
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.
Additional Language Support
No design/testing has been conducted for characters outside of ASCII.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file str_case_util-1.0.0.tar.gz.
File metadata
- Download URL: str_case_util-1.0.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.24.1 CPython/3.13.3 Linux/6.11.0-1012-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16c5149764434eda39bb0c9dcd3d1a3b3fdfadcd7741cf4496ff1aa60acc989e
|
|
| MD5 |
ebcba51852bcea9cb1d3c2a0acbabbb4
|
|
| BLAKE2b-256 |
f4e27698c6756cdd04daf7406fcf017ef14e8a47194f7fdfc5222472b63c1a1d
|
File details
Details for the file str_case_util-1.0.0-py2.py3-none-any.whl.
File metadata
- Download URL: str_case_util-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.24.1 CPython/3.13.3 Linux/6.11.0-1012-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
788286a9551d58bb068e412d849701518eba5fcd4267a5f109eaf014faf642dd
|
|
| MD5 |
9be0c6ed035cd4edd3991f2094feff34
|
|
| BLAKE2b-256 |
b09f11c4650f4859999fedf3f7c0cb068ae763f978195419ca913c3b20e0998d
|