Skip to content

fabiospampinato/is

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

50 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Is

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

Features

  • Lodash-compatible: The provided functions are compatible with lodash's, so they can be used as drop-in replacements. This includes non-obvious things like being able to work with polyfilled builtins, not throwing in old environments where things like Map aren't available, working with objects from other realms, working with weird things like Boolean ( true ) and Object ( true ), and being isomorphic.
  • Types: Good TypeScript types ship with the library rather than being an afterthought. Also more subtlety this means that some extra functions like isFalsy and isTruthy are provided because their return type is useful for type checking, for lodash it doesn't make much sense to provide these functions because all their value is in the type system, and lodash doesn't care about that.
  • Modern: Some functions have been updated to account for bigints and some addititional ones have been added, the library is tree-shakable so you only pay for what you use.
  • Focused: Every function provided accepts a single argument of unknown type and returns a boolean, almost always as a TypeScript type guard.

Install

npm install --save is@npm:@fabiospampinato/is

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