Skip to curriculum
Jump to the editor

21.8. Convert every key to camelCase

Easy
Topics
15–25 min

Many back ends and databases name fields in snake_case (first_name, created_at), and some config formats use kebab-case. JavaScript code usually uses camelCase. Converting at the edge, once, when the response arrives, keeps the rest of the app consistent, but only if every key is converted: the keys of nested objects and of objects inside arrays too.

Write two functions in camelize.js. toCamelCase(key) converts one key. camelizeKeys(value) returns a copy of a value in which every object key, at every depth, has been converted.

Signature

JAVASCRIPT
function toCamelCase(key)    // returns a string
function camelizeKeys(value) // returns the converted copy
  • key: a string.
  • value: any value. It is never changed.

Requirements

toCamelCase:

  1. The key is split into words at runs of _, - and whitespace. Empty words (from leading, trailing or repeated separators) are dropped.
  2. The first word gets its first character lower-cased; every later word gets its first character upper-cased. The rest of each word is left exactly as it is.
  3. A key with no separator is one word: "firstName" stays "firstName", "FirstName" becomes "firstName".
  4. Digits are kept: "line_2" becomes "line2".
  5. A key that has no words at all ("", "_", "--") is returned unchanged.

camelizeKeys:

  1. Every key of every plain object (prototype Object.prototype or null) is converted with toCamelCase, at every depth, including objects inside arrays.
  2. Arrays stay arrays; their items are converted but their indexes are not keys.
  3. Values are never converted: a string value such as "snake_value" stays as it is. Dates, Maps, class instances and other non-plain objects are put in the copy as they are.
  4. The result is new: the input is not changed and no plain object or array is shared with it.
  5. When two keys of one object convert to the same name, the later one in Object.keys order wins.
  6. An own key named __proto__ (as JSON.parse creates) becomes the key proto and changes no prototype.
  7. A value that contains itself throws a TypeError.

Examples

JAVASCRIPT
toCamelCase("created_at");    // "createdAt"
toCamelCase("Content-Type");  // "contentType"
toCamelCase("__private__");   // "private"
toCamelCase("user_ID");       // "userID"
JAVASCRIPT
camelizeKeys({
  user_id: 7,
  order_items: [{ unit_price: 3, sku_code: "a_b" }],
  created_at: new Date(0),
});
// { userId: 7, orderItems: [{ unitPrice: 3, skuCode: "a_b" }], createdAt: <the same Date> }

Environment

  • Plain JavaScript in a browser page; the tests call the globals toCamelCase and camelizeKeys from camelize.js.
  • Only own, enumerable string keys are converted (Object.keys). Symbol keys are out of scope.
  • Splitting "HTTPStatus" into words by capitals is out of scope: only _, - and whitespace separate words.

Hints

  1. Which string method splits on a regular expression, and what does a + after a character class change?
  2. In camelizeKeys, which values are walked into, and which are put back as they are?

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…