5.0.0-rc.0

isClass()

isClass()

Checks if any value is a function type or the type obtained from its object class equal to 'function' and an instance of Function. It also confirms it's a class by checking whether the function converted with Function.prototype.toString() to string contains the word class at the beginning.

is-class.func.ts
const isClass = <Class = Function, Payload extends object = object>(
  value: any,
  callback: ResultCallback<any, Payload> = resultCallback,
  payload?: Payload
): value is Class =>
  callback(
    typeof value === 'function' ||
    (typeOf(value) === 'function' && value instanceof Function)
    ? /class/.test(Function.prototype.toString.call(value).slice(0, 5))
    : false,
    value,
    payload
  );

Generic type variables

Class=Function

The Class generic type variable indicates the class type of the given value via the return type, by default Function.

Payloadextendsobject=object

The Payload generic type variable constrained by object indicates the type of optional parameter payload of the supplied callback function and payload optional parameter of the isClass() function from which it captures its value.

Parameters

value: any

The value of any type to check.

callback: ResultCallback<any, Payload>

A callback function of ResultCallback type with parameters, the value that has been checked, the result of this check, and payload of generic type variable Payload with optional properties from the provided payload, to handle them before the result return. By default, it uses resultCallback() function.

payload?: Payload

An optional object of the generic type variable Payload is assigned to the payload of the given callback function.

Return type

value isClass

The return type is a boolean as the result of its statement indicating the value is a generic type variable Class by default Function.

Returns

The return value is a boolean indicating whether the provided value is a class.

Example usage

Basic example

// Example usage.
import { isClass } from '@angular-package/type';

class Class { x = 5; }
const FUNC = (x: number): any => x + 5;

isClass<Class>(Class); // Returns `true` as `value is Class`
isClass(FUNC); // Returns `false` as `value is Function`
isClass(() => 5); // Returns `false` as `value is Function`

Callback and payload parameters

// Callback and payload parameters example usage.
import { isClass } from '@angular-package/type';

isClass(() => 5, (result, value, payload) => {
  value // Returns `() => 5`
  if (payload) {
    result // Returns `false`
    payload.c // Returns `class Class`
  }
  return result;
}, { c: Class }); // Returns `false` as `value is Function`

Last updated