21.8. Convert every key to camelCase
Topics
This question is in
21. Recursion and nested dataMany 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
function toCamelCase(key) // returns a string
function camelizeKeys(value) // returns the converted copykey: a string.value: any value. It is never changed.
Requirements
toCamelCase:
- The key is split into words at runs of
_,-and whitespace. Empty words (from leading, trailing or repeated separators) are dropped. - 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.
- A key with no separator is one word:
"firstName"stays"firstName","FirstName"becomes"firstName". - Digits are kept:
"line_2"becomes"line2". - A key that has no words at all (
"","_","--") is returned unchanged.
camelizeKeys:
- Every key of every plain object (prototype
Object.prototypeornull) is converted withtoCamelCase, at every depth, including objects inside arrays. - Arrays stay arrays; their items are converted but their indexes are not keys.
- 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. - The result is new: the input is not changed and no plain object or array is shared with it.
- When two keys of one object convert to the same name, the later one in
Object.keysorder wins. - An own key named
__proto__(asJSON.parsecreates) becomes the keyprotoand changes no prototype. - A value that contains itself throws a
TypeError.
Examples
toCamelCase("created_at"); // "createdAt"
toCamelCase("Content-Type"); // "contentType"
toCamelCase("__private__"); // "private"
toCamelCase("user_ID"); // "userID"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
toCamelCaseandcamelizeKeysfromcamelize.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
- Which string method splits on a regular expression, and what does a
+after a character class change? - 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