Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pascalCase option #42

Merged
merged 5 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/bench.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals bench suite set */
'use strict';
const camelcaseKeysNpm = require('camelcase-keys');
const camelcaseKeys = require('..');
const fixture = require('./fixture');
const camelcaseKeys = require('..');

suite('camelcaseKeys', () => {
set('mintime', 1000);
Expand Down
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