Go Types
Type-checking utilities for JavaScript.
- version: 1.3.1
- license: MIT
Installation
npm i go-types
or
yarn add go-types
Usage
ES6
import Types from 'go-types'
Types.isArray([]);
or
import { isArray } from 'go-types';
isArray([]);
Node
const Types = require('go-types');
Types.isArray([]);
or
const { isArray } = require('go-types');
isArray([]);
Web browser
<script src="dist/go-types.min.js"></script>
<script>
const { isArray } = Types;
isArray([]);
</script>
Documentation
Table of Contents
- Primitive
- Utility
- Number
- Object
- Array
- Collection
- Node
Primitive
isUndefined
Determines whether the value is undefined
.
Parameters
-
value
any The value to check.
Examples
Undefined values
// returns true
isUndefined(undefined);
isUndefined(void 0);
Non-undefined values
// returns false
isUndefined(null);
isUndefined(false);
isUndefined(0);
isUndefined("");
isUndefined(NaN);
Returns boolean true
if the value is undefined
; false
otherwise.
Meta
- since: 1.0.0
isNull
Determines whether the value is null
.
Parameters
-
value
any The value to check.
Examples
Null values
// returns true
isNull(null);
Non-null values
// returns false
isNull(undefined);
isNull(false);
isNull(0);
isNull("");
isNull(NaN);
Returns boolean true
if the value is null
; false
otherwise.
Meta
- since: 1.0.0
isBoolean
- See: Types.isPrimitive
- See: Types.isObject
- See: Types.isTypeOf
Determines whether the value is a primitive boolean or Boolean
object.
Parameters
-
value
any The value to check.
Examples
Boolean values
// returns true
isBoolean(true);
isBoolean(new Boolean(true));
Non-boolean values
// returns false
isBoolean(1);
isBoolean("true");
Returns boolean true
if the value is a primitive boolean or Boolean
object; false
otherwise.
Meta
- since: 1.0.0
isNumber
- See: Types.isPrimitive
- See: Types.isObject
- See: Types.isTypeOf
Determines whether the value is a primitive number or Number
object
which includes the "special" numbers +Infinity
, -Infinity
and NaN
.
Parameters
-
value
any The value to check.
Examples
Number values
// returns true
isNumber(1);
isNumber(-12.34);
isNumber(new Number(1));
isNumber(Infinity);
isNumber(NaN);
Non-number values
// returns false
isNumber("1");
isNumber(true);
Returns boolean true
if the value is a primitive number or Number
object; false
otherwise.
Meta
- since: 1.0.0
isString
- See: Types.isPrimitive
- See: Types.isObject
- See: Types.isTypeOf
Determines whether the value is a primitive string or String
object.
Parameters
-
value
any The value to check.
Examples
String values
// returns true
isString("1");
isString(new String("abc"));
Non-string values
// returns false
isString(1);
isString(["a", "b"]);
Returns boolean true
if the value is a primitive string or String
object; false
otherwise.
Meta
- since: 1.0.0
isSymbol
- See: Symbol
Determines whether the value is a primitive Symbol
.
Parameters
-
value
any The value to check.
Examples
Symbol values
// returns true
isSymbol(Symbol("abc"));
Non-Symbol values
// returns false
isSymbol("abc");
isSymbol(/[a-z]/);
Returns boolean true
if the value is a primitive Symbol
; false
otherwise.
Meta
- since: 1.0.0
isBigInt
- See: BigInt
Determines whether the value is a primitive BigInt
.
Parameters
-
value
any The value to check.
Examples
BigInt values
// returns true
isBigInt(BigInt(1));
isBigInt(BigInt(-1234567890));
Non-BigInt values
// returns false
isBigInt(Number.MAX_VALUE);
isBigInt(Infinity);
isBigInt(NaN);
isBigInt("1n");
Returns boolean true
if the value is a primitive BigInt
; false
otherwise.
Meta
- since: 1.0.0
isPrimitive
Determines whether the value is a primitive value.
Parameters
-
value
any The value to check.
Examples
Primitive values
// returns true
isPrimitive(undefined);
isPrimitive(null);
isPrimitive(false);
isPrimitive(0);
isPrimitive("abc");
isPrimitive(Symbol("abc"));
isPrimitive(BigInt(1234567890));
Non-primitive values
// returns false
isPrimitive(new Number(1));
isPrimitive([1, 2, 3]);
isPrimitive(function () {});
Returns boolean true
if the value is a primitive value; false
otherwise.
Meta
- since: 1.0.0
Utility
isNullish
- See: undefined
- See: null
Determines whether the value is null
or undefined
.
Parameters
-
value
any The value to check.
Examples
Nullish values
// returns true
isNullish();
isNullish(undefined);
isNullish(void 0);
Non-nullish values
// returns false
isNullish(null);
isNullish(false);
isNullish(0);
isNullish("");
isNullish([]);
isNullish(NaN);
Returns boolean true
if the value is null
or undefined
; false
otherwise.
Meta
- since: 1.0.0
isEmpty
Determines whether the value is an empty value which can be one of the following:
null
undefined
- A string whose length is zero
- An array with no elements
- A plain object with no enumerable keys
- An iterable with no iterable values
Parameters
-
value
any The value to check.
Examples
Empty values
// returns true
isEmpty(null);
isEmpty(undefined);
isEmpty("");
isEmpty([]);
isEmpty({});
isEmpty(new Set());
Non-empty values
// returns false
isEmpty(" ");
isEmpty(0);
isEmpty(false);
isEmpty([null]);
isEmpty({a: ""});
isEmpty(NaN);
Returns boolean true
if the value is an empty value; false
otherwise.
Meta
- since: 1.0.0
isBlank
Determines whether the value is a blank value which can be one of the following:
null
undefined
- A string whose length is zero or has only space characters
- An array that has no elements or has only nullish values
- A plain object that has no enumerable keys or has only nullish values
- An iterable that has no iterable values or has only nullish values.
Parameters
-
value
any The value to check.
Examples
Blank values
// returns true
isBlank(null);
isBlank(undefined);
isBlank(" \t\n ");
isBlank([undefined, null]);
isBlank({a: null, b: undefined});
isBlank(new Set(null));
Non-blank values
// returns false
isBlank(0);
isBlank(false);
isBlank(" a ");
isBlank([""]);
isBlank({a: ""});
isBlank(NaN);
Returns boolean true
if the value is a blank value; false
otherwise.
Meta
- since: 1.0.0
isTruthy
Determines whether the value is a truthy value.
A truthy value is a value that is considered true when encountered in a Boolean context.
All values are truthy except false
, 0
, ""
, undefined
, null
and NaN
.
Parameters
-
value
any The value to check.
Examples
Truthy values
// returns true
isTruthy(true);
isTruthy(1);
isTruthy("false");
isTruthy(new Boolean(false));
isTruthy({});
Non-truthy values
// returns false
isTruthy(false);
isTruthy(0);
isTruthy("");
isTruthy(undefined);
isTruthy(null);
isTruthy(NaN);
Returns boolean true
if the value is a truthy value; false
otherwise.
Meta
- since: 1.0.0
isFalsy
Determines whether the value is a falsy value which can be one of the following:
false
0
""
undefined
null
NaN
Parameters
-
value
any The value to check.
Examples
Falsy values
// returns true
isFalsy(false);
isFalsy(0);
isFalsy("");
isFalsy(undefined);
isFalsy(null);
isFalsy(NaN);
Non-falsy values
// returns false
isFalsy(true);
isFalsy(-1);
isFalsy("false");
isFalsy(new Boolean(false));
isFalsy({});
Returns boolean true
if the value is a falsy value; false
otherwise.
Meta
- since: 1.0.0
isTypeOf
Determines whether the value has a specified type. Based on the type of the type value, it will check the following:
- string - checks if the type matches (case-sensitive) using the
typeof
operator. - function - checks if the value is an instance of the function/class using the
instanceof
operator; if the test returnsfalse
, it checks if the function returns a truthy value when invoked with the value. - null/undefined - checks if the value is equal to the type using the strict equality (
===
) operator. - array - checks if the value has any type defined in the array.
Parameters
Examples
Matching type values
// returns true
isTypeOf(1, "number");
isTypeOf({a: 1}, Object);
isTypeOf("abc", ["string", undefined]);
Non-matching type values
// returns false
isTypeOf(1, "boolean");
isTypeOf("", String);
isTypeOf(null, ["string", String]);
Returns boolean true
if the value is of the type; false
otherwise.
Meta
- since: 1.0.0
Number
isInteger
Determines whether the value is an integer (a primitive number or Number
object).
Parameters
-
value
any The value to check.
Examples
Integer values
// returns true
isInteger(1);
isInteger(new Number(1));
Non-integer values
// returns false
isInteger(isInteger(0.1));
isInteger("1");
isInteger(Infinity);
isInteger(NaN);
Returns boolean true
if the value is an integer; false
otherwise.
Meta
- since: 1.0.0
isFloat
Determines whether the value is a finite number with a decimal point (a primitive number or Number
object).
Parameters
-
value
any The value to check.
Examples
Float values
// returns true
isFloat(0.1);
isFloat(-1234.56789);
isFloat(new Number(0.1));
Non-float values
// returns false
isFloat(1);
isFloat(1.0);
isFloat("1.2");
Returns boolean true
if the value is a finite number with a decimal point; false
otherwise.
Meta
- since: 1.0.0
isFinite
- See: isFinite
Determines whether the value is a finite number (a primitive number or Number
object).
Parameters
-
value
any The value to check.
Examples
Finite values
// returns true
isFinite(0);
isFinite(-0.1);
isFinite(Number.MAX_VALUE);
Non-finite values
// returns false
isFinite(Infinity);
isFinite("1");
isFinite(NaN);
Returns boolean true
if the value is a finite number; false
otherwise.
Meta
- since: 1.0.0
isInfinite
- See: isFinite
Determines whether the value is a primitive number or Number
object that represents positive or negative infinity.
Parameters
-
value
any The value to check.
Examples
Infinite values
// returns true
isInfinite(Infinity);
isInfinite(Number.NEGATIVE_INFINITY);
Non-infinite values
// returns false
isInfinite(Number.MAX_VALUE);
isInfinite(NaN);
Returns boolean true
if the value is a number that represents positive or negative infinity; false
otherwise.
Meta
- since: 1.0.0
isNaN
- See: Number.isNaN
Determines whether the value is NaN
(a primitive number or a Number
object).
NaN is the only value in javascript that is not equal to itself.
Parameters
-
value
any The value to check.
Examples
NaN values
// returns true
isNaN(NaN);
isNaN(new Number(NaN));
isNaN(0/0);
Non-NaN values
// returns false
isNaN(undefined);
isNaN(null);
isNaN("NaN");
isNaN(Infinity);
Returns boolean true
if the value is NaN
; false
otherwise.
Meta
- since: 1.0.0
isNumeric
Determines whether the value is a numeric value which can be one of the following:
- A primitive number or
Number
object that has a finite value - A primitive string or
String
object that represents a finite number - A BigInt
Parameters
-
value
any The value to check.
Examples
Numeric values
// returns true
isNumeric(1);
isNumeric(new Number(1));
isNumeric("1.0");
isNumeric(new String("-1.234e+8"))
isNumeric(BigInt(1234567890));
isNumeric(Infinity);
Non-numeric values
// returns false
isNumeric(NaN);
isNumeric(Infinity);
isNumeric("12000n");
isNumeric("1*2");
Returns boolean true
if the value is a numeric value; false
otherwise.
Meta
- since: 1.0.0
Object
isObject
Determines whether the value is an Object
.
Parameters
-
value
any The value to check.
Examples
Object values
// returns true
isObject({a: 1, b: 2});
isObject(["a", "b"]);
isObject(new Object());
isObject(new Number(1));
isObject(function() {});
Non-object values
// returns false
isObject(null);
isObject("abc");
isObject(Symbol("abc"));
isObject(1);
isObject(BigInt(1));
Returns boolean true
if the value is an Object
; false
otherwise.
Meta
- since: 1.0.0
isPlainObject
Determines whether the value is a plain object created using object literal or by the Object
constructor.
Parameters
-
value
any The value to check.
Examples
Plain object values
// returns true
isPlainObject({a: 1, b: 2});
isPlainObject(new Object());
Non-plain-object values
// returns false
isPlainObject(null);
isPlainObject("abc");
isPlainObject([1, 2]);
isPlainObject(new Number(1));
Returns boolean true
if the value is a plain object; false
otherwise.
Meta
- since: 1.0.0
isCyclic
Determines whether the value is an cyclic object with a circular reference. A cycle object has an enumerable property (or a sub-property) whose value points to the object itself or a property of the object.
Parameters
-
value
any The value to check.
Examples
Cyclic object
// returns true
var obj = {};
obj.ref = obj;
isCyclic(obj);
Non-cyclic values
// returns false
isCyclic({a: 1, b: 2});
isCyclic([1, 2, 3]);
isCyclic("abc");
isCyclic(function() {});
Returns boolean true
if the value is a cyclic object with a circular reference; false
otherwise.
Meta
- since: 1.2.0
isIterable
- See: Iterable
Determines whether the value is iterable which can be used in a for..of
operator.
Parameters
-
value
any The value to check.
Examples
Iterable values
// returns true
isIterable([1, 2, 3]);
isIterable("abc");
isIterable(new Map());
isIterable(new Set());
isIterable({[Symbol.iterator]: function() {}});
Non-iterable values
// returns false
isIterable({a: 1, b: 2});
isIterable(12345);
Returns boolean true
if the value is iterable; false
otherwise.
Meta
- since: 1.0.0
isFunction
Determines whether the value is a Function
object.
Parameters
-
value
any The value to check.
Examples
Function values
// returns true
isFunction(function() {});
isFunction(Object);
Non-function values
// returns false
isFunction(null);
isFunction(new Object());
isFunction("function() {}");
Returns boolean true
if the value is a Function
object; false
otherwise.
Meta
- since: 1.0.0
isDate
- See: Date
Determines whether the value is a Date
object.
Parameters
-
value
any The value to check.
Examples
Date values
// returns true
isDate(new Date());
isDate(new Date(""));
Non-date values
// returns false
isDate("2001-01-01T00:00:00.000Z");
isDate(978307200000);
Returns boolean true
if the value is a Date
object; false
otherwise.
Meta
- since: 1.0.0
isValidDate
- See: Date
Determines whether the value is a valid Date
object.
Parameters
-
value
any The value to check.
Examples
Valid Date objects
// returns true
isValidDate(new Date());
isValidDate(new Date("2001-01-01T00:00:00.000Z"));
isValidDate(new Date(-1));
Invalid Date & non-date values
// returns false
isValidDate(new Date(""));
isValidDate("2001-01-01T00:00:00.000Z");
isValidDate(978307200000);
Returns boolean true
if the value is a valid Date
object; false
otherwise.
Meta
- since: 1.3.0
isInvalidDate
- See: Date
Determines whether the value is an invalid Date
object.
Parameters
-
value
any The value to check.
Examples
Invalid Date objects
// returns true
isInvalidDate(new Date(""));
isInvalidDate(new Date(NaN));
Valid Date & non-date values
// returns false
isInvalidDate(new Date());
isInvalidDate("2001-01-01T00:00:00.000Z");
isInvalidDate(978307200000);
Returns boolean true
if the value is an invalid Date
object; false
otherwise.
Meta
- since: 1.3.0
isRegExp
- See: RegExp
Determines whether the value is a RegExp
object.
Parameters
-
value
any The value to check.
Examples
RegExp values
// returns true
isRegExp(/[a-z]/);
isRegExp(new RegExp("[a-z]"));
Non-RegExp values
// returns false
isRegExp("[a-z]");
isRegExp("/[a-z]/");
Returns boolean true
if the value is a RegExp
object; false
otherwise.
Meta
- since: 1.0.0
isPromise
- See: Promise
Determines whether the value is a Promise
object.
Parameters
-
value
any The value to check.
Examples
Promise values
// returns true
isPromise(new Promise(resolve, reject));
isPromise(Promise.resolve());
isPromise(Promise.reject());
Non-promise values
// returns false
isPromise({then: function() {}, catch: function() {}});
Returns boolean true
if the value is a Promise
object; false
otherwise.
Meta
- since: 1.0.0
isPromiseLike
Determines whether the value is an object that defines then
and catch
methods.
Parameters
-
value
any The value to check.
Examples
Promise-like objects
// returns true
isPromiseLike(new Promise(resolve, reject));
isPromiseLike({then: function() {}, catch: function() {}});
Non-promise-like values
// returns false
isPromiseLike({then: function() {}});
isPromiseLike({then: true, catch: true});
Returns boolean true
if the value is a Promise-like object; false
otherwise.
Meta
- since: 1.0.0
isThenable
Determines whether the value is an object that defines a then
method.
Parameters
-
value
any The value to check.
Examples
Thenable objects
// returns true
isThenable(new Promise(resolve, reject));
isThenable({then: function() {}});
Non-thenable values
// returns false
isThenable(null);
isThenable({then: true});
Returns boolean true
if the value is a thenable object; false
otherwise.
Meta
- since: 1.1.0
isError
- See: Error
Determines whether the value is an Error
object.
Parameters
-
value
any The value to check.
Examples
Error objects
// returns true
isError(new Error("Validation error"));
isError(new TypeError("Expected a number, but found: " + value));
Non-Error values
// returns false
isError(null);
isError(NaN);
isError(Promise.reject());
isError({name: "TypeError", message: "Expected a number"});
Returns boolean true
if the value is an Error
object; false
otherwise.
Meta
- since: 1.0.0
Array
isArray
Determines whether the value is an Array
object.
Parameters
-
value
any The value to check.
Examples
Array values
// returns true
isArray(["a", "b"]);
isArray(new Array());
Non-array values
// returns false
isArray("abc");
isArray({length: 0});
isArray(new Int8Array());
Returns boolean true
if the value is an Array
object; false
otherwise.
Meta
- since: 1.0.0
isArrayOf
Determines whether the value is a non-empty array and all elements in the array have the specified type.
Parameters
-
value
any The value to check. -
type
(string | Function | null | undefined | Array) The type of elements
Examples
Matching values
// returns true
isArrayOf([1, 2, 3], "number");
isArrayOf(["a", "b", null], ["string", null]);
isArrayOf([{a: 1}, {b: 2}], Object);
Non-matching values
// returns false
isArrayOf([1, 2, "c"], "number");
isArrayOf([], undefined);
Returns boolean true
if the value is a non-empty array and and all elements in the array have the specified type; false
otherwise.
Meta
- since: 1.1.0
isArrayLike
Determines whether the value is an array-like object with a "length" property whose value is an integer and is within 0 ~ Number.MAX_SAFE_INTEGER (both inclusive).
Parameters
-
value
any The value to check.
Examples
Array-like objects
// returns true
isArrayLike([]);
isArrayLike("abc");
isArrayLike({length: 0});
isArrayLike(new Int8Array());
Non-array-like values
// returns false
isArrayLike({});
isArrayLike({length: -1})
isArrayLike(new Set());
isArrayLike(Array);
Returns boolean true
if the value is an array-like object; false
otherwise.
Meta
- since: 1.0.0
isArrayLikeOf
Determines whether the value is a non-empty array-like object and all elements in the object have the specified type.
Parameters
-
value
any The value to check. -
type
(string | Function | null | undefined | Array) The type of elements
Examples
Matching values
// returns true
isArrayLikeOf([1, 2, 3], "number");
isArrayLikeOf({0: "a", 1: "b", length: 2}, "string");
Non-matching values
// returns false
isArrayLikeOf([1, 2, "c"], "number");
isArrayLikeOf({a: 1, b: 2, length: 2}, "number");
Returns boolean true
if the value is a non-empty array-like object and all elements in the object have the specified type; false
otherwise.
Meta
- since: 1.1.0
isPair
Determines whether the value is an array with two elements.
Parameters
-
value
any The value to check.
Examples
Pair values
// returns true
isPair([1, 2]);
isPair([null, {}]);
Non-pair values
// returns false
isPair([]);
isPair({a: 1, b: 2});
isPair("ab");
Returns boolean true
if the value is an array with two elements; false
otherwise.
Meta
- since: 1.2.0
Collection
isMap
- See: Map
Determines whether the value is a Map
object.
Parameters
-
value
any The value to check.
Examples
Map values
// returns true
isMap(new Map());
Non-Map values
// returns false
isMap(new WeakMap());
isMap({a: 1, b: 2});
Returns boolean true
if the value is a Map
object; false
otherwise.
Meta
- since: 1.0.0
isSet
- See: Set
Determines whether the value is a Set
object.
Parameters
-
value
any The value to check.
Examples
Set values
// returns true
isSet(new Set());
Non-Set values
// returns false
isSet(new WeakSet());
isSet([1, 2, 3]);
Returns boolean true
if the value is a Set
object; false
otherwise.
Meta
- since: 1.0.0
isWeakMap
- See: WeakMap
Determines whether the value is a WeakMap
object.
Parameters
-
value
any The value to check.
Examples
WeakMap values
// returns true
isWeakMap(new WeakMap());
Non-WeakMap values
// returns false
isWeakMap(new Map());
isWeakMap({a: 1, b: 2});
Returns boolean true
if the value is a WeakMap
object; false
otherwise.
Meta
- since: 1.0.0
isWeakSet
- See: WeakSet
Determines whether the value is a WeakSet
object.
Parameters
-
value
any The value to check.
Examples
WeakSet values
// returns true
isWeakSet(new WeakSet());
Non-WeakSet values
// returns false
isWeakSet(new Set());
isWeakSet([1, 2, 3]);
Returns boolean true
if the value is a WeakSet
object; false
otherwise.
Meta
- since: 1.0.0
Node
isNode
- See: Node
Determines whether the value is a DOM Node
object.
Parameters
-
value
any The value to check.
Examples
Node objects
// returns true
isNode(document);
isNode(document.createElement("div"));
isNode(new DocumentFragment());
Non-Node values
// returns false
isNode(Node);
isNode(window);
Returns boolean true
if the value is a DOM Node
object; false
otherwise.
Meta
- since: 1.1.0
isElement
- See: Element
Determines whether the value is a DOM Element
object.
Parameters
-
value
any The value to check.
Examples
Element objects
// returns true
isElement(document.createElement("div"));
Non-Element values
// returns false
isElement(document);
isElement(window);
isElement(new DocumentFragment());
Returns boolean true
if the value is a DOM Element
object; false
otherwise.
Meta
- since: 1.1.0
isFragment
- See: DocumentFragment
Determines whether the value is a DocumentFragment
object.
Parameters
-
value
any The value to check.
Examples
DocumentFragment objects
// returns true
isFragment(new DocumentFragment());
Non-DocumentFragment values
// returns false
isFragment(document);
isFragment(window);
isFragment(document.createElement("div"));
Returns boolean true
if the value is a DocumentFragment
object; false
otherwise.
Meta
- since: 1.1.0