Skip to content

Commit

Permalink
Improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 29, 2023
1 parent 507d5d0 commit 46d17e7
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 74 deletions.
95 changes: 63 additions & 32 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,76 @@ export type CamelCaseKeys<
// Return anything else as-is.
: T;

type Options = {
export type Options = {
/**
Exclude keys from being camel-cased.
If this option can be statically determined, it's recommended to add `as const` to it.
@default []
*/
readonly exclude?: ReadonlyArray<string | RegExp>;

/**
Recurse nested objects and objects in arrays.
@default false
@example
```
import camelcaseKeys from 'camelcase-keys';
const object = {
'foo-bar': true,
nested: {
unicorn_rainbow: true
}
};
camelcaseKeys(object, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}
camelcaseKeys(object, {deep: false});
//=> {fooBar: true, nested: {unicorn_rainbow: true}}
```
*/
readonly deep?: boolean;

/**
Exclude keys from being camel-cased.
Uppercase the first character: `bye-bye` → `ByeBye`
If this option can be statically determined, it's recommended to add `as const` to it.
@default false
@default []
@example
```
import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-bar': true}, {pascalCase: true});
//=> {FooBar: true}
camelcaseKeys({'foo-bar': true}, {pascalCase: false});
//=> {fooBar: true}
````
*/
readonly exclude?: ReadonlyArray<string | RegExp>;
readonly pascalCase?: boolean;

/**
Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR`
@default false
@example
```
import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true});
//=> {fooBAR: true}
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false});
//=> {fooBar: true}
````
*/
readonly preserveConsecutiveUppercase?: boolean;

/**
Exclude children at the given object paths in dot-notation from being camel-cased. For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`.
Expand All @@ -115,15 +169,17 @@ type Options = {
```
import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({
const object = {
a_b: 1,
a_c: {
c_d: 1,
c_e: {
e_f: 1
}
}
}, {
};
camelcaseKeys(object, {
deep: true,
stopPaths: [
'a_c.c_e'
Expand All @@ -141,21 +197,6 @@ type Options = {
```
*/
readonly stopPaths?: readonly string[];

/**
Uppercase the first character as in `bye-bye` → `ByeBye`.
@default false
*/
readonly pascalCase?: boolean;

/**
Preserve consecutive uppercase characters: `foo_BAR` → `FooBAR`.
@default false
*/
readonly preserveConsecutiveUppercase?: boolean;

};

/**
Expand All @@ -174,16 +215,6 @@ camelcaseKeys({'foo-bar': true});
// Convert an array of objects
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
//=> [{fooBar: true}, {barFoo: false}]
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}
camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}),
//=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}}
// Convert object keys to pascal case
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true});
//=> {FooBar: true, Nested: {UnicornRainbow: true}}
```
@example
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
"camelcase": "^8.0.0",
"map-obj": "5.0.0",
"quick-lru": "^6.1.1",
"type-fest": "^4.2.0"
"type-fest": "^4.3.2"
},
"devDependencies": {
"ava": "^5.3.1",
"matcha": "^0.7.0",
"tsd": "^0.28.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
},
"xo": {
Expand Down
107 changes: 67 additions & 40 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ camelcaseKeys({'foo-bar': true});
// Convert an array of objects
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
//=> [{fooBar: true}, {barFoo: false}]

camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}

camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}),
//=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}}

// preserve Uppercase if they are consecutive
camelcaseKeys({'foo-BAR': true, nested: {unicorn_RAINbow: true}}, {deep: true, preserveConsecutiveUppercase: false});
//=> {fooBar: true, nested: {unicornRainbow: true}}
camelcaseKeys({'foo-BAR': true, nested: {unicorn_RAINbow: true}}, {deep: true, preserveConsecutiveUppercase: true});
//=> {fooBAR: true, nested: {unicornRAINbow: true}}

// Convert object keys to pascal case
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true});
//=> {FooBar: true, Nested: {UnicornRainbow: true}}
```

```js
Expand Down Expand Up @@ -70,23 +54,87 @@ Default: `[]`

Exclude keys from being camel-cased.

##### deep

Type: `boolean`\
Default: `false`

Recurse nested objects and objects in arrays.

```js
import camelcaseKeys from 'camelcase-keys';

const object = {
'foo-bar': true,
nested: {
unicorn_rainbow: true
}
};

camelcaseKeys(object, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}

camelcaseKeys(object, {deep: false});
//=> {fooBar: true, nested: {unicorn_rainbow: true}}
```

##### pascalCase

Type: `boolean`\
Default: `false`

Uppercase the first character: `bye-bye``ByeBye`

```js
import camelcaseKeys from 'camelcase-keys';

camelcaseKeys({'foo-bar': true}, {pascalCase: true});
//=> {FooBar: true}

camelcaseKeys({'foo-bar': true}, {pascalCase: false});
//=> {fooBar: true}
````

##### preserveConsecutiveUppercase

Type: `boolean`\
Default: `false`

Preserve consecutive uppercase characters: `foo-BAR``FooBAR`

```js
import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true});
//=> {fooBAR: true}
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false});
//=> {fooBar: true}
````
##### stopPaths
Type: `string[]`\
Default: `[]`
Exclude children at the given object paths in dot-notation from being camel-cased. For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`.
Exclude children at the given object paths in dot-notation from being camel-cased.
For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`.
```js
camelcaseKeys({
import camelcaseKeys from 'camelcase-keys';

const object = {
a_b: 1,
a_c: {
c_d: 1,
c_e: {
e_f: 1
}
}
}, {
};

camelcaseKeys(object, {
deep: true,
stopPaths: [
'a_c.c_e'
Expand All @@ -105,27 +153,6 @@ camelcaseKeys({
*/
```

##### deep

Type: `boolean`\
Default: `false`

Recurse nested objects and objects in arrays.

##### pascalCase

Type: `boolean`\
Default: `false`

Uppercase the first character as in `bye-bye``ByeBye`.

##### preserveConsecutiveUppercase

Type: `boolean`\
Default: `false`

Preserve consecutive uppercase characters: `foo-BAR``FooBAR` if true vs `foo-BAR``FooBar` if false

## Related

- [decamelize-keys](https://github.com/sindresorhus/decamelize-keys) - The inverse of this package
Expand Down

0 comments on commit 46d17e7

Please sign in to comment.