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

fix: Field starting with '_' generates an interface property starting with 'undefined' #344

Merged
merged 1 commit into from
Aug 5, 2021
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
3 changes: 2 additions & 1 deletion src/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export function maybeSnakeToCamel(s: string, options: Pick<Options, 'snakeToCame
.split('_')
.map((word, i) => {
if (i === 0) {
return word[0] + word.substring(1).toLowerCase();
// if first symbol is "_" then skip it
return word ? word[0] + word.substring(1).toLowerCase() : '';
} else {
return capitalize(word.toLowerCase());
}
Expand Down
16 changes: 16 additions & 0 deletions tests/case-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ describe('case', () => {
it('does nothing is already camel', () => {
expect(maybeSnakeToCamel('FooBar', { snakeToCamel: true })).toEqual('FooBar');
});

// deal with original protoc which converts
// _uuid -> Uuid
// __uuid -> Uuid
// _uuid_foo -> UuidFoo
it('converts snake to camel with first underscore', () => {
expect(maybeSnakeToCamel('_uuid', { snakeToCamel: true })).toEqual('Uuid');
});

it('converts snake to camel with first double underscore', () => {
expect(maybeSnakeToCamel('__uuid', { snakeToCamel: true })).toEqual('Uuid');
});

it('converts snake to camel with first underscore and camelize other', () => {
expect(maybeSnakeToCamel('_uuid_foo', { snakeToCamel: true })).toEqual('UuidFoo');
});
});