chkr
v8.0.0
Published
js type check
Downloads
17
Readme
chkr
js type check
Installation
npm install chkr
Example
const {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())
API
Type
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()
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
sample
method returns a sample data of a type
Concrete Type
Id
any typeNull
null or undefinedAny
any thing but notNull
Num
input a number or a string consist of only digits will output a exact numberStr
any thing will transfer to stringBool
true or 'true' to true, false or 'false' to false, number, object... throwsTime
Date or any thing can be transfered into Date bynew Date
Type Combinator
Const
a type with only one value (1)Or
accept some types returns a type which can be all the given types (+)Obj
accept an object indicate an object has some key with some type (*)Optional
make type optional (Null + Type)Kv
accept a type called value type to generate a key value paire object typeArr
Array of a type
Recursive Type Def
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)
Test
$ npm test
$ npm run test-cov