DEV Community

Discussion on: JavaScript Challenge 6: Convert string to camel case

Collapse
 
zerodragon profile image
Zero Dragon

You are correct! here is the revisited version:

const toCamelCase = ([first, ...rest]) => {
  const [head, ...tail] = rest.join('').split(/-|_/)
    .map(([head, ...tail]) => 
      [
        head.toUpperCase(),
        tail.join('').toLowerCase()
      ].join(''))
    .join('')
  return [first, head.toLowerCase(), ...tail].join('')
}

console.log(toCamelCase('THE_awesome-and_sUPERB-stealth-WARRIOR'))
console.log(toCamelCase('tHE_awesome-and_sUPERB-stealth-WARRIOR'))

// TheAwesomeAndSuperbStealthWarrior
// theAwesomeAndSuperbStealthWarrior
Enter fullscreen mode Exit fullscreen mode

darn PascalCase...