Package Exports
- go-types
- go-types/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (go-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Introduction
A JavaScript type-checking library.
- version: 1.1.0
- author: Michael Ko <koyote130708@gmail.com>
If you find this library useful, please consider donating. Thank you. :)
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="Types.js"></script>
<script>
const { isArray } = Types;
isArray([]);
</script>
Documentation
isUndefined
Determines whether the argument is undefined
.
Parameters
arg
any The argument 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 argument is undefined
; false
otherwise.
Meta
- since: 1.0.0
isNull
Determines whether the argument is null
.
Parameters
arg
any The argument 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 argument is null
; false
otherwise.
Meta
- since: 1.0.0
isBoolean
- See: Types.isPrimitive
- See: Types.isObject
- See: Types.isTypeOf
Determines whether the argument is a primitive boolean or Boolean
object.
Note: To check exclusively, use with isPrimitive
or isObject
respectively or use isTypeOf
instead.
Parameters
arg
any The argument 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 argument 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 argument is a primitive number or Number
object which includes the "special" numbers +Infinity
, -Infinity
and NaN
.
Note: To check exclusively, use with isPrimitive
or isObject
respectively or use isTypeOf
instead.
Parameters
arg
any The argument 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 argument 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 argument is a primitive string or String
object.
Note: To check exclusively, use with isPrimitive
or isObject
respectively or use isTypeOf
instead.
Parameters
arg
any The argument 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 argument is a primitive string or String
object; false
otherwise.
Meta
- since: 1.0.0
isPrimitive
Determines whether the argument is a primitive value.
Parameters
arg
any The argument 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 argument is a primitive value; false
otherwise.
Meta
- since: 1.0.0
isObject
Determines whether the argument is an object.
Parameters
arg
any The argument 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 argument is an object; false
otherwise.
Meta
- since: 1.0.0
isPlainObject
Determines whether the argument is a plain object created using object literal or new Object()
.
Parameters
arg
any The argument 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 argument is a plain object; false
otherwise.
Meta
- since: 1.0.0
isFunction
Determines whether the argument is a function.
Parameters
arg
any The argument 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 argument is a function; false
otherwise.
Meta
- since: 1.0.0
isArray
Determines whether the argument is an array.
Parameters
arg
any The argument 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 argument is an array; false
otherwise.
Meta
- since: 1.0.0
isArrayLike
Determines whether the argument is an array-like object with a "length" property.
Parameters
arg
any The argument 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(new Set());
isArrayLike(Array);
Returns boolean true
if the argument is an array-like object; false
otherwise.
Meta
- since: 1.0.0
isArrayOf
Determines whether the argument is an array of the specified type and has at least one element.
Parameters
arg
any The argument to checktype
(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 argument is an array and and all elements in the array are of the specified type; false
otherwise.
Meta
- since: 1.1.0
isArrayLikeOf
Determines whether the argument is an array-like object of the specified type and has at least one element.
Parameters
arg
any The argument to checktype
(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 argument is an array-like object and all elements in the array are of the specified type; false
otherwise.
Meta
- since: 1.1.0
isDate
- See: Date
Determines whether the argument is a Date
object.
Parameters
arg
any The argument 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 argument is a Date
object; false
otherwise.
Meta
- since: 1.0.0
isRegExp
- See: RegExp
Determines whether the argument is a RegExp
object.
Parameters
arg
any The argument 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 argument is a RegExp
object; false
otherwise.
Meta
- since: 1.0.0
isPromise
- See: Promise
Determines whether the argument is a Promise
object.
Parameters
arg
any The argument 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 argument is a Promise
object; false
otherwise.
Meta
- since: 1.0.0
isPromiseLike
Determines whether the argument is an object that defines then
and catch
methods.
Parameters
arg
any The argument 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 argument is a Promise-like object; false
otherwise.
Meta
- since: 1.0.0
isThenable
Determines whether the argument is an object that defines a then
method.
Parameters
arg
any The argument 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 argument is a thenable object; false
otherwise.
Meta
- since: 1.1.0
isSymbol
- See: Symbol
Determines whether the argument is a Symbol
.
Parameters
arg
any The argument 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 argument is a Symbol
; false
otherwise.
Meta
- since: 1.0.0
isBigInt
- See: BigInt
Determines whether the argument is a BigInt
value.
Parameters
arg
any The argument 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 argument is a BigInt
value; false
otherwise.
Meta
- since: 1.0.0
isMap
- See: Map
Determines whether the argument is a Map
object.
Parameters
arg
any The argument 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 argument is a Map
object; false
otherwise.
Meta
- since: 1.0.0
isSet
- See: Set
Determines whether the argument is a Set
object.
Parameters
arg
any The argument 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 argument is a Set
object; false
otherwise.
Meta
- since: 1.0.0
isWeakMap
- See: WeakMap
Determines whether the argument is a WeakMap
object.
Parameters
arg
any The argument 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 argument is a WeakMap
object; false
otherwise.
Meta
- since: 1.0.0
isWeakSet
- See: WeakSet
Determines whether the argument is a WeakSet
object.
Parameters
arg
any The argument 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 argument is a WeakSet
object; false
otherwise.
Meta
- since: 1.0.0
isIterable
- See: Iterable
Determines whether the argument is iterable. An iterable implements the Symbol.iterator
method and can be used in a for..of
operator.
Parameters
arg
any The argument 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 argument is iterable; false
otherwise.
Meta
- since: 1.0.0
isError
- See: Error
Determines whether the argument is an Error
object.
Parameters
arg
any The argument to check
Examples
Error objects
// returns true
isError(new Error("Validation error"));
isError(new TypeError("Expected a number, but found: " + arg));
Non-Error values
// returns false
isError(null);
isError(NaN);
isError(Promise.reject());
isError({name: "TypeError", message: "Expected a number"});
Returns boolean true
if the argument is an Error
object; false
otherwise.
Meta
- since: 1.0.0
isNode
- See: Node
Determines whether the argument is a DOM Node
object.
Parameters
arg
any The argument 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 argument is a DOM Node
object; false
otherwise.
Meta
- since: 1.1.0
isElement
- See: Element
Determines whether the argument is a DOM Element
object.
Parameters
arg
any The argument 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 argument is a DOM Element
object; false
otherwise.
Meta
- since: 1.1.0
isFragment
- See: DocumentFragment
Determines whether the argument is a DocumentFragment
object.
Parameters
arg
any The argument 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 argument is a DocumentFragment
object; false
otherwise.
Meta
- since: 1.1.0
isNullish
- See: undefined
- See: null
Determines whether the argument is null
or undefined
.
Parameters
arg
any The argument 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 argument is null
or undefined
; false
otherwise.
Meta
- since: 1.0.0
isTruthy
Determines whether the argument 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
arg
any The argument 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 argument is a truthy value; false
otherwise.
Meta
- since: 1.0.0
isFalsy
Determines whether the argument is a falsy value which can be one of the following:
false
0
""
undefined
null
NaN
Parameters
arg
any The argument 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 argument is a falsy value; false
otherwise.
Meta
- since: 1.0.0
isEmpty
Determines whether the argument is an empty value which can be one of the following:
null
undefined
- An empty string whose length is zero
- An empty array whose length is zero
- An empty object which has no enumerable keys
Parameters
arg
any The argument to check
Examples
Empty values
// returns true
isEmpty(null);
isEmpty(undefined);
isEmpty("");
isEmpty([]);
isEmpty({});
Non-empty values
// returns false
isEmpty(" ");
isEmpty(0);
isEmpty(false);
isEmpty([null]);
isEmpty({a: ""});
isEmpty(NaN);
Returns boolean true
if the argument is an empty value; false
otherwise.
Meta
- since: 1.0.0
isBlank
Determines whether the argument is a blank value which can be one of the following:
null
undefined
- A blank string which is empty or has only space characters
- A blank array which has no elements or has only undefined or null values
- A blank object which has no enumerable keys or has only undefined or null values
Parameters
arg
any The argument to check
Examples
Blank values
// returns true
isBlank(null);
isBlank(undefined);
isBlank(" \t\n ");
isBlank([undefined, null]);
isBlank({a: null, b: undefined})
Non-blank values
// returns false
isBlank(0);
isBlank(false);
isBlank(" a ");
isBlank([""]);
isBlank({a: ""});
isBlank(NaN);
Returns boolean true
if the argument is a blank value; false
otherwise.
Meta
- since: 1.0.0
isNumeric
Determines whether the argument is a numeric value which can be one of the following:
- Primitive number or
Number
object that has a finite value - Primitive string or
String
object that represents a finite number - BigInt
Parameters
arg
any The argument 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 argument is a numeric value; false
otherwise.
Meta
- since: 1.0.0
isInteger
Determines whether the argument is an integer (a primitive number or Number
object).
Parameters
arg
any The argument 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 argument is an integer; false
otherwise.
Meta
- since: 1.0.0
isSafeInteger
- See: Number.isSafeInteger
Determines whether the argument is a safe integer (a primitive number or Number
object).
A safe integer is an integer that:
- can be exactly represented as an IEEE-754 double precision number, and
- whose IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.
Parameters
arg
any The argument to check
Examples
Safe integer values
// returns true
isSafeInteger(1);
isSafeInteger(Number.MAX_SAFE_INTEGER);
isSafeInteger(Number.MIN_SAFE_INTEGER);
Non-safe-integer values
// returns false
isSafeInteger(0.1);
isSafeInteger("1");
isSafeInteger(Number.MIN_SAFE_INTEGER - 1);
isSafeInteger(NaN);
Returns boolean true
if the argument is a safe integer; false
otherwise.
Meta
- since: 1.1.0
isFloat
Determines whether the argument is a primitive number or Number
object whose value is with a decimal point.
Parameters
arg
any The argument to check
Examples
Decimal values
// returns true
isFloat(0.1);
isFloat(-1234.56789);
isFloat(new Number(0.1));
Non-decimal values
// returns false
isFloat(1);
isFloat(1.0);
isFloat("1.2");
Returns boolean true
if the argument is a number with a decimal point; false
otherwise.
Meta
- since: 1.0.0
isFinite
- See: isFinite
Determines whether the argument is a primitive number or Number
object whose value is a finite number.
Parameters
arg
any The argument 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 argument is a finite number; false
otherwise.
Meta
- since: 1.0.0
isInfinite
- See: isFinite
Determines whether the argument is a primitive number or Number
object that represents positive or negative infinity.
Parameters
arg
any The argument 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 argument is a number that represents positive or negative infinity; false
otherwise.
Meta
- since: 1.0.0
isNaN
- See: Number.isNaN
Determines whether the argument is a primitive number or a Number
object whose value is NaN
.
NaN is the only value in javascript that is not equal to itself.
Parameters
arg
any The argument 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 argument is NaN
; false
otherwise.
Meta
- since: 1.0.0
isTypeOf
Determines whether the argument is the specified type. Based on the type value, it will perform the following:
- string - checks if the type matches (case-sensitive) using the
typeof
operator. - function - checks if the argument is an instance of the function/class using the
instanceof
operator. - null/undefined - checks if the argument is equal to the type using the strict equality (
===
) operator. - array - checks if there is any matching type in the array by calling this function with the same argument and each type value.
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 argument is of the type; false
otherwise.
Meta
- since: 1.0.0