From e6228b0bf25476276a459a51885e00445a2c8c3a Mon Sep 17 00:00:00 2001 From: Aleksei Androsov Date: Thu, 5 Aug 2021 21:46:42 +0300 Subject: [PATCH] fix: Field starting with '_' generates an interface property starting with 'undefined' --- src/case.ts | 3 ++- tests/case-test.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/case.ts b/src/case.ts index ed90bff75..37656b557 100644 --- a/src/case.ts +++ b/src/case.ts @@ -6,7 +6,8 @@ export function maybeSnakeToCamel(s: string, options: Pick { 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()); } diff --git a/tests/case-test.ts b/tests/case-test.ts index cad40a158..33971df4e 100644 --- a/tests/case-test.ts +++ b/tests/case-test.ts @@ -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'); + }); });