js type check
npm install chkrconst {Num, Optional, Arr, Str, Bool} = require('chkr');
//simple type check
Num(1) //==> 1
Num('1') //throws error
Num('a') //throws error
//type combination
Optional(Num)(undefined) //===> undefined
Arr(Num)([1,2,3]) //===> [1,2,3]
console.log(Obj({
user: Str,
age: Num,
isAdmin: Bool,
pages: Arr(Str)
}).sample())a type is a js function with sample as it's method. it's inspect symbol is customized to show the infomation of itself.
type function checks and parse the input value and returns then transformed value or throw an error if the input value is not the required type
sample method returns a sample data of a type
Idany typeNullnull or undefinedAnyany thing but notNullNuminput a number or a string consist of only digits will output a exact numberStrany thing will transfer to stringBooltrue or 'true' to true, false or 'false' to false, number, object... throwsTimeDate or any thing can be transfered into Date bynew Date
Consta type with only one value (1)Oraccept some types returns a type which can be all the given types (+)Objaccept an object indicate an object has some key with some type (*)Optionalmake type optional (Null + Type)Kvaccept a type called value type to generate a key value paire object typeArrArray of a type
recursive type is supported using a fn withSelf. you can use this to define an List
const List = ValueType => withSelf(Self => Or(Const(Empty), Obj({head: ValueType, tail: Self})))
const NumList = List(Num)$ npm test
$ npm run test-cov