Skip to content

Commit

Permalink
fix: Fix snakeToCamel single value parsing. (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh authored Feb 17, 2022
1 parent 53e6a85 commit e1ad866
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
});
});

0 comments on commit e1ad866

Please sign in to comment.