Skip to content

Instantly share code, notes, and snippets.

@entirelymagic
Last active December 7, 2021 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entirelymagic/0e65ff3eb9fa478c102d7ff697207081 to your computer and use it in GitHub Desktop.
Save entirelymagic/0e65ff3eb9fa478c102d7ff697207081 to your computer and use it in GitHub Desktop.
text-to-kebab
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