Check Complex Types "check-complex-types"
Checks if variable has specific type. It can be used for primitives but its power lies in complex types like interfaces and in optional logic.
install
npm install check-complex-types
usage
; const testedInterface = types; testedInterface;// equal false testedInterface;// equal true
API
Types
There are number of types:
ANY
for any type
; typesANY;// equal truetypesANY;// equal truetypesANY;// equal truetypesANY;// equal true
STRING or STRING.args([conditions])
for string primitives and string objects
; typesSTRING;// equal truetypesSTRING;// equal false
string conditions
minLength: positive number
Check if string length is bigger then minLength
value
; typesSTRING;// equal truetypesSTRING;// equal false
maxLength: positive number
Check if string length is shorter then maxLength
value
; typesSTRING;// equal truetypesSTRING;// equal false
equal: string
Check if string is equal to defined value
; typesSTRING;// equal truetypesSTRING;// equal false
match: string
Check if string match regexp
; typesSTRING;// equal truetypesSTRING;// equal false
ARRAY([elementType]) or ARRAY([elementType]).args([conditions])
checks if value is array. If elementType
is provided it also checks if every array element has specified type.
; typesARRAY;// equal falsetypesARRAY;// equal true types;// equal falsetypes;// equal true
array conditions
minLength: positive number
Check if array length is bigger then minLength
value
; typesARRAY;// equal truetypesARRAY;// equal false
maxLength: positive number
Check if array length is shorter then maxLength
value
; typesARRAY;// equal truetypesARRAY;// equal false
NUMBER or NUMBER.args([conditions])
for number primitive and number object
; typesNUMBER;// equal truetypesNUMBER;// equal false
number conditions
equal: string
Check if number is equal to defined value
; typesNUMBER;// equal truetypesNUMBER;// equal false
minValue: string
Check if number is greater than defined value
; typesNUMBER;// equal truetypesNUMBER;// equal false
maxValue: string
Check if number is lower than defined value
; typesNUMBER;// equal falsetypesNUMBER;// equal true
OBJECT
for objects with exception for null and primitive wrappers (primitive objects like Number, String, Boolean)
FUNCTION
for functions
BOOLEAN
for boolean primitive and boolean object
SYMBOL
for symbols
UNDEFINED
for undefined
NULL
for null
OPTIONAL(optionalType)
check if value has specific type optionalType
or doesn't exist (is undefined)
; types;// equal truetypes;// equal true
INTERFACE(interface)
for checking if tested object has interface properties with correct types interface is an object with property names and description
example
; const someObject = {} someString: "test" someValue: 1234 someObject: someInnerString: "inner" ; types;// equal true
INSTANCE(class)
check if argument instance of class
SOME(arrayOfTypes)
for checking if value has one or more types from array of types arrayOfTypes
; types;// equal truetypes;// equal false
EVERY(arrayOfTypes)
for checking if value match all types in array of types arrayOfTypes
; types;// equal false types; // equal true
NOT(type)
Accept different type and return true if provided type is false.
; types;// equal falsetypes;// equal true
NONE(arrayOfTypes)
for checking if value not match any types in array of types arrayOfTypes
; types;// equal falsetypes;// equal true
Types methods
Each type can be used as function to pass additional properties or uses as is. Each type has methods
test(testedValue): boolean
Method is used to check if testedValue match specific type. It will return true if it match or false otherwise.
Example
We would like to check if specific value is a string
; const someValue = "test"; typesSTRING;// equal true
args(conditions)
Some types have additional conditions like min/max length and it can be set by this method.
It returns object with method test
.
typesSTRING;// equal true
More complex example
We would like to check if tested object has some specific properties with specific types
; const someObject = {} someString: "test" someValue: 1234 someObject: someInnerString: "inner" ; types;// equal true
License
MIT