-
-
Notifications
You must be signed in to change notification settings - Fork 257
Don't depend on stringcase (closes #369) #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
797b76f
26ac759
6e8166c
3aacf5e
4990254
f5fd5e4
000c3d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,20 @@ | ||
| import builtins | ||
| import re | ||
| from keyword import iskeyword | ||
| from typing import List | ||
|
|
||
| import stringcase | ||
| delimiters = " _-" | ||
|
|
||
|
|
||
| def sanitize(value: str) -> str: | ||
| """ Removes every character that isn't 0-9, A-Z, a-z, ' ', -, or _ """ | ||
| return re.sub(r"[^\w _\-]+", "", value) | ||
| """ Removes every character that isn't 0-9, A-Z, a-z, or a known delimiter """ | ||
| return re.sub(rf"[^\w{delimiters}]+", "", value) | ||
|
|
||
|
|
||
| def split_words(value: str) -> List[str]: | ||
| """ Split a string on non-capital letters and known delimiters """ | ||
| value = " ".join(re.split("([A-Z]?[a-z0-9]+)", value)) | ||
| return re.findall(rf"[^{delimiters}]+", value) | ||
|
|
||
|
|
||
| def fix_keywords(value: str) -> str: | ||
|
|
@@ -25,22 +32,23 @@ def fix_reserved_words(value: str) -> str: | |
| return value | ||
|
|
||
|
|
||
| def group_title(value: str) -> str: | ||
| value = re.sub(r"([A-Z]{2,})([A-Z][a-z]|[ \-_]|$)", lambda m: m.group(1).title() + m.group(2), value.strip()) | ||
| value = re.sub(r"(^|[ _-])([A-Z])", lambda m: m.group(1) + m.group(2).lower(), value) | ||
| return value | ||
|
|
||
|
|
||
| def snake_case(value: str) -> str: | ||
| return fix_keywords(stringcase.snakecase(group_title(sanitize(value)))) | ||
| words = split_words(sanitize(value)) | ||
| value = "_".join(words).lower() | ||
| return fix_keywords(value) | ||
|
|
||
|
|
||
| def pascal_case(value: str) -> str: | ||
| return fix_keywords(stringcase.pascalcase(sanitize(value.replace(" ", "")))) | ||
| words = split_words(sanitize(value)) | ||
| words = [word.capitalize() if not word.isupper() else word for word in words] | ||
ramnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| value = "".join(words) | ||
|
||
| return fix_keywords(value) | ||
|
|
||
|
|
||
| def kebab_case(value: str) -> str: | ||
| return fix_keywords(stringcase.spinalcase(group_title(sanitize(value)))) | ||
| words = split_words(sanitize(value)) | ||
| value = "-".join(words).lower() | ||
| return fix_keywords(value) | ||
|
|
||
|
|
||
| def remove_string_escapes(value: str) -> str: | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.