From c0bf0fc13da70e2bde923cd1746119d2e7ac4b2f Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Wed, 19 Jan 2022 08:41:47 -0600 Subject: [PATCH] fix: Have snakeToCamel leave existing mixed case. (#482) Fixes #478 --- src/case.ts | 3 ++- tests/case-test.ts | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/case.ts b/src/case.ts index 5e57b5fa7..4cd770126 100644 --- a/src/case.ts +++ b/src/case.ts @@ -2,12 +2,13 @@ import { Options } from './options'; export function maybeSnakeToCamel(s: string, options: Pick): 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()); } diff --git a/tests/case-test.ts b/tests/case-test.ts index 15c2536cf..1e5a55764 100644 --- a/tests/case-test.ts +++ b/tests/case-test.ts @@ -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'); });