-
Notifications
You must be signed in to change notification settings - Fork 28
Closed
Description
The toKebab function does not work in every case. Here are some examples:
function toKebab (string) {
return string
.split('')
.map((letter, index) => {
if (/[A-Z]/.test(letter)) {
return ` ${letter.toLowerCase()}`
}
return letter
})
.join('')
.trim()
.replace(/[_\s]+/g, '-')
}
let samples = [
"ThereIsWay_too MuchCGIInFilms These-days",
"UnicodeCanBeCAPITALISEDTooYouKnow",
"CAPITALLetters at the StartOfAString_work_too",
"As_they_DoAtTheEND",
"BitteWerfenSie-dieFußballeInDenMüll",
"IchHabeUberGesagtNichtÜber",
"2BeOrNot2Be",
"ICannotBelieveThe100GotRenewed. It-isSOOOOOOBad"
];
samples.forEach(sample => console.log(toKebab(sample)))ThereIsWay_too MuchCGIInFilms These-daysbecomesthere-is-way-too-much-c-g-i-in-films-these-daysIchHabeUberGesagtNichtÜberbecomesich-habe-uber-gesagt-nichtÜberICannotBelieveThe100GotRenewed. It-isSOOOOOOBadbecomesi-cannot-believe-the100-got-renewed.-it-is-s-o-o-o-o-o-o-bad
Why don't you try this:
// From https://stackoverflow.com/a/62527131/15511745
function toKebab(string) {
return string.replace(/([^[\p{L}\d]+|(?<=[\p{Ll}\d])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{Ll}\d])|(?<=[\p{L}\d])(?=\p{Lu}[\p{Ll}\d]))/gu, '-'
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels