-
-
Notifications
You must be signed in to change notification settings - Fork 246
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.
Show resolved
Hide resolved
|
||
value = "".join(words) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A side-effect of this is that a leading delimiter gets stripped. So _MyClass and __MyClass both become MyClass. This may not be a problem, but it is a breaking change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I wonder if we should stay consistent or not. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After thinking about it a bit, I think it's better to remove the leading underscores since that has the semantics in Python of being private. Nothing in the generated interfaces should be private so... this new behavior seems better. |
||
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.