re: Daily Challenge #95 - CamelCase Method VIEW POST

FULL DISCUSSION
 

I agree with SavagePixie. replace is the way to go if you're in JS!

Here's an extended version that covers all the cases I can think of...

const toUpperCamelCase = str => str.replace(
  /[^a-zA-Z]*([a-zA-Z]?)([a-z]+|[A-Z]*)/g,
  (_, firstLetter, rest) => firstLetter.toUpperCase() + rest.toLowerCase()
);

> [
  'Fudge And Sprite', // Words
  'fudge and sprite', // words
  'fudge-and-sprite', // kebab-case
  'fudge_and_sprite', // underscore_case
  'fudgeAndSprite',   // lowerCamelCase
  'FudgeAndSprite',   // UpperCamelCase stays as it is
  'FUDGE_AND_SPRITE', // CONSTANT_CASE
  'fUDGEaNDsPRITE',   // um, uPSIDEDOWNcASE I guess
  '>>> Fudge,    (and SPRITE!)' // or whatevs
].map(toUpperCamelCase);
< ["FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite", "FudgeAndSprite"]

Regex:
Regular expression visualization
test in Debuggex

code of conduct - report abuse