Skip to content

Commit

Permalink
fix: Leave mixed case in all words. (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh committed Jan 21, 2022
1 parent 51f121b commit 8a26c9c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ export function maybeSnakeToCamel(s: string, options: Pick<Options, 'snakeToCame
return s
.split('_')
.map((word, i) => {
if (i === 0) {
// if first symbol is "_" then skip it
return word ? word[0] + (hasLowerCase ? word.substring(1) : word.substring(1).toLowerCase()) : '';
} else {
return capitalize(word.toLowerCase());
}
// If the word is already mixed case, leave the exist case as-is
word = hasLowerCase ? word : word.toLowerCase();
return i === 0 ? word : capitalize(word);
})
.join('');
} else {
Expand Down
4 changes: 3 additions & 1 deletion tests/case-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ describe('case', () => {
});

it('de-upper cases', () => {
expect(maybeSnakeToCamel('FOO_BAR', keys)).toEqual('FooBar');
expect(maybeSnakeToCamel('FOO_BAR', keys)).toEqual('fooBar');
});

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

it('leaves the first character as it was', () => {
Expand Down

0 comments on commit 8a26c9c

Please sign in to comment.