# @mmckelvy/case

> A set of utility functions converting the case of strings.

Latest version **1.3.1** (published 2023-06-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mmckelvy/case
pnpm add @mmckelvy/case
yarn add @mmckelvy/case
bun add @mmckelvy/case
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2023-06-28 |
| First published | 2019-08-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mark McKelvy |
| Maintainers | mmckelvy |
| Keywords | case, camel, case, snake, case |

## Links

- npm: https://www.npmjs.com/package/@mmckelvy/case
- Repository: https://github.com/mmckelvy/case
- Homepage: https://github.com/mmckelvy/case#readme
- Issues: https://github.com/mmckelvy/case/issues
- npm.io page: https://npm.io/package/@mmckelvy/case

## Dependencies (1)

- [@mmckelvy/map-object](https://npm.io/package/@mmckelvy/map-object.md) ^1.0.3

## 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.3.1 (latest) — 2023-06-28
- 1.3.0 — 2023-06-28
- 1.2.0 — 2021-06-25
- 1.1.2 — 2020-09-20
- 1.1.1 — 2020-09-20
- 1.1.0 — 2020-09-20
- 1.0.4 — 2020-01-02
- 1.0.3 — 2019-12-20
- 1.0.2 — 2019-08-17
- 1.0.0 — 2019-08-16

## README

# case
A set of utility functions for converting case.

## Installation
```
npm install @mmckelvy/case
```

## Usage
Convert the case of a word:

```javascript
const { camelCase, snakeCase } = require('@mmckelvy/case');

camelCase('first_name');
// -> 'firstName'

snakeCase('firstName');
// -> 'first_name'

properCase('first_name');
// -> 'First name'

titleCase('first_name');
// -> 'First Name'
```

Handles kabob-case, extra characters, and spaces as well:
```javascript
camelCase('first-name');
// -> 'firstName'

snakeCase('First--Name');
// -> 'first_name'
```

A frequent use case for case conversion is converting the case of object keys.  `case` comes with `camelCaseKeys` and `snakeCaseKeys` helper methods to handle this use case:

```javascript
const { camelCaseKeys, snakeCaseKeys } = require('@mmckelvy/case');

const snakeToCamel = {
  first_name: 'John',
  last_name: 'Smith'
};

camelCaseKeys(snakeToCamel);
/* ->
{
  firstName: 'John',
  lastName: 'Smith'
};
*/

const camelToSnake = {
  firstName: 'John',
  lastName: 'Smith'
};

snakeCaseKeys(camelToSnake);
/* ->
{
  first_name: 'John',
  last_name: 'Smith'
};
*/
```
Works with arrays:

```javascript
const input = [
  {
    first_name: 'John',
    last_name: 'Smith'
  },
  {
    first_name: 'Jane',
    last_name: 'Jenkins'
  },
];

camelCaseKeys(input);
/* ->
[
  {
    firstName: 'John',
    lastName: 'Smith'
  },
  {
    firstName: 'Jane',
    lastName: 'Jenkins'
  },
];
*/
```

To handle nested objects, pass `{recursive: true}` as the second argument:

```javascript
const input = {
  first_name: 'John',
  last_name: 'Smith',
  address: {
    country_of_residence: 'USA',
  }
};

camelCaseKeys(input, {recursive: true});

/* ->
{
  firstName: 'John',
  lastName: 'Smith',
  address: {
    countryOfResidence: 'USA'
  }
};
*/

```

## API
### `camelCase(str)`
Convert a string to camelCase.

#### str
`string`

The string to convert.

#### return
`string`

A new string in camelCase.


### `snakeCase(str)`
Convert a string to snake_case.

#### str
`string`

The string to convert.

#### return
`string`

A new string in snake_case.


### `properCase(str)`
Convert a string to Proper case.

#### str
`string`

The string to convert.

#### return
`string`

A new string in Proper case.


### `titleCase(str)`
Convert a string to Title Case.

#### str
`string`

The string to convert.

#### return
`string`

A new string in Title Case.


### `camelCaseKeys(input, options?)`
Convert object keys to camelCase.

#### input
`object` or `object[]`

An object or array of objects with keys you'd like to convert.

#### options (optional)
`object`

##### options.recursive
`boolean`

Recursively convert keys for nested objects and arrays.

#### return
A new `object` or `object[]` (does not mutate the original `object` or `object[]`).


### `snakeCaseKeys(input, options?)`
Convert object keys to snake_case.

#### input
`object` or `object[]`

An object or array of objects with keys you'd like to convert.

#### options (optional)
`object`

##### options.recursive
`boolean`

Recursively convert keys for nested objects and arrays.

#### return
A new `object` or `object[]` (does not mutate the original `object` or `object[]`).


### `compare(a, b, fn?)`
Standardize two strings and then compare them.

#### a
`string`

The first string to compare.

#### b
`string`

The second string to compare.

#### fn (optional)
`function`

A function applied to each string to standardize it.  Defaults to `snakeCase`.

#### return
`boolean`

`true` if the strings are `===` after applying the standardization function, else `false`.

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