DEV Community

Discussion on: Solving "Spinal Tap Case" / freeCodeCamp Algorithm Challenges

ttatsf profile image
tatsuo fukuchi • Edited on

How do you like this?

const spinalCase = s => 
    , '-'

spinalCase('SentencesContainingAWordWithOneLetter CONSTANT_CASE')  
// 'sentences-containing-a-word-with-one-letter-constant-case'
Thread Thread
willsmart profile image

Well, it's a trade-off. On the one hand "AAAA" is the word "A" four times in UpperCamelCase, on the other hand it's the word "AAAA" in CONSTANT_CASE. In the end the problem is just ambiguous.

You've solved the tradeoff by only breaking camelcase words where the next word has more than one char
That may be a fair strategy since it means that constant case is well covered, but does mean some pretty reasonable camel case sentences won't work. ("ThisIsASpinalTap" -> "this-isa-spinal-tap" )

In the end, it's about figuring out which cases are important and what rules you want to cover.

I think a good medium might be to take blocks of uppercase letters surrounded by non-letters, and stop them from being clobbered as camel case words by lowercasing them.

f = s => s
  .replace( // edited to handle numbers a bit

"this is a spinal tap a a a a"
// -> [