is-what

  • Version 5.0.2
  • Published
  • 33.5 kB
  • No dependencies
  • MIT license

Install

npm i is-what
yarn add is-what
pnpm add is-what

Overview

JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.

Index

Functions

function getType

getType: (payload: unknown) => string;
  • Returns the object type of the given payload

function isAnyObject

isAnyObject: (payload: unknown) => payload is PlainObject;
  • Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)

function isArray

isArray: (payload: unknown) => payload is unknown[];
  • Returns whether the payload is an array

function isBlob

isBlob: (payload: unknown) => payload is Blob;
  • Returns whether the payload is a Blob

function isBoolean

isBoolean: (payload: unknown) => payload is boolean;
  • Returns whether the payload is a boolean

function isDate

isDate: (payload: unknown) => payload is Date;
  • Returns whether the payload is a Date, and that the date is valid

function isEmptyArray

isEmptyArray: (payload: unknown) => payload is [];
  • Returns whether the payload is a an empty array

function isEmptyObject

isEmptyObject: (
payload: unknown
) => payload is { [x: string]: never; [x: number]: never; [x: symbol]: never };
  • Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)

function isEmptyString

isEmptyString: (payload: unknown) => payload is string;
  • Returns whether the payload is ''

function isError

isError: (payload: unknown) => payload is Error;
  • Returns whether the payload is an Error

function isFile

isFile: (payload: unknown) => payload is File;
  • Returns whether the payload is a File

function isFullArray

isFullArray: (payload: unknown) => payload is unknown[];
  • Returns whether the payload is a an array with at least 1 item

function isFullObject

isFullObject: (payload: unknown) => payload is PlainObject;
  • Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)

function isFullString

isFullString: (payload: unknown) => payload is string;
  • Returns whether the payload is a string, BUT returns false for ''

function isFunction

isFunction: (payload: unknown) => payload is AnyFunction;
  • Returns whether the payload is a function (regular or async)

function isInstanceOf

isInstanceOf: {
<T extends AnyClass>(value: unknown, class_: T): value is T;
<K extends GlobalClassName>(
value: unknown,
className: K
): value is (typeof globalThis)[K];
(value: unknown, className: string): value is object;
};
  • Checks if a value is an instance of a class or a class name. Useful when you want to check if a value is an instance of a class that may not be defined in the current scope. For example, if you want to check if a value is an OffscreenCanvas instance, you might not want to do the song and dance of using typeof OffscreenCanvas !== 'undefined' and then shimming OffscreenCanvas if the types aren't around.

    Parameter value

    The value to recursively check

    Parameter class_

    A string or class that the value should be an instance of

    Example 1

    if (isInstanceOf(value, 'OffscreenCanvas')) { // value is an OffscreenCanvas }

function isMap

isMap: (payload: unknown) => payload is Map<unknown, unknown>;
  • Returns whether the payload is a Map

function isNaNValue

isNaNValue: (payload: unknown) => payload is number;
  • Returns whether the payload is literally the value NaN (it's NaN and also a number)

function isNegativeNumber

isNegativeNumber: (payload: unknown) => payload is number;
  • Returns whether the payload is a negative number (but not 0)

function isNull

isNull: (payload: unknown) => payload is null;
  • Returns whether the payload is null

function isNullOrUndefined

isNullOrUndefined: (payload: unknown) => payload is null;
  • Returns true whether the payload is null or undefined

function isNumber

isNumber: (payload: unknown) => payload is number;
  • Returns whether the payload is a number (but not NaN)

    This will return false for NaN!!

function isObject

isObject: (payload: unknown) => payload is PlainObject;
  • Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)

function isObjectLike

isObjectLike: <T extends PlainObject>(payload: unknown) => payload is T;
  • Returns whether the payload is an object like a type passed in < >

    Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an id prop.

    T This must be passed in < >

function isOneOf

isOneOf: {
<A, B extends A, C extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>): TypeGuard<
A,
B | C
>;
<A, B extends A, C extends A, D extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>,
c: TypeGuard<A, D>
): TypeGuard<A, B | C | D>;
<A, B extends A, C extends A, D extends A, E extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>,
c: TypeGuard<A, D>,
d: TypeGuard<A, E>
): TypeGuard<A, B | C | D | E>;
<A, B extends A, C extends A, D extends A, E extends A, F extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>,
c: TypeGuard<A, D>,
d: TypeGuard<A, E>,
e: TypeGuard<A, F>
): TypeGuard<A, B | C | D | E | F>;
};
  • A factory function that creates a function to check if the payload is one of the given types.

    Example 1

    import { isOneOf, isNull, isUndefined } from 'is-what'

    const isNullOrUndefined = isOneOf(isNull, isUndefined)

    isNullOrUndefined(null) // true isNullOrUndefined(undefined) // true isNullOrUndefined(123) // false

function isPlainObject

isPlainObject: (payload: unknown) => payload is PlainObject;
  • Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)

function isPositiveNumber

isPositiveNumber: (payload: unknown) => payload is number;
  • Returns whether the payload is a positive number (but not 0)

function isPrimitive

isPrimitive: (payload: unknown) => payload is string | number | boolean | symbol;
  • Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String

    | Symbol)

function isPromise

isPromise: (payload: unknown) => payload is Promise<unknown>;
  • Returns whether the payload is a Promise

function isRegExp

isRegExp: (payload: unknown) => payload is RegExp;
  • Returns whether the payload is a regular expression (RegExp)

function isSet

isSet: (payload: unknown) => payload is Set<unknown>;
  • Returns whether the payload is a Set

function isString

isString: (payload: unknown) => payload is string;
  • Returns whether the payload is a string

function isSymbol

isSymbol: (payload: unknown) => payload is symbol;
  • Returns whether the payload is a Symbol

function isType

isType: <T extends AnyFunction | AnyClass>(
payload: unknown,
type: T
) => payload is T;
  • Does a generic check to check that the given payload is of a given type. In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!); It will, however, differentiate between object and null

    Throws

    {TypeError} Will throw type error if type is an invalid type

function isUndefined

isUndefined: (payload: unknown) => payload is undefined;
  • Returns whether the payload is undefined

function isWeakMap

isWeakMap: (payload: unknown) => payload is WeakMap<WeakKey, unknown>;
  • Returns whether the payload is a WeakMap

function isWeakSet

isWeakSet: (payload: unknown) => payload is WeakSet<WeakKey>;
  • Returns whether the payload is a WeakSet

Type Aliases

type AnyAsyncFunction

type AnyAsyncFunction = (...args: unknown[]) => Promise<unknown>;

    type AnyClass

    type AnyClass = new (...args: unknown[]) => unknown;

      type AnyFunction

      type AnyFunction = (...args: any[]) => any;

        type PlainObject

        type PlainObject = {
        [key in string | number | symbol]: unknown;
        };

          Package Files (38)

          Dependencies (0)

          No dependencies.

          Dev Dependencies (6)

          Peer Dependencies (0)

          No peer dependencies.

          Badge

          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/is-what.

          • Markdown
            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/is-what)
          • HTML
            <a href="https://www.jsdocs.io/package/is-what"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>