Created
September 21, 2016 10:11
-
-
Save thevangelist/8ff91bac947018c9f3bfaad6487fa149 to your computer and use it in GitHub Desktop.
JS: convert to kebab-case
This file contains 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
const convertToKebabCase = (string) => { | |
return string.replace(/\s+/g, '-').toLowerCase(); | |
} |
Previously:
Som3thing\3lse => som3-thing-3-lse
My version:Som3thing\3lse => som3thing-3lse
Sorry @tehpsalmist
kebabCase('ABC_123') -> 'ab-c-123'
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('-');
Sorry @Vadorequest
toKebabCase('FOO-BAR') -> ''f-o-o-bar''
str .replace(/([A-Z])([A-Z])/g, '$1-$2') .replace(/([a-z])([A-Z])/g, '$1-$2') .replace(/[\s_]+/g, '-') .toLowerCase()
Sorry @GerardRodes
'FOO-BAR' -> ''f-oo-b-ar''
@oravecz That's a shame! Do you think you have a fix handy?
https://lodash.com/docs/4.17.15#kebabCase I don't use any other nowadays
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For me the issue lies with
toLowerCase
, this should only happen IF string was replaced.chDe
->ch-de
American Flag
->American Flag