# camel-keys

[![npm version](https://img.shields.io/npm/v/camel-keys.svg)](https://www.npmjs.com/package/camel-keys)
[![CI](https://github.com/fahdi/camel-keys/actions/workflows/ci.yml/badge.svg)](https://github.com/fahdi/camel-keys/actions/workflows/ci.yml)
[![install size](https://packagephobia.com/badge?p=camel-keys)](https://packagephobia.com/result?p=camel-keys)
[![license](https://img.shields.io/npm/l/camel-keys.svg)](./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

```bash
npm install camel-keys
```

## Usage

```js
// 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](https://www.fahdmurtaza.com)
