-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk:utils): add prop types util (#87)
- Loading branch information
Showing
14 changed files
with
165 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
|
||
import { PropTypes } from '../propTypes' | ||
|
||
describe('propTypes.ts', () => { | ||
test('maxLength work', async () => { | ||
const maxLength = PropTypes.maxLength(4) | ||
expect(maxLength.default).toEqual(undefined) | ||
|
||
const validator = maxLength.validator!.bind(undefined) | ||
|
||
expect(validator('test')).toEqual(true) | ||
expect(validator('test1')).toEqual(false) | ||
}) | ||
|
||
test('minLength work', async () => { | ||
const minLength = PropTypes.minLength(4) | ||
expect(minLength.default).toEqual(undefined) | ||
|
||
const validator = minLength.validator!.bind(undefined) | ||
|
||
expect(validator('test')).toEqual(true) | ||
expect(validator('tes')).toEqual(false) | ||
}) | ||
|
||
test('positive work', async () => { | ||
const positive = PropTypes.positive | ||
expect(positive.default).toEqual(undefined) | ||
|
||
const validator = positive.validator!.bind(undefined) | ||
|
||
expect(validator(1)).toEqual(true) | ||
expect(validator(0)).toEqual(false) | ||
expect(validator(-1)).toEqual(false) | ||
}) | ||
|
||
test('bool work', async () => { | ||
const bool = PropTypes.bool | ||
expect(bool.default).toEqual(undefined) | ||
}) | ||
}) |
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,2 +1,3 @@ | ||
export * from './convert' | ||
export * from './propTypes' | ||
export * from './typeof' |
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,44 @@ | ||
import VueTypes, { toType, toValidableType, VueTypeDef, VueTypeValidableDef } from 'vue-types' | ||
|
||
// use `undefined` as the default value for each prop | ||
VueTypes.sensibleDefaults = {} | ||
|
||
export class PropTypes extends VueTypes { | ||
// define a custom validator that accepts configuration parameters | ||
static maxLength(max: number): VueTypeDef<string> { | ||
return toType('maxLength', { | ||
type: String, | ||
validator: (value: string) => value.length <= max, | ||
}) | ||
} | ||
|
||
static minLength(min: number): VueTypeDef<string> { | ||
return toType('minLength', { | ||
type: String, | ||
validator: (value: string) => value.length >= min, | ||
}) | ||
} | ||
|
||
// a native-like validator that supports the `.validable` method | ||
static get positive(): VueTypeValidableDef<number> { | ||
return toValidableType('positive', { | ||
type: Number, | ||
validator: (value: number) => value > 0, | ||
}) | ||
} | ||
|
||
// see https://github.com/dwightjack/vue-types/issues/71 | ||
static get bool(): VueTypeValidableDef<boolean> & { default: boolean } { | ||
return toValidableType('bool', { | ||
type: Boolean, | ||
default: undefined, | ||
}) as VueTypeValidableDef<boolean> & { default: boolean } | ||
} | ||
} | ||
|
||
// withUndefined(PropTypes.oneOfType([PropTypes.bool, PropTypes.string])) is work | ||
// see https://github.com/dwightjack/vue-types/issues/71 | ||
export function withUndefined<T extends { default?: unknown }>(type: T): T { | ||
type.default = undefined | ||
return type | ||
} |
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
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
Oops, something went wrong.