Skip to content

wizard04wsu/javascript-type-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 

Repository files navigation

Improved JavaScript Type Testing

A robust alternative to JavaScript's built-in type testing.

See the test page for examples.

Version 4 is not backwards compatible.


This module uses an expanded set of type names and related descriptors to simplify common tests of values. Basic types do not distinguish between primitives and objects, but descriptors primitive or object can be used to determine that aspect. For example, 5 and new Number(5) are both number, but 5 is primitive and new Number(5) is object.

The is() function returns an IsType object describing its argument.

Syntax:

is(value)

IsType Object

Member Description
.type The type of value (using this module's type names).
.of(class) Tests if value was an instance of class.
.all(...descriptors) Takes a list of descriptor names as arguments. Returns true if all of them applied to value.
.any(...descriptors) Takes a list of descriptor names as arguments. Returns true if any of them applied to value.
[descriptor] Descriptors are listed in the table below. Each descriptor property is a boolean that is true if it applied to value.

Enumerable properties of the is function are string values of the name of each descriptor. These can be used in the .all() and .any() methods instead of string literals. For example, is(value).all("number", "object") is equivalent to is(value).all(is.number, is.object).

Type Names and Related Descriptors

Descriptor Type Name Primitive Values Instances Of Classes
defined not undefined Object
undefined yes undefined
primitive not an instance of Object
object yes 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 falsy 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
real 0, 5 Number with a real 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 of length == 0, Map or Set of size == 0
nonempty not empty String, Array, Map, or Set that is not empty
date yes Date
error yes Error
function yes Function
promise yes Promise
regex yes Regex

Note

Note that JavaScript doesn't always treat mathematical expressions of undefined or indeterminate form as you might expect. For example, 1/0 is an undefined form, but JavaScript evaluates it as Infinity.