From e1ad866c95751c37ed13f02f4da2dc9076ab4758 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Wed, 16 Feb 2022 19:50:36 -0600 Subject: [PATCH] fix: Fix snakeToCamel single value parsing. (#513) --- src/options.ts | 11 +++++++++-- tests/options-test.ts | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/options.ts b/src/options.ts index f1529486d..1214dba25 100644 --- a/src/options.ts +++ b/src/options.ts @@ -146,6 +146,8 @@ export function optionsFromParameter(parameter: string | undefined): Options { options.snakeToCamel = []; } else if ((options.snakeToCamel as any) === true) { options.snakeToCamel = ['keys', 'json']; + } else if (typeof options.snakeToCamel === 'string') { + options.snakeToCamel = [options.snakeToCamel]; } return options; @@ -155,8 +157,13 @@ export function optionsFromParameter(parameter: string | undefined): Options { function parseParameter(parameter: string): Options { const options = {} as any; const pairs = parameter.split(',').map((s) => s.split('=')); - pairs.forEach(([key, value]) => { - options[key] = value === 'true' ? true : value === 'false' ? false : value; + pairs.forEach(([key, _value]) => { + const value = _value === 'true' ? true : _value === 'false' ? false : _value; + if (options[key]) { + options[key] = [options[key], value]; + } else { + options[key] = value; + } }); return options; } diff --git a/tests/options-test.ts b/tests/options-test.ts index 9b8a89b5a..dfca28c72 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -85,4 +85,14 @@ describe('options', () => { useOptionals: 'messages', }); }); + + it('can set snakeToCamel as string', () => { + const options = optionsFromParameter('snakeToCamel=keys'); + expect(options).toMatchObject({ snakeToCamel: ['keys'] }); + }); + + it('can set multiple values as an array', () => { + const options = optionsFromParameter('foo=one,foo=two'); + expect(options).toMatchObject({ foo: ['one', 'two'] }); + }); });