Is
The definitive collection of is* functions for runtime type checking. Realms support, tree-shakable, with types.
Every function provided accepts a single argument of unknown type and returns a boolean, almost always as a TypeScript type guard.
Install
npm install is@npm:@fabiospampinato/is
Functions
Usage
isArguments
Checks if a value is an arguments object.
import {isArguments} from 'is';
const args = (function () { return arguments; })();
isArguments ( args );
isArguments ( [] );
isArrayBuffer
Checks if a value is an ArrayBuffer object.
import {isArrayBuffer} from 'is';
isArrayBuffer ( new ArrayBuffer ( 8 ) );
isArrayBuffer ( [] );
isArrayLikeObject
Checks if a value is an Array-like object, meaning a non-string and non-function with an integer length
property.
import {isArrayLikeObject} from 'is';
isArrayLikeObject ( [] );
isArrayLikeObject ( { length: 1 } );
isArrayLikeObject ( 'foo' );
isArrayLikeObject ( isArrayLikeObject );
isArrayLike
Checks if a value is an Array-like object, meaning a non-function with an integer length
property.
import {isArrayLike} from 'is';
isArrayLike ( [] );
isArrayLike ( { length: 1 } );
isArrayLike ( 'foo' );
isArrayLike ( isArrayLike );
isArray
Checks if a value is an Array.
import {isArray} from 'is';
isArray ( [] );
isArray ( {} );
isArrowFunction
Checks if a value is an arrow function. There's a detectable difference between regular and arrow functions.
import {isArrowFunction} from 'is';
isArrowFunction ( () => {} );
isArrowFunction ( function () {} );
isAsyncFunction
Checks if a value is an async function. Note that this will return false for async generator functions.
import {isAsyncFunction} from 'is';
isAsyncFunction ( async () => {} );
isAsyncFunction ( () => {} );
isAsyncGeneratorFunction
Checks if a value is an async generator function.
import {isAsyncGeneratorFunction} from 'is';
isAsyncGeneratorFunction ( function* () {} );
isAsyncGeneratorFunction ( function () {} );
isAsyncIterable
Checks if a value is an async iterable.
import {isAsyncIterable} from 'is';
const myAsyncIterable = {
async * [Symbol.asyncIterator]() {
yield 'hello';
}
};
isAsyncIterable ( myAsyncIterable );
isAsyncIterable ( [] );
isAttribute
Checks if a value is likely a DOM attribute.
import {isAttribute} from 'is';
isAttribute ( document.createAttribute ( 'foo' ) );
isAttribute ( body );
isBigInt
Checks if a value is a bigint.
import {isBigInt} from 'is';
isBigInt ( 0n );
isBigInt ( 0 );
isBigInt64Array
Checks if a value is a BigInt64Array.
import {isBigInt64Array} from 'is';
isBigInt64Array ( new BigInt64Array () );
isBigInt64Array ( [] );
isBigUint64Array
Checks if a value is a BigUint64Array.
import {isBigUint64Array} from 'is';
isBigUint64Array ( new BigUint64Array () );
isBigUint64Array ( [] );
isBlob
Checks if a value is a Blob.
import {isBlob} from 'is';
isBlob ( new Blob ( [] ) );
isBlob ( [] );
isBoolean
Checks if a value is a boolean.
import {isBoolean} from 'is';
isBoolean ( true );
isBoolean ( false );
isBoolean ( 0 );
isBoundFunction
Checks if a value is a bound function.
import {isBoundFunction} from 'is';
isBoundFunction ( (function () {}).bind ( this ) );
isBoundFunction ( () => {} );
isBoundFunction ( function () {} );
isBoxedBigInt
Check if a value is a boxed bigint.
import {isBoxedBigInt} from 'is';
isBoxedBigInt ( 0n );
isBoxedBigInt ( Object ( 0n ) );
isBoxedBoolean
Check if a value is a boxed boolean.
import {isBoxedBoolean} from 'is';
isBoxedBoolean ( true );
isBoxedBoolean ( Object ( true ) );
isBoxedNumber
Check if a value is a boxed number.
import {isBoxedNumber} from 'is';
isBoxedNumber ( 0 );
isBoxedNumber ( Object ( 0 ) );
isBoxedPrimitive
Check if a value is a boxed primitive.
import {isBoxedPrimitive} from 'is';
isBoxedPrimitive ( 0 );
isBoxedPrimitive ( Object ( 0 ) );
isBoxedPrimitive ( Object ( 0n ) );
isBoxedString
Check if a value is a boxed string.
import {isBoxedString} from 'is';
isBoxedString ( 'foo' );
isBoxedString ( Object ( 'foo' ) );
isBoxedSymbol
Check if a value is a boxed symbol.
import {isBoxedSymbol} from 'is';
isBoxedSymbol ( Symbol () );
isBoxedSymbol ( Object ( Symbol () ) );
isBuffer
Checks if a value is a Buffer.
import {isBuffer} from 'is';
isBuffer ( Buffer.from ( '' ) );
isBuffer ( [] );
isClass
Checks if a value is an ES6 class. Note that classes lowered to work in ES5 are not actual classes anymore, there's a detectable difference when the class
keyword is used.
import {isClass} from 'is';
isClass ( class Foo {} );
isClass ( isClass );
Checks if a value is likely a DOM comment.
import {isComment} from 'is';
isComment ( document.createComment ( 'foo' ) );
isComment ( body );
isDataView
Checks if a value is a DataView.
import {isDataView} from 'is';
isDataView ( new DataView ( new ArrayBuffer ( 2 ) ) );
isDataView ( [] );
isDate
Checks if a value is a Date.
import {isDate} from 'is';
isDate ( new Date () );
isDate ( 0 );
isDefined
Checks if a value is not undefined, nor null.
import {isDefined} from 'is';
isDefined ( undefined );
isDefined ( null );
isDefined ( 0 );
isDocument
Checks if a value is likely a DOM document.
import {isDocument} from 'is';
isDocument ( document );
isDocument ( window );
isDocumentFragment
Checks if a value is likely a DOM document fragment.
import {isDocumentFragment} from 'is';
isDocumentFragment ( new DocumentFragment () );
isDocumentFragment ( document );
isDocumentType
Checks if a value is likely a DOM document type.
import {isDocumentType} from 'is';
isDocumentType ( document.doctype );
isDocumentType ( document );
isElement
Checks if a value is likely a DOM element.
import {isElement} from 'is';
isElement ( body );
isElement ( window );
isEmpty
Checks if a value is an empty array, string, buffer, typed array, arguments object, map, set, prototype or regular object.
import {isEmpty} from 'is';
isEmpty ( [] );
isEmpty ( {} );
isEmpty ( 123 );
isEmpty ( [123] );
isError
Checks if a value is an Error.
import {isError} from 'is';
isError ( new Error () );
isError ( { message: 'asd' } );
isEven
Checks if a value is an even integer.
import {isEven} from 'is';
isEven ( 2 );
isEven ( 1 );
isFalsy
Checks if a value is falsy.
import {isFalsy} from 'is';
isFalsy ( 0 );
isFalsy ( '' );
isFalsy ( [] );
isFinite
Checks if a value is a finite number.
import {isFinite} from 'is';
isFinite ( 0 );
isFinite ( Infinity );
isFinite ( -Infinity );
isFinite ( NaN );
isFloat
Checks if a value is a float.
import {isFloat} from 'is';
isFloat ( 1.2 );
isFloat ( 0 );
isFloat ( -1 );
isFloat32Array
Checks if a value is a Float32Array.
import {isFloat32Array} from 'is';
isFloat32Array ( new Float32Array () );
isFloat32Array ( [] );
isFloat64Array
Checks if a value is a Float64Array.
import {isFloat64Array} from 'is';
isFloat64Array ( new Float64Array () );
isFloat64Array ( [] );
isFrozen
Checks if a value is frozen.
import {isFrozen} from 'is';
isFrozen ( Object.freeze ( {} ) );
isFrozen ( {} );
isFunction
Checks if a value is a function.
import {isFunction} from 'is';
isFunction ( isFunction );
isFunction ( { call: () => {} } );
isGeneratorFunction
Checks if a value is a generator function. Note that this will return false for async generator functions.
import {isGeneratorFunction} from 'is';
isGeneratorFunction ( function* () {} );
isGeneratorFunction ( function () {} );
isInt8Array
Checks if a value is a Int8Array.
import {isInt8Array} from 'is';
isInt8Array ( new Int8Array () );
isInt8Array ( [] );
isInt16Array
Checks if a value is a Int16Array.
import {isInt16Array} from 'is';
isInt16Array ( new Int16Array () );
isInt16Array ( [] );
isInt32Array
Checks if a value is a Int32Array.
import {isInt32Array} from 'is';
isInt32Array ( new Int32Array () );
isInt32Array ( [] );
isInteger
Checks if a value is an integer.
import {isInteger} from 'is';
isInteger ( 0 );
isInteger ( -1 );
isInteger ( 1.2 );
isIterable
Checks if a value is an iterable.
import {isIterable} from 'is';
isIterable ( [] );
isIterable ( {} );
isLength
Checks if a value could be a valid length index.
import {isLength} from 'is';
isLength ( 0 );
isLength ( -1 );
isLength ( 1.2 );
isLength ( Infinity );
isMap
Checks if a value is a Map.
import {isMap} from 'is';
isMap ( new Map () );
isMap ( {} );
isNaN
Checks if a value is exactly NaN.
import {isNaN} from 'is';
isNaN ( NaN );
isNaN ( undefined );
isNative
Checks if a value is likely a native function.
import {isNative} from 'is';
isNative ( [].push );
isNative ( isNative );
isNegativeZero
Checks if a value is a negative zero, which if you didn't know is detectably different from a positive zero, for real.
import {isNegativeZero} from 'is';
isNegativeZero ( -0 );
isNegativeZero ( 0 );
isNil
Checks if a value is null or undefined.
import {isNil} from 'is';
isNil ( null );
isNil ( undefined );
isNil ( {} );
isNode
Checks if a value is likely a DOM node.
import {isNode} from 'is';
isNode ( document.body );
isNode ( undefined );
isNull
Checks if a value is null.
import {isNull} from 'is';
isNull ( null );
isNull ( undefined );
isNumber
Checks if a value is a number.
import {isNumber} from 'is';
isNumber ( 0 );
isNumber ( Infinity );
isNumber ( -Infinity );
isNumber ( NaN );
isNumber ( '0' );
isNumberLike
Checks if a string can be safely converted to a number.
import {isNumberLike} from 'is';
isNumberLike ( '3' );
isNumberLike ( '12.3' );
isNumberLike ( '1e100' );
isNumberLike ( '0xff' );
isNumberLike ( 'foo' );
isObjectLike
Checks if a value is an object (not necessarily a plain object).
import {isObjectLike} from 'is';
isObjectLike ( {} );
isObjectLike ( [] );
isObjectLike ( isObjectLike );
isObject
Checks if a value is not a primitive. This is the opposite of isPrimitive.
import {isObject} from 'is';
isObject ( {} );
isObject ( [] );
isObject ( isObject );
isObject ( 123 );
isOdd
Checks if a value is an odd integer.
import {isOdd} from 'is';
isOdd ( 1 );
isOdd ( 2 );
isPlainObject
Checks if a value is a plain object.
import {isPlainObject} from 'is';
isPlainObject ( {} );
isPlainObject ( [] );
isPlainObject ( isPlainObject );
isPrimitive
Checks if a value is a primitive. This is the opposite of isObject.
import {isPrimitive} from 'is';
isPrimitive ( null );
isPrimitive ( undefined );
isPrimitive ( '' );
isPrimitive ( 0 );
isPrimitive ( 0n );
isPrimitive ( true );
isPrimitive ( Symbol () );
isPrimitive ( {} );
isPrimitive ( isPrimitive );
isPromise
Checks if a value is a Promise.
import {isPromise} from 'is';
isPromise ( Promise.resolve () );
isPromise ( Promise.reject () );
isPromise ( { then: () => {} } );
isPrototype
Checks if a value is likely a prototype.
import {isPrototype} from 'is';
isPrototype ( Array.prototype );
isPrototype ( isPrototype );
isRegExp
Checks if a value is likely a regex.
import {isRegExp} from 'is';
isRegExp ( /x/ );
isRegExp ( new RegExp ( 'x' ) );
isRegExp ( 'x' );
isSealed
Checks if a value is sealed.
import {isSealed} from 'is';
isSealed ( Object.seal ( {} ) );
isSealed ( {} );
isSafeInteger
Checks if a value is an integer that can be represented faithfully in JavaScript.
import {isSafeInteger} from 'is';
isSafeInteger ( 0 );
isSafeInteger ( Number.MAX_SAFE_INTEGER );
isSafeInteger ( -Number.MAX_SAFE_INTEGER );
isSafeInteger ( Number.MAX_SAFE_INTEGER + 1 );
isSet
Checks if a value is a Set.
import {isSet} from 'is';
isSet ( new Set () );
isSet ( [] );
isSharedArrayBuffer
Checks if a value is a SharedArrayBuffer object.
import {isSharedArrayBuffer} from 'is';
isSharedArrayBuffer ( new SharedArrayBuffer ( 8 ) );
isSharedArrayBuffer ( [] );
isString
Checks if a value is a string.
import {isString} from 'is';
isString ( 'foo' );
isString ( ['f', 'o', 'o'] );
isSymbol
Checks if a value is a symbol.
import {isSymbol} from 'is';
isSymbol ( Symbol () );
isSymbol ( {} );
isText
Checks if a value is likely a DOM text.
import {isText} from 'is';
isText ( new Text ( 'foo' ) );
isText ( 'foo ); // => false
isTruthy
Checks if a value is truthy.
import {isTruthy} from 'is';
isTruthy ( [] );
isTruthy ( 0 );
isTruthy ( '' );
isTypedArray
Checks if a value is a TypedArray.
import {isTypedArray} from 'is';
isTypedArray ( new Int8Array ( 8 ) );
isTypedArray ( [] );
isUint8Array
Checks if a value is a Uint8Array.
import {isUint8Array} from 'is';
isUint8Array ( new Uint8Array () );
isUint8Array ( [] );
isUint8ClampedArray
Checks if a value is a Uint8ClampedArray.
import {isUint8ClampedArray} from 'is';
isUint8ClampedArray ( new Uint8ClampedArray () );
isUint8ClampedArray ( [] );
isUint16Array
Checks if a value is a Uint16Array.
import {isUint16Array} from 'is';
isUint16Array ( new Uint16Array () );
isUint16Array ( [] );
isUint32Array
Checks if a value is a Uint32Array.
import {isUint32Array} from 'is';
isUint32Array ( new Uint32Array () );
isUint32Array ( [] );
isUndefined
Checks if a value is undefined.
import {isUndefined} from 'is';
isUndefined ( undefined );
isUndefined ( null );
isWeakMap
Checks if a value is a WeakMap.
import {isWeakMap} from 'is';
isWeakMap ( new WeakMap () );
isWeakMap ( new Map () );
isWeakRef
Checks if a value is a WeakRef.
import {isWeakRef} from 'is';
isWeakRef ( new WeakRef ( WeakRef ) );
isWeakRef ( WeakRef ) );
isWeakReferable
Checks if a value can be held weakly, via WeakRef, WeakMap and WeakSet.
import {isWeakReferable} from 'is';
isWeakReferable ( {} );
isWeakReferable ( 123 ) );
isWeakSet
Checks if a value is a WeakSet.
import {isWeakSet} from 'is';
isWeakSet ( new WeakSet () );
isWeakSet ( new Set () );
isWindow
Checks if a value is the Window object.
import {isWindow} from 'is';
isWindow ( globalThis.window );
isWindow ( {} ) );
License
- Parts: MIT © Fabio Spampinato.
- Parts: MIT © lodash.