Created
October 7, 2020 04:25
-
-
Save tak-bro/6515fdf6bd90d0ed24dac902d5c6d71d to your computer and use it in GitHub Desktop.
[JS] convert string to kebab case
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: 30 Seconds of Knowledge | |
| const toKebabCase = str => | |
| str && | |
| str | |
| .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) | |
| .map(x => x.toLowerCase()) | |
| .join('-'); | |
| toKebabCase('camelCase'); // 'camel-case' | |
| toKebabCase('some text'); // 'some-text' | |
| toKebabCase('some-mixed_string With spaces_underscores-and-hyphens'); // 'some-mixed-string-with-spaces-underscores-and-hyphens' | |
| toKebabCase('AllThe-small Things'); // "all-the-small-things" | |
| toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'); // "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment