powercheck
Straightforward, zero-dependencies and powerful utility to check types, instances or values. Takes away the dirt, lets you write more solid code and drastically improves code readability.
npm install powercheck --save
Usage
Basic
; ; // -> true ; // -> false ; // -> undefined ; // throws an exception
Optional value
; // -> true ; // -> true
Or: import "optional" using ES6 modules
; ; // -> true
Instance checking
; // -> true ; // -> true
Equality checking
; // -> false
OneOf-checking
; // -> false ; // -> true ; // -> true ; // -> true
Every-checking
; // -> true ; // -> false
N.B.: validations will be executed in order. When a validation fails, remaining validations won't be executed. So, in is.validate()
functions (custom validations), you can rely on the previous validations.
Validation function
; // -> true
; ; // -> true
Array literal
; // -> true ; // -> false
N.B.: it's recursive
; // -> true
Object literal
; // -> true ; // -> false
N.B.: extra properies won't be accepted
is({
foo: 'bar',
baz: 'kopz',
extra: 'boo'
}, {
foo: String,
baz: String
});
// -> false
N.B.: mising properies won't be accepted
; // -> false
Throw exceptions instead
; ; // -> false (boolean) ; // -> throws an exception ; // -> throws a custom error ; // -> throws a custom error with extra information ; // -> undefined (no exception has been thrown, nothing is being returned either) ; // -> undefined (no exception has been thrown, nothing is being returned either)
API
is(value, validator)
is()
returns a boolean and check()
throws an exception.
Key | Value |
---|---|
value | Anything you want to check the type, instance and/or value for |
validator | String , Number , Object , Array , Function , Symbol (ES6), SomeConstructor , is.optional(<validator>) , is.validate((value) => trueOrFalse) , is.equals(compareValue) , is.oneOf([validators]) , [<validator>] , {key1: <validator>, ...} |
Questions
If you have questions, you can ask them on StackOverflow mentioning me @jessedvrs.