Skip to content

The definitive collection of is* functions for runtime type checking. Lodash-compatible, tree-shakable, with types.

License

Notifications You must be signed in to change notification settings

fabiospampinato/is

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Primitives Boxed Primitives Built-ins DOM Typed Array
isPrimitive isBoxedPrimitive isArray isAttribute isTypedArray
isBigInt isBoxedBigInt isArguments isComment isInt8Array
isBoolean isBoxedBoolean isArrayBuffer isDocument isInt16Array
isNil isBoxedNumber isArrayLike isDocumentFragment isInt32Array
isNull isBoxedString isAsyncIterable isDocumentType isUint8Array
isNumber isBoxedSymbol isBlob isElement isUint8ClampedArray
isString isBuffer isNode isUint16Array
isSymbol isClass isText isUint32Array
isUndefined isDataView isWindow isFloat32Array
isDate isFloat64Array
isError isBigInt64Array
isIterable isBigUint64Array
isMap
isNative
isPromise
isRegExp
isSet
isSharedArrayBuffer
isWeakMap
isWeakRef
isWeakSet
Function Number Object Others
isArrowFunction isEven isArrayLikeObject isDefined
isAsyncFunction isFinite isFrozen isEmpty
isAsyncGeneratorFunction isFloat isObject isFalsy
isBoundFunction isInteger isObjectLike isTruthy
isFunction isLength isPlainObject isWeakReferable
isGeneratorFunction isNaN isPrototype
isNegativeZero isSealed
isNumberLike
isOdd
isSafeInteger

Usage

isArguments

Checks if a value is an arguments object.

import {isArguments} from 'is';

const args = (function () { return arguments; })();

isArguments ( args ); // => true
isArguments ( [] ); // => false

isArrayBuffer

Checks if a value is an ArrayBuffer object.

import {isArrayBuffer} from 'is';

isArrayBuffer ( new ArrayBuffer ( 8 ) ); // => true
isArrayBuffer ( [] ); // => false

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 ( [] ); // => true
isArrayLikeObject ( { length: 1 } ); // => true
isArrayLikeObject ( 'foo' ); // => false
isArrayLikeObject ( isArrayLikeObject ); // => false

isArrayLike

Checks if a value is an Array-like object, meaning a non-function with an integer length property.

import {isArrayLike} from 'is';

isArrayLike ( [] ); // => true
isArrayLike ( { length: 1 } ); // => true
isArrayLike ( 'foo' ); // => true
isArrayLike ( isArrayLike ); // => false

isArray

Checks if a value is an Array.

import {isArray} from 'is';

isArray ( [] ); // => true
isArray ( {} ); // => false

isArrowFunction

Checks if a value is an arrow function. There's a detectable difference between regular and arrow functions.

import {isArrowFunction} from 'is';

isArrowFunction ( () => {} ); // => true
isArrowFunction ( function () {} ); // => false

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 () => {} ); // => true
isAsyncFunction ( () => {} ); // => false

isAsyncGeneratorFunction

Checks if a value is an async generator function.

import {isAsyncGeneratorFunction} from 'is';

isAsyncGeneratorFunction ( function* () {} ); // => true
isAsyncGeneratorFunction ( function () {} ); // => false

isAsyncIterable

Checks if a value is an async iterable.

import {isAsyncIterable} from 'is';

const myAsyncIterable = {
  async * [Symbol.asyncIterator]() {
    yield 'hello';
  }
};

isAsyncIterable ( myAsyncIterable ); // => true
isAsyncIterable ( [] ); // => false

isAttribute

Checks if a value is likely a DOM attribute.

import {isAttribute} from 'is';

isAttribute ( document.createAttribute ( 'foo' ) ); // => true
isAttribute ( body ); // => false

isBigInt

Checks if a value is a bigint.

import {isBigInt} from 'is';

isBigInt ( 0n ); // => true
isBigInt ( 0 ); // => false

isBigInt64Array

Checks if a value is a BigInt64Array.

import {isBigInt64Array} from 'is';

isBigInt64Array ( new BigInt64Array () ); // => true
isBigInt64Array ( [] ); // => false

isBigUint64Array

Checks if a value is a BigUint64Array.

import {isBigUint64Array} from 'is';

isBigUint64Array ( new BigUint64Array () ); // => true
isBigUint64Array ( [] ); // => false

isBlob

Checks if a value is a Blob.

import {isBlob} from 'is';

isBlob ( new Blob ( [] ) ); // => true
isBlob ( [] ); // => false

isBoolean

Checks if a value is a boolean.

import {isBoolean} from 'is';

isBoolean ( true ); // => true
isBoolean ( false ); // => true
isBoolean ( 0 ); // => false;

isBoundFunction

Checks if a value is a bound function.

import {isBoundFunction} from 'is';

isBoundFunction ( (function () {}).bind ( this ) ); // => true
isBoundFunction ( () => {} ); // => true
isBoundFunction ( function () {} ); // => false

isBoxedBigInt

Check if a value is a boxed bigint.

import {isBoxedBigInt} from 'is';

isBoxedBigInt ( 0n ); // => false
isBoxedBigInt ( Object ( 0n ) ); // => true

isBoxedBoolean

Check if a value is a boxed boolean.

import {isBoxedBoolean} from 'is';

isBoxedBoolean ( true ); // => false
isBoxedBoolean ( Object ( true ) ); // => true

isBoxedNumber

Check if a value is a boxed number.

import {isBoxedNumber} from 'is';

isBoxedNumber ( 0 ); // => false
isBoxedNumber ( Object ( 0 ) ); // => true

isBoxedPrimitive

Check if a value is a boxed primitive.

import {isBoxedPrimitive} from 'is';

isBoxedPrimitive ( 0 ); // => false
isBoxedPrimitive ( Object ( 0 ) ); // => true
isBoxedPrimitive ( Object ( 0n ) ); // => true

isBoxedString

Check if a value is a boxed string.

import {isBoxedString} from 'is';

isBoxedString ( 'foo' ); // => false
isBoxedString ( Object ( 'foo' ) ); // => true

isBoxedSymbol

Check if a value is a boxed symbol.

import {isBoxedSymbol} from 'is';

isBoxedSymbol ( Symbol () ); // => false
isBoxedSymbol ( Object ( Symbol () ) ); // => true

isBuffer

Checks if a value is a Buffer.

import {isBuffer} from 'is';

isBuffer ( Buffer.from ( '' ) ); // => true
isBuffer ( [] ); // => false

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 {} ); // => true
isClass ( isClass ); // => false

isComment

Checks if a value is likely a DOM comment.

import {isComment} from 'is';

isComment ( document.createComment ( 'foo' ) ); // => true
isComment ( body ); // => false

isDataView

Checks if a value is a DataView.

import {isDataView} from 'is';

isDataView ( new DataView ( new ArrayBuffer ( 2 ) ) ); // => true
isDataView ( [] ); // => false

isDate

Checks if a value is a Date.

import {isDate} from 'is';

isDate ( new Date () ); // => true
isDate ( 0 ); // => false

isDefined

Checks if a value is not undefined, nor null.

import {isDefined} from 'is';

isDefined ( undefined ); // => false
isDefined ( null ); // => false
isDefined ( 0 ); // => true

isDocument

Checks if a value is likely a DOM document.

import {isDocument} from 'is';

isDocument ( document ); // => true
isDocument ( window ); // => false

isDocumentFragment

Checks if a value is likely a DOM document fragment.

import {isDocumentFragment} from 'is';

isDocumentFragment ( new DocumentFragment () ); // => true
isDocumentFragment ( document ); // => false

isDocumentType

Checks if a value is likely a DOM document type.

import {isDocumentType} from 'is';

isDocumentType ( document.doctype ); // => true
isDocumentType ( document ); // => false

isElement

Checks if a value is likely a DOM element.

import {isElement} from 'is';

isElement ( body ); // => true
isElement ( window ); // => false

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 ( [] ); // => true
isEmpty ( {} ); // => true
isEmpty ( 123 ); // => true
isEmpty ( [123] ); // => false

isError

Checks if a value is an Error.

import {isError} from 'is';

isError ( new Error () ); // => true
isError ( { message: 'asd' } ); // => false

isEven

Checks if a value is an even integer.

import {isEven} from 'is';

isEven ( 2 ); // => true
isEven ( 1 ); // => false

isFalsy

Checks if a value is falsy.

import {isFalsy} from 'is';

isFalsy ( 0 ); // => true
isFalsy ( '' ); // => true
isFalsy ( [] ); // => false

isFinite

Checks if a value is a finite number.

import {isFinite} from 'is';

isFinite ( 0 ); // => true
isFinite ( Infinity ); // => false
isFinite ( -Infinity ); // => false
isFinite ( NaN ); // => false

isFloat

Checks if a value is a float.

import {isFloat} from 'is';

isFloat ( 1.2 ); // => true
isFloat ( 0 ); // => false
isFloat ( -1 ); // => false

isFloat32Array

Checks if a value is a Float32Array.

import {isFloat32Array} from 'is';

isFloat32Array ( new Float32Array () ); // => true
isFloat32Array ( [] ); // => false

isFloat64Array

Checks if a value is a Float64Array.

import {isFloat64Array} from 'is';

isFloat64Array ( new Float64Array () ); // => true
isFloat64Array ( [] ); // => false

isFrozen

Checks if a value is frozen.

import {isFrozen} from 'is';

isFrozen ( Object.freeze ( {} ) ); // => true
isFrozen ( {} ); // => false

isFunction

Checks if a value is a function.

import {isFunction} from 'is';

isFunction ( isFunction ); // => true
isFunction ( { call: () => {} } ); // => false

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* () {} ); // => true
isGeneratorFunction ( function () {} ); // => false

isInt8Array

Checks if a value is a Int8Array.

import {isInt8Array} from 'is';

isInt8Array ( new Int8Array () ); // => true
isInt8Array ( [] ); // => false

isInt16Array

Checks if a value is a Int16Array.

import {isInt16Array} from 'is';

isInt16Array ( new Int16Array () ); // => true
isInt16Array ( [] ); // => false

isInt32Array

Checks if a value is a Int32Array.

import {isInt32Array} from 'is';

isInt32Array ( new Int32Array () ); // => true
isInt32Array ( [] ); // => false

isInteger

Checks if a value is an integer.

import {isInteger} from 'is';

isInteger ( 0 ); // => true
isInteger ( -1 ); // => true
isInteger ( 1.2 ); // => false

isIterable

Checks if a value is an iterable.

import {isIterable} from 'is';

isIterable ( [] ); // => true
isIterable ( {} ); // => false

isLength

Checks if a value could be a valid length index.

import {isLength} from 'is';

isLength ( 0 ); // => true
isLength ( -1 ); // => false
isLength ( 1.2 ); // => false
isLength ( Infinity ); // => false

isMap

Checks if a value is a Map.

import {isMap} from 'is';

isMap ( new Map () ); // => true
isMap ( {} ); // => false

isNaN

Checks if a value is exactly NaN.

import {isNaN} from 'is';

isNaN ( NaN ); // => true
isNaN ( undefined ); // => false

isNative

Checks if a value is likely a native function.

import {isNative} from 'is';

isNative ( [].push ); // => true
isNative ( isNative ); // => false

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 ); // => true
isNegativeZero ( 0 ); // => false

isNil

Checks if a value is null or undefined.

import {isNil} from 'is';

isNil ( null ); // => true
isNil ( undefined ); // => true
isNil ( {} ); // => false

isNode

Checks if a value is likely a DOM node.

import {isNode} from 'is';

isNode ( document.body ); // => true
isNode ( undefined ); // => false

isNull

Checks if a value is null.

import {isNull} from 'is';

isNull ( null ); // => true
isNull ( undefined ); // => false

isNumber

Checks if a value is a number.

import {isNumber} from 'is';

isNumber ( 0 ); // => true
isNumber ( Infinity ); // => true
isNumber ( -Infinity ); // => true
isNumber ( NaN ); // => true
isNumber ( '0' ); // => false

isNumberLike

Checks if a string can be safely converted to a number.

import {isNumberLike} from 'is';

isNumberLike ( '3' ); // => true
isNumberLike ( '12.3' ); // => true
isNumberLike ( '1e100' ); // => true
isNumberLike ( '0xff' ); // => true
isNumberLike ( 'foo' ); // => false

isObjectLike

Checks if a value is an object (not necessarily a plain object).

import {isObjectLike} from 'is';

isObjectLike ( {} ); // => true
isObjectLike ( [] ); // => true
isObjectLike ( isObjectLike ); // => false

isObject

Checks if a value is not a primitive. This is the opposite of isPrimitive.

import {isObject} from 'is';

isObject ( {} ); // => true
isObject ( [] ); // => true
isObject ( isObject ); // => true
isObject ( 123 ); // => false

isOdd

Checks if a value is an odd integer.

import {isOdd} from 'is';

isOdd ( 1 ); // => true
isOdd ( 2 ); // => false

isPlainObject

Checks if a value is a plain object.

import {isPlainObject} from 'is';

isPlainObject ( {} ); // => true
isPlainObject ( [] ); // => false
isPlainObject ( isPlainObject ); // => false

isPrimitive

Checks if a value is a primitive. This is the opposite of isObject.

import {isPrimitive} from 'is';

isPrimitive ( null ); // => true
isPrimitive ( undefined ); // => true
isPrimitive ( '' ); // => true
isPrimitive ( 0 ); // => true
isPrimitive ( 0n ); // => true
isPrimitive ( true ); // => true
isPrimitive ( Symbol () ); // => true
isPrimitive ( {} ); // => false
isPrimitive ( isPrimitive ); // => false

isPromise

Checks if a value is a Promise.

import {isPromise} from 'is';

isPromise ( Promise.resolve () ); // => true
isPromise ( Promise.reject () ); // => true
isPromise ( { then: () => {} } ); // => false

isPrototype

Checks if a value is likely a prototype.

import {isPrototype} from 'is';

isPrototype ( Array.prototype ); // => true
isPrototype ( isPrototype ); // => false

isRegExp

Checks if a value is likely a regex.

import {isRegExp} from 'is';

isRegExp ( /x/ ); // => true
isRegExp ( new RegExp ( 'x' ) ); // => true
isRegExp ( 'x' ); // => false

isSealed

Checks if a value is sealed.

import {isSealed} from 'is';

isSealed ( Object.seal ( {} ) ); // => true
isSealed ( {} ); // => false

isSafeInteger

Checks if a value is an integer that can be represented faithfully in JavaScript.

import {isSafeInteger} from 'is';

isSafeInteger ( 0 ); // => true
isSafeInteger ( Number.MAX_SAFE_INTEGER ); // => true
isSafeInteger ( -Number.MAX_SAFE_INTEGER ); // => true
isSafeInteger ( Number.MAX_SAFE_INTEGER + 1 ); // => false

isSet

Checks if a value is a Set.

import {isSet} from 'is';

isSet ( new Set () ); // => true
isSet ( [] ); // => false

isSharedArrayBuffer

Checks if a value is a SharedArrayBuffer object.

import {isSharedArrayBuffer} from 'is';

isSharedArrayBuffer ( new SharedArrayBuffer ( 8 ) ); // => true
isSharedArrayBuffer ( [] ); // => false

isString

Checks if a value is a string.

import {isString} from 'is';

isString ( 'foo' ); // => true
isString ( ['f', 'o', 'o'] ); // => false

isSymbol

Checks if a value is a symbol.

import {isSymbol} from 'is';

isSymbol ( Symbol () ); // => true
isSymbol ( {} ); // => false

isText

Checks if a value is likely a DOM text.

import {isText} from 'is';

isText ( new Text ( 'foo' ) ); // => true
isText ( 'foo ); // => false

isTruthy

Checks if a value is truthy.

import {isTruthy} from 'is';

isTruthy ( [] ); // => true
isTruthy ( 0 ); // => false
isTruthy ( '' ); // => false

isTypedArray

Checks if a value is a TypedArray.

import {isTypedArray} from 'is';

isTypedArray ( new Int8Array ( 8 ) ); // => true
isTypedArray ( [] ); // => false

isUint8Array

Checks if a value is a Uint8Array.

import {isUint8Array} from 'is';

isUint8Array ( new Uint8Array () ); // => true
isUint8Array ( [] ); // => false

isUint8ClampedArray

Checks if a value is a Uint8ClampedArray.

import {isUint8ClampedArray} from 'is';

isUint8ClampedArray ( new Uint8ClampedArray () ); // => true
isUint8ClampedArray ( [] ); // => false

isUint16Array

Checks if a value is a Uint16Array.

import {isUint16Array} from 'is';

isUint16Array ( new Uint16Array () ); // => true
isUint16Array ( [] ); // => false

isUint32Array

Checks if a value is a Uint32Array.

import {isUint32Array} from 'is';

isUint32Array ( new Uint32Array () ); // => true
isUint32Array ( [] ); // => false

isUndefined

Checks if a value is undefined.

import {isUndefined} from 'is';

isUndefined ( undefined ); // => true
isUndefined ( null ); // => false

isWeakMap

Checks if a value is a WeakMap.

import {isWeakMap} from 'is';

isWeakMap ( new WeakMap () ); // => true
isWeakMap ( new Map () ); // => false

isWeakRef

Checks if a value is a WeakRef.

import {isWeakRef} from 'is';

isWeakRef ( new WeakRef ( WeakRef ) ); // => true
isWeakRef ( WeakRef ) ); // => false

isWeakReferable

Checks if a value can be held weakly, via WeakRef, WeakMap and WeakSet.

import {isWeakReferable} from 'is';

isWeakReferable ( {} ); // => true
isWeakReferable ( 123 ) ); // => false

isWeakSet

Checks if a value is a WeakSet.

import {isWeakSet} from 'is';

isWeakSet ( new WeakSet () ); // => true
isWeakSet ( new Set () ); // => false

isWindow

Checks if a value is the Window object.

import {isWindow} from 'is';

isWindow ( globalThis.window ); // => true, in a browser environment
isWindow ( {} ) ); // => false

License

  • Parts: MIT © Fabio Spampinato.
  • Parts: MIT © lodash.

About

The definitive collection of is* functions for runtime type checking. Lodash-compatible, tree-shakable, with types.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published