Skip to content

Commit

Permalink
fix: Have snakeToCamel leave existing mixed case. (#482)
Browse files Browse the repository at this point in the history
Fixes #478
  • Loading branch information
stephenh committed Jan 19, 2022
1 parent 2bfe0df commit c0bf0fc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Options } from './options';

export function maybeSnakeToCamel(s: string, options: Pick<Options, 'snakeToCamel'>): string {
if (options.snakeToCamel.includes('keys') && s.includes('_')) {
const hasLowerCase = !!s.match(/[a-z]/);
return s
.split('_')
.map((word, i) => {
if (i === 0) {
// if first symbol is "_" then skip it
return word ? word[0] + word.substring(1).toLowerCase() : '';
return word ? word[0] + (hasLowerCase ? word.substring(1) : word.substring(1).toLowerCase()) : '';
} else {
return capitalize(word.toLowerCase());
}
Expand Down
4 changes: 4 additions & 0 deletions tests/case-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ describe('case', () => {
expect(maybeSnakeToCamel('FOO_BAR', keys)).toEqual('FooBar');
});

it('leaves existing mixed cases', () => {
expect(maybeSnakeToCamel('clientI_d', keys)).toEqual('clientID');
});

it('leaves the first character as it was', () => {
expect(maybeSnakeToCamel('Foo_Bar', keys)).toEqual('FooBar');
});
Expand Down

0 comments on commit c0bf0fc

Please sign in to comment.