Skip to content

Commit

Permalink
Add pascalCase option (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
niktekusho and sindresorhus committed Sep 23, 2019
1 parent a31d51e commit 5f224ff
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ declare namespace camelcaseKeys {
@default []
*/
readonly exclude?: ReadonlyArray<string | RegExp>;

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

Expand All @@ -36,6 +43,9 @@ camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: 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}}
import minimist = require('minimist');
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ const cache = new QuickLru({maxSize: 100000});
const camelCaseConvert = (input, options) => {
options = {
deep: false,
pascalCase: false,
...options
};

const {exclude} = options;
const {exclude, pascalCase} = options;

return mapObj(input, (key, value) => {
if (!(exclude && has(exclude, key))) {
if (cache.has(key)) {
key = cache.get(key);
} else {
const ret = camelCase(key);
const ret = camelCase(key, {pascalCase});

if (key.length < 100) { // Prevent abuse
cache.set(key, ret);
Expand Down
3 changes: 3 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ expectType<{[key: string]: unknown}>(camelcaseKeys({'foo-bar': true}));
expectType<{[key: string]: unknown}>(
camelcaseKeys({'foo-bar': true}, {deep: true})
);
expectType<{[key: string]: unknown}>(
camelcaseKeys({'foo-bar': true}, {deep: true, pascalCase: true})
);
expectType<{[key: string]: unknown}>(
camelcaseKeys({'foo-bar': true}, {exclude: ['foo', /bar/]})
);
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);

camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: 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 @@ -66,6 +70,13 @@ Default: `false`

Recurse nested objects and objects in arrays.

##### pascalCase

Type: `boolean`<br>
Default: `false`

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


## Related

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ test('deep option', t => {
);
});

test('pascalCase option only', t => {
t.true(camelcaseKeys({'new-foo-bar': true}, {pascalCase: true}).NewFooBar);
});

test('pascalCase and deep options', t => {
t.deepEqual(
// eslint-disable-next-line camelcase
camelcaseKeys({p_foo_bar: true, p_obj: {p_two: false, p_arr: [{p_three_four: true}]}}, {deep: true, pascalCase: true}),
{PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}
);
});

test('handles nested arrays', t => {
t.deepEqual(
// eslint-disable-next-line camelcase
Expand Down

0 comments on commit 5f224ff

Please sign in to comment.