Skip to content

fahdi/camel-keys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

camel-keys

npm version CI install size license

Convert an object's keys to camelCase — from hyphen-case, snake_case, or spaced words.

  • Zero dependencies
  • ESM + CommonJS (works with both import and require)
  • Typed — ships hand-written .d.ts
  • Optional deep mode recurses nested objects and objects inside arrays
  • Never mutates its input

Install

npm install camel-keys

Usage

// 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 }] }

API

camelKeys(input, deep?)

input

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.

deep

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.

Key conversion

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.

License

MIT © Fahad Murtaza

About

Convert JSON object's keys to camelCase from hyphen-case

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages