Skip to content

unifiers matchInstanceOf()

Eugene Lazutkin edited this page Apr 3, 2026 · 3 revisions

Matches values based on instanceof checks.

Introduction

This unifier matches values against constructors using the instanceof operator. Can match a single constructor or multiple constructors.

import matchInstanceOf from 'deep6/unifiers/matchInstanceOf.js';
import unify from 'deep6/unify.js';

unify(new Date(), matchInstanceOf(Date)); // truthy
unify([], matchInstanceOf(Array)); // truthy
unify([], matchInstanceOf([Array, Set, Map])); // truthy

API

matchInstanceOf(types)

Arguments:

  • types — a required constructor function or array of constructor functions to check against.

Returns a MatchInstanceOf unifier instance.

Class: MatchInstanceOf

Extends Unifier.

Properties

  • types — array of constructors to match against.

Methods

  • unify(val, ls, rs) — checks if val instanceof Type for any type.
    • Returns true if val is an instance of any configured type.
    • Returns false if val is falsy, a Variable, or not an instance.

Examples

import matchInstanceOf from 'deep6/unifiers/matchInstanceOf.js';
import {match} from 'deep6';

// Match Date instances
match(new Date(), matchInstanceOf(Date)); // true
match('2024-01-01', matchInstanceOf(Date)); // false

// Match collection types
const isCollection = matchInstanceOf([Array, Set, Map]);
match([], isCollection); // true
match(new Set(), isCollection); // true
match({}, isCollection); // false

// Match custom classes
class User {}
class Admin {}
const isUserType = matchInstanceOf([User, Admin]);
match(new User(), isUserType); // true
match(new Admin(), isUserType); // true

Clone this wiki locally