Convert Between Naming Cases

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write toCamelCase, toSnakeCase, toKebabCase and toTitleCase, each taking a string in any style and returning it in its own. First split the input into words: any character other than an English letter or digit separates words, and a capital letter starts a new word when it follows a lowercase letter or a digit, or when it follows another capital and is followed by a lowercase letter. So "XMLHttpRequest" is XML, Http and Request, "getURLForID" is get, URL, For and ID, and digits stay in the word before them: "version2Beta" is version2 and Beta. Then toSnakeCase joins the lowercased words with "_", toKebabCase with "-", toTitleCase capitalises each word (first letter upper case, the rest lower) and joins them with spaces, and toCamelCase lowercases the first word and capitalises the rest, with nothing between them. An input with no words gives "".

Your code must pass

  • Splits words at spaces, underscores and hyphens
  • Splits camelCase and PascalCase words
  • Keeps acronyms together
  • Keeps digits with the word before
  • Converts between all four styles