A robust alternative to type testing in vanilla JavaScript. Uses an expanded set of type names to simplify common tests.
See the test page for examples.
Version 4 is not backwards compatible.
is(value)
The return value is an object that describes the argument.
Member | Description |
---|---|
.type | The type name of value. |
.of(class) | Tests if value is an instance of class. |
.all(...descriptors) | Takes descriptor names as arguments. Returns true if all of them apply to value. |
.any(...descriptors) | Takes descriptor names as arguments. Returns true if any of them apply to value. |
[descriptor] | Each descriptor property is a boolean that is true if it applies to value. |
Enumerable properties of is
are string constants of all the descriptor names. These can be used
in the .all()
and .any()
methods instead of string literals.
For example, these are equivalent:
is(value).all("number", "object")
is(value).all(is.number, is.object)
Types in this module do not distinguish between primitives and objects. For example, 5
and new Number(5)
are both of type "number".
A descriptor is a boolean value that is true
if it applies to the value being tested. Type names are included as descriptors. For example, 5
is associated with the primitive, number, and finite descriptors, among others.
Descriptor | Type Name | Primitive Values | Instances Of Classes |
---|---|---|---|
defined | not undefined | Object |
|
undefined | yes | undefined | |
primitive | any primitive value | ||
object | yes1 | Object |
|
objectish | null |
Object |
|
null | yes | null |
|
nullish | undefined, null |
||
boolean | yes | false , true |
|
false | false |
||
true | true |
||
falsy | undefined, null , false , 0n , NaN , 0 , "" |
||
truthy | not a falsy value | Object |
|
symbol | yes | a Symbol |
|
bigint | yes | 0n , 5n |
|
numberish | 0 , 5 , Infinity , NaN |
Number |
|
nan | yes | NaN |
Number with value NaN |
number | yes | 0 , 5 , Infinity |
Number excluding those with value NaN |
real2 | 0 , 5 |
Number with a finite number value |
|
finite | 0 , 5 |
Number with a finite number value |
|
infinite | Infinity |
Number with an infinite value |
|
string | yes | "" , "foo" |
String |
array | yes | [] , [1,2] |
Array |
map | yes | Map |
|
set | yes | Set |
|
weakmap | yes | WeakMap |
|
weakset | yes | WeakSet |
|
empty | "" , [] |
String or Array with .length === 0 ,Map or Set with .size === 0 |
|
nonempty | "foo" , [1,2] |
String or Array with .length > 0 ,Map or Set with .size > 0 |
|
date | yes | Date |
|
error | yes | Error |
|
function | yes | Function |
|
promise | yes | Promise |
|
regex | yes | Regex |