Convert an object's keys to camelCase — from hyphen-case, snake_case, or spaced words.
- Zero dependencies
- ESM + CommonJS (works with both
importandrequire) - Typed — ships hand-written
.d.ts - Optional deep mode recurses nested objects and objects inside arrays
- Never mutates its input
npm install camel-keys// ESM
import camelKeys from 'camel-keys';
// CommonJS
const camelKeys = require('camel-keys');
camelKeys({ 'foo-bar': true });
//=> { fooBar: true }
// Shallow (default): only top-level keys are converted
camelKeys({ 'foo-bar': true, nested: { unicorn_rainbow: true } });
//=> { fooBar: true, nested: { unicorn_rainbow: true } }
// Deep: recurse into nested objects
camelKeys({ 'foo-bar': true, nested: { unicorn_rainbow: true } }, true);
//=> { fooBar: true, nested: { unicornRainbow: true } }
// Deep also recurses into objects inside arrays
camelKeys({ 'my-list': [{ 'a-b': 1 }, { c_d: 2 }] }, true);
//=> { myList: [{ aB: 1 }, { cD: 2 }] }Type: object | Array
The object to convert. When deep is enabled, arrays are traversed too. The
input is never mutated — a new value is returned.
Type: boolean
Default: false
Recurse into nested objects and objects nested inside arrays. When false,
only the top-level keys are converted and nested values are copied as-is.
Keys are split on separators (hyphens, underscores, whitespace and other
non-alphanumeric characters) and on case/acronym/digit boundaries. Output
matches lodash.camelCase for ASCII input:
| Input | Output |
|---|---|
foo-bar |
fooBar |
unicorn_rainbow |
unicornRainbow |
FOO_BAR |
fooBar |
HTTPRequest |
httpRequest |
userID |
userId |
foo2bar |
foo2Bar |
If two keys map to the same camelCase key, the last one wins.
MIT © Fahad Murtaza