8.0.0 • Published 6 years ago
chkr v8.0.0
chkr
js type check
Installation
npm install chkrExample
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
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
Type Combinator
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 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-cov8.0.0
6 years ago
7.0.0
7 years ago
6.1.9
7 years ago
6.1.8
7 years ago
6.1.7
7 years ago
6.1.6
7 years ago
6.1.5
7 years ago
6.1.4
7 years ago
6.1.3
7 years ago
6.1.2
7 years ago
6.1.1
7 years ago
6.1.0
7 years ago
6.0.1
7 years ago
6.0.0
7 years ago
5.0.1
8 years ago
5.0.0
8 years ago
4.0.6
8 years ago
4.0.5
8 years ago
4.0.4
8 years ago
4.0.3
8 years ago
4.0.2
9 years ago
4.0.1
9 years ago
4.0.0
9 years ago
3.0.1
10 years ago
3.0.0
10 years ago
2.0.2
10 years ago