# snake-camel

> convert object keys from/to camelCase and snake_case

Latest version **1.0.9** (published 2024-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install snake-camel
pnpm add snake-camel
yarn add snake-camel
bun add snake-camel
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2024-12-10 |
| First published | 2019-02-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Shunsuke Watanabe |
| Maintainers | craftgear |
| Keywords | camelCase, snake_case |

## Links

- npm: https://www.npmjs.com/package/snake-camel
- Repository: https://github.com/craftgear/snake-camel
- Homepage: https://github.com/craftgear/snake-camel#readme
- Issues: https://github.com/craftgear/snake-camel/issues
- npm.io page: https://npm.io/package/snake-camel

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.0.9 (latest) — 2024-12-10
- 1.0.8 — 2021-02-25
- 1.0.7 — 2021-02-25
- 1.0.6 — 2020-10-10
- 1.0.5 — 2020-10-07
- 1.0.4 — 2020-10-07
- 1.0.3 — 2020-10-07
- 1.0.2 — 2020-10-07
- 1.0.1 — 2020-10-07
- 1.0.0 — 2019-02-28

## README

# snake-camel

[![Actions Status](https://github.com/craftgear/snake-camel/workflows/ci/badge.svg)](https://github.com/craftgear/snake-camel/actions)

convert property names from/to camelCase and snake_case recursively.

## How to use
### import
```
import { toCamel, toSnake } from 'snake-camel';
```

### `toCamel` - convert snake_case to camelCase
```
toCamel({
  foo_bar: 'foo',
  bar_baz: [1, 2, 3],
  qux_foo: {
    baz_qux: 'baz',
  },
  baz_qux: [{ baz_foo: 1 }, { qux_bar: { foo_foo: 2 } }, true, 1, 'fooBar', 'barBaz'],
})
```
yields:
```
{
   fooBar: 'foo',
   barBaz: [1, 2, 3],
   quxFoo: {
     bazQux: 'baz',
   },
   bazQux: [{ bazFoo: 1 }, { quxBar: { fooFoo: 2 } }, true, 1, 'fooBar', 'barBaz'],
}
```

### `toSnake` - convert camelCase to snake_case 
```
toSnake({
  fooBar: 'foo',
  barBaz: [1, 2, 3],
  quxFoo: {
    bazQux: 'baz',
  },
  bazQux: [{ bazFoo: 1 }, { quxBar: { fooFoo: 2 } }, true, 1, 'fooBar', 'barBaz'],
})
```
yields:
```
{
  foo_bar: 'foo',
  bar_baz: [1, 2, 3],
  qux_foo: {
    baz_qux: 'baz',
  },
  baz_qux: [{ baz_foo: 1 }, { qux_bar: { foo_foo: 2 } }, true, 1, 'fooBar', 'barBaz'],
}
```

### dealing with an `Array` of `Object`s
Mapping `toCamle` / `toSnake` over an array does the trick.
```
const snakeCase = {
    foo_bar: 'foo',
    bar_baz: [1, 2, 3],
};
const arr = [snakeCase, snakeCase].map(toCamel);
```
yields:
```
[
  {
    fooBar: 'foo',
    barBaz: [1, 2, 3],
  },
  {
    fooBar: 'foo',
    barBaz: [1, 2, 3],
  },
]
```

---
_Source: https://npm.io/package/snake-camel · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
