variable-type
A high-performance javascript(
less then 1 kb
) library, runtime type checking for variable and similar objects.一个非常简单的(仅 1 kb)高性能的用于做变量结构校验的 JavaScript 模块。
Inspired by prop-types.
1. Install
npm i --save variable-type
Then import it.
;
2. API & Types
Before use it to check variable, you should make your Types.
And the library contains Types
below:
- VT.bool
- VT.func
- VT.number
- VT.string
- VT.object
- VT.array
- VT.any
- VT.null
- VT.undefined
- VT.instanceOf(Class)
- VT.typeOf(String)
- VT.in(Array)
- VT.arrayOf(Type)
- VT.shape(TypeObject)
- VT.and(TypeArray)
- VT.or(TypeArray)
- VT.not(Type)
- VT.apply(Function)
The Type
has 2 API:
- check(value)
- optional(): convent the type into optional.
You can see all the usage in the test cases file.
If more Types are needed, welcome to send a pull request
, or put an issue to me.
3. Usage examples
Here is some examples. More you can see in test.ts file.
- Simple usage
VTnumber;VTstring;VTfunc;VTbool;VTobject;VTarray;VTnull;VTundefined;VT;VT;
- And / Or / Not
VT; VT; VT;
Array
type.
const arr = 'hello' 'world' 25 1992 8 1; const types = VT; types; // will get true.
Object
type.
const obj = name: 'hustcc' boy: true birthday: 1992 8 1; const types = VTshape name: VTstring boy: VTbool birthday: VT; types; // will get true.
Complex
example.
// The only API `check`.VTshape a: VTbool b: VTnumber c: VTstring d: VTfunc e: VT f: VT g: VTshape h: VT ; // Then will get true.
- Optional type
VTshape name: VTstring birthday: VTstring sex: VTstring; // Then will get true.
4. Test & Perf
# install dependence
$ npm i
# run unit test
$ npm run test
# run performance test
$ npm run perf
[OPS] variable-type / prop-types = 5.033
License
MIT@hustcc.