diff --git a/snippets/camel.md b/snippets/camel.md index 01dbf1181..4e140054d 100644 --- a/snippets/camel.md +++ b/snippets/camel.md @@ -10,16 +10,16 @@ Use `title()` to capitalize the first letter of each word convert the rest to lo Finally, use `replace()` to remove spaces between words. ```py -import re +from re import sub def camel(s): - s = re.sub(r"(_|-)+", " ", s).title().replace(" ", "") + s = sub(r"(_|-)+", " ", s).title().replace(" ", "") return s[0].lower() + s[1:] ``` ```py -camel('some_database_field_name'); # 'someDatabaseFieldName' -camel('Some label that needs to be camelized'); # 'someLabelThatNeedsToBeCamelized' -camel('some-javascript-property'); # 'someJavascriptProperty' -camel('some-mixed_string with spaces_underscores-and-hyphens'); # 'someMixedStringWithSpacesUnderscoresAndHyphens' +camel('some_database_field_name') # 'someDatabaseFieldName' +camel('Some label that needs to be camelized') # 'someLabelThatNeedsToBeCamelized' +camel('some-javascript-property') # 'someJavascriptProperty' +camel('some-mixed_string with spaces_underscores-and-hyphens') # 'someMixedStringWithSpacesUnderscoresAndHyphens' ``` diff --git a/snippets/kebab.md b/snippets/kebab.md index f4fc7c2e7..a83829bce 100644 --- a/snippets/kebab.md +++ b/snippets/kebab.md @@ -8,18 +8,19 @@ Converts a string to kebab case. Break the string into words and combine them adding `-` as a separator, using a regexp. ```py -import re +from re import sub def kebab(s): - return re.sub(r"(\s|_|-)+","-", - re.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) - ) + return 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)) ``` ```py -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" +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" ```