Last active
December 7, 2021 09:32
-
-
Save entirelymagic/0e65ff3eb9fa478c102d7ff697207081 to your computer and use it in GitHub Desktop.
text-to-kebab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from re import sub | |
def kebab(s): | |
return '-'.join( | |
sub(r"(\s|_|-)+"," ", | |
sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+", | |
lambda mo: ' ' + mo.group(0).lower(), s)).split()) | |
# Examples: | |
kebab('camelCase') # 'camel-case' | |
kebab('some text') # 'some-text' | |
kebab('some-mixed_string With spaces_underscores-and-hyphens') | |
# 'some-mixed-string-with-spaces-underscores-and-hyphens' | |
kebab('AllThe-small Things') # 'all-the-small-things' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment