-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(#285): Implement More Types, Refactor, Test in KtCol
- Loading branch information
1 parent
41daabf
commit a7fae3a
Showing
18 changed files
with
421 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
packages/vue-props-validators/source/common/resolve-default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { REQUIRED } from '../constants' | ||
import { Option } from '../modules' | ||
|
||
export const resolveDefault = ( | ||
option: Option, | ||
): Option['default'] extends typeof REQUIRED | ||
? { required: true } | ||
: { default: Option['default'] } => | ||
option.default === REQUIRED ? { required: true } : { default: option.default } | ||
export const resolveDefault = <OPTION extends Option>(option: OPTION) => | ||
(option.default === REQUIRED | ||
? { required: true } | ||
: { default: option.default }) as OPTION['default'] extends typeof REQUIRED | ||
? { required: true } | ||
: { default: OPTION['default'] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1 @@ | ||
import { resolveDefault } from './common/resolve-default' | ||
import { REQUIRED } from './constants' | ||
import { createEnum } from './modules/enum' | ||
import { Options, ExtendsOne, Result } from './types' | ||
import { isNumber } from './utilities' | ||
|
||
export const vuePropsValidators = <PROPS extends Options>( | ||
props: PROPS, | ||
): { | ||
[KEY in keyof PROPS]: ExtendsOne< | ||
PROPS[KEY], | ||
'enum', | ||
Result<PROPS[KEY], StringConstructor>, | ||
ExtendsOne< | ||
PROPS[KEY], | ||
'float', | ||
Result<PROPS[KEY], NumberConstructor>, | ||
ExtendsOne< | ||
PROPS[KEY], | ||
'integer', | ||
Result<PROPS[KEY], NumberConstructor>, | ||
never | ||
> | ||
> | ||
> | ||
} => | ||
Object.fromEntries( | ||
Object.entries(props).map(([prop, option]) => { | ||
switch (option.type) { | ||
case 'enum': | ||
return [prop, createEnum(option)] | ||
|
||
case 'float': | ||
return [ | ||
prop, | ||
{ | ||
...resolveDefault(option), | ||
type: Number, | ||
validator: (value: unknown) => isNumber(value), | ||
}, | ||
] | ||
|
||
case 'integer': | ||
return [ | ||
prop, | ||
{ | ||
...resolveDefault(option), | ||
type: Number, | ||
validator: (value: unknown) => Number.isSafeInteger(value), | ||
}, | ||
] | ||
} | ||
|
||
throw new Error('invalid') | ||
}), | ||
) | ||
|
||
const X = vuePropsValidators({ | ||
example: { | ||
default: REQUIRED, | ||
nullable: true, | ||
type: 'enum', | ||
}, | ||
example2: { | ||
default: REQUIRED, | ||
nullable: true, | ||
type: 'integer', | ||
}, | ||
example3: { | ||
// eslint-disable-next-line no-magic-numbers | ||
default: () => 123, | ||
nullable: true, | ||
type: 'integer', | ||
}, | ||
}) | ||
export * as vuePropsValidators from './namespace' |
56 changes: 32 additions & 24 deletions
56
packages/vue-props-validators/source/modules/enum.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/vue-props-validators/source/modules/float.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { vuePropsValidators } from '..' | ||
import { REQUIRED } from '../constants' | ||
|
||
import { TypeFloat } from './float' | ||
|
||
import { Type } from '.' | ||
|
||
const BASE_FLOAT: TypeFloat = { | ||
default: () => null, | ||
nullable: true, | ||
type: Type.FLOAT, | ||
} | ||
|
||
test('float has correct type', () => | ||
expect(vuePropsValidators.create({ example: BASE_FLOAT })).toMatchObject({ | ||
example: { type: Number }, | ||
})) | ||
|
||
test('float validator works', () => { | ||
const { example } = vuePropsValidators.create({ | ||
example: BASE_FLOAT, | ||
}) | ||
|
||
expect(example.validator('test')).toBeFalsy() | ||
expect(example.validator(undefined)).toBeFalsy() | ||
/* eslint-disable no-magic-numbers */ | ||
expect(example.validator(42)).toBeTruthy() | ||
expect(example.validator(420)).toBeTruthy() | ||
expect(example.validator(2.3)).toBeTruthy() | ||
expect(example.validator(NaN)).toBeFalsy() | ||
/* eslint-enable no-magic-numbers */ | ||
}) | ||
|
||
test('float (nullable: false)', () => | ||
expect( | ||
vuePropsValidators | ||
.create({ | ||
example: { ...BASE_FLOAT, nullable: false }, | ||
}) | ||
.example.validator(null), | ||
).toBeFalsy()) | ||
|
||
test('float (nullable: true)', () => | ||
expect( | ||
vuePropsValidators | ||
.create({ | ||
example: { ...BASE_FLOAT, nullable: true }, | ||
}) | ||
.example.validator(null), | ||
).toBeTruthy()) | ||
|
||
test('float (default)', () => | ||
expect( | ||
vuePropsValidators | ||
.create({ | ||
example: { ...BASE_FLOAT, default: () => null }, | ||
}) | ||
.example.default(), | ||
).toBe(null)) | ||
|
||
test('float (required)', () => | ||
expect( | ||
vuePropsValidators.create({ | ||
example: { ...BASE_FLOAT, default: REQUIRED }, | ||
}), | ||
).toMatchObject({ | ||
example: { | ||
required: true, | ||
}, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import { TypeBase } from '.' | ||
import { baseValidator } from '../common/base-validator' | ||
import { resolveDefault } from '../common/resolve-default' | ||
import { Result } from '../types' | ||
import { isNumber } from '../utilities' | ||
|
||
import { TypeBase, Type } from '.' | ||
|
||
export type TypeFloat = TypeBase & { | ||
type: 'float' | ||
type: Type.FLOAT | ||
} | ||
|
||
export const createFloat = <OPTION extends TypeFloat>( | ||
option: OPTION, | ||
): Result<OPTION, NumberConstructor> => ({ | ||
...resolveDefault(option), | ||
type: Number, | ||
validator: baseValidator(option, (value: unknown) => isNumber(value)), | ||
}) |
Oops, something went wrong.