Skip to content

Commit

Permalink
fix: Field starting with '_' generates an interface property starting…
Browse files Browse the repository at this point in the history
… with 'undefined' (#344)
  • Loading branch information
doochik authored Aug 5, 2021
1 parent 2149cab commit fab354f
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -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');
});
});

0 comments on commit fab354f

Please sign in to comment.