Skip to curriculum
Jump to the editor

2.9. Convert between camelCase, snake_case and kebab-case

Easy
Topics

This question is in

2. Strings and text
15–25 min

The same name is written in different styles in different places: backgroundColor in JavaScript, background-color in CSS and URLs, background_color in a database or a JSON API. Code that moves data between them converts names all the time, and the hard part is not the joining but the splitting: where do the words of XMLHttpRequest or html5Parser begin?

Write four functions in case.js: toWords, which finds the words, and toCamelCase, toSnakeCase and toKebabCase, which join them.

Signature

JAVASCRIPT
function toWords(text)     // an array of lower-case words
function toCamelCase(text) // "backgroundColor"
function toSnakeCase(text) // "background_color"
function toKebabCase(text) // "background-color"

Requirements

  1. toWords returns the words of text in lower case. Any character that is not an ASCII letter or digit separates words (spaces, _, -, ., /…). Separators at the edges or in runs produce no empty words.
  2. A lower-case letter or a digit followed by a capital starts a new word: "fooBar" gives foo, bar; "html5Parser" gives html5, parser.
  3. In a run of capitals followed by a lower-case letter, the last capital starts a new word: "XMLHttpRequest" gives xml, http, request. A run of capitals with no lower-case letter after it is one word: "userID" gives user, id.
  4. Digits stay with the letters before them ("version2" is one word), and a capital followed by lower-case letters is not split ("Hello" is one word).
  5. toCamelCase writes the first word in lower case and every later word with a capital first letter, with nothing between them.
  6. toSnakeCase joins the words with "_", and toKebabCase with "-".
  7. All four accept any style as input, including their own, SCREAMING_SNAKE_CASE and PascalCase, and return an empty result for an empty string.
  8. When every word starts with a letter and has at least two characters, converting through another style gives the same result as converting directly. Other names can lose a word boundary in camelCase, and that is expected: toCamelCase("foo_2") is "foo2", which reads back as one word, and toCamelCase("a_b_c") is "aBC", which reads back as a, bc.

Examples

JAVASCRIPT
toWords("XMLHttpRequest");     // ["xml", "http", "request"]
toWords("  user.first-name "); // ["user", "first", "name"]
toWords("html5Parser");        // ["html5", "parser"]
JAVASCRIPT
toCamelCase("background-color");  // "backgroundColor"
toCamelCase("MAX_RETRY_COUNT");   // "maxRetryCount"
toSnakeCase("getHTTPResponse");   // "get_http_response"
toKebabCase("Area51Zone");        // "area51-zone"
toKebabCase("");                  // ""

Environment

  • Plain JavaScript in a browser page; the tests call the four globals from case.js.
  • The argument is always a string. Letters outside A-Z and a-z are not part of the tests.
  • Any built-in may be used.

Hints

  1. Instead of finding the words directly, could you first insert a space at every word boundary, then split on separators?
  2. In "XMLHttp", which two characters mark the boundary, and how would a pattern describe them?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…