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 );
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 ( {} );
isBigInt
🆕
Checks if a value is a BigInt.
import {isBigInt} from 'is';
isBigInt ( 0n );
isBigInt ( 0 );
isBoolean
Checks if a value is a boolean.
import {isBoolean} from 'is';
isBoolean ( true );
isBoolean ( false );
isBoolean ( 0 );
isBuffer
Checks if a value is a Buffer.
import {isBuffer} from 'is';
isBuffer ( Buffer.from ( '' ) );
isBuffer ( [] );
isDate
Checks if a value is a Date.
import {isDate} from 'is';
isDate ( new Date () );
isDate ( 0 );
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' } );
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 );
isFunction
Checks if a value is a function.
import {isFunction} from 'is';
isFunction ( isFunction );
isFunction ( { call: () => {} } );
isInteger
Checks if a value is an integer.
import {isInteger} from 'is';
isInteger ( 0 );
isInteger ( -1 );
isInteger ( 1.2 );
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 );
isNil
Checks if a value is null or undefined.
import {isNil} from 'is';
isNil ( null );
isNil ( undefined );
isNil ( {} );
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' );
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 );
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' );
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 ( [] );
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 ( {} );
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 ( [] );
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 () );
isWeakSet
Checks if a value is a WeakSet.
import {isWeakSet} from 'is';
isWeakSet ( new WeakSet () );
isWeakSet ( new Set () );
License
- Parts: MIT © Fabio Spampinato.
- Parts: MIT © lodash.