Skip to content

Commit

Permalink
fix(core): restore lost coerceTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jun 18, 2020
1 parent 0e837cd commit 23abfff
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
57 changes: 56 additions & 1 deletion packages/tao/src/shared/params.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
import { convertAliases, convertToCamelCase, lookupUnmatched } from './params';
import {
coerceTypes,
convertAliases,
convertToCamelCase,
lookupUnmatched,
} from './params';

describe('params', () => {
describe('coerceTypes', () => {
it('should handle booleans', () => {
const opts = coerceTypes({ a: true, b: 'true', c: false, d: 'true' }, {
properties: {
a: { type: 'boolean' },
b: { type: 'boolean' },
c: { type: 'boolean' },
d: { type: 'string' },
},
} as any);

expect(opts).toEqual({
a: true,
b: true,
c: false,
d: 'true',
});
});

it('should handle numbers', () => {
const opts = coerceTypes({ a: 1, b: '2', c: '3' }, {
properties: {
a: { type: 'number' },
b: { type: 'number' },
c: { type: 'string' },
},
} as any);

expect(opts).toEqual({
a: 1,
b: 2,
c: '3',
});
});

it('should handle arrays', () => {
const opts = coerceTypes({ a: 'one,two', b: 'three,four' }, {
properties: {
a: { type: 'array' },
b: { type: 'string' },
},
} as any);

expect(opts).toEqual({
a: ['one', 'two'],
b: 'three,four',
});
});
});

describe('convertToCamelCase', () => {
it('should convert dash case to camel case', () => {
expect(
Expand Down
3 changes: 3 additions & 0 deletions packages/tao/src/shared/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function convertToCamelCase(parsed: Options): Options {
{}
);
}

function camelCase(input: string): string {
if (input.indexOf('-') > 1) {
return input
Expand All @@ -67,6 +68,8 @@ export function coerceTypes(opts: Options, schema: Schema): Options {
opts[k] = opts[k] === true || opts[k] === 'true';
} else if (schema.properties[k] && schema.properties[k].type == 'number') {
opts[k] = Number(opts[k]);
} else if (schema.properties[k] && schema.properties[k].type == 'array') {
opts[k] = opts[k].toString().split(',');
}
});
return opts;
Expand Down

0 comments on commit 23abfff

Please sign in to comment.