This is a library of commonly used validation logic for Typescript.
Every function has an inverse, for example check_is_string and
check_is_not_string.
| Data type | Library |
|---|---|
| string | @checker/string |
| number | @checker/number |
| boolean | @checker/boolean |
| nullish | @checker/nullish |
| array | @checker/array |
| object | @checker/object |
Examples:
@checker/string
import{check_is_string}from"@checker/string";typeValue=string|null;letvalue: Value=null;if(someCondition){value="hello";}if(check_is_string(value)){value;// value is Hello and this type is string}
import{check_is_index_found}from"@checker/string";constwords="hello world";constindex=words.indexOf("hello");if(check_is_index_found(index)){// When index is not -1}
import{check_is_empty_string}from"@checker/string";constvalue=" ";consttrimmed=value.trim();if(check_is_empty_string(trimmed)){// When value is empty string}
@checker/number
import{check_is_number,check_is_odd}from"@checker/number";constarray=["a",1,true,2,3,4,5];constodds=array.filter(check_is_number).filter(check_is_odd);// odds = [1, 3, 5]
@checker/boolean
import{check_is_truthy}from"@checker/boolean";constarray=[null,undefined,0,1,,"","hello",true];constfiltered=array.filter(check_is_truthy);// [1, 'hello', true]
@checker/nullish
import{check_is_not_nullish}from"@checker/nullish";constarray=[null,undefined,0,1,'','hello'true];constfiltered=array.filter(check_is_not_nullish);// [0, 1, '', 'hello', true]
@checker/array
Checks if the given index is the last iteration in the array.
import{check_is_last_iteration}from"@checker/array";constarray=[1,2,3];constmapped=array.map((item,index)=>{if(check_is_last_iteration(index,array)){returnitem+1;}returnitem;});// mapped = [1, 2, 4]
@checker/object
Checks if the given index is not the last iteration in the array.
import{check_is_empty_object}from"@checker/object";typeValue=Partial<{[key: string]: boolean}>;constvalue: Value={};if(someCondition){value["key"]=true;}if(check_is_empty_object(value)){// When value is empty object}
import{check_is_instance}from"@checker/object";try{// some action}catch(err){if(check_is_instance(err,DatabaseError)){// err is DatabaseError instance}}
All library licence is MIT.