matchigo - v1.0.2
    Preparing search index...

    Function match

    • Data-driven dispatch — matches value against each rule in order and runs the first rule whose with matches. For the hot path, hoist rules to module scope; the compiled dispatcher is cached on the array identity, so inline rebuilds miss the cache (and trigger a dev warning).

      Type Parameters

      • T
      • R

      Parameters

      • value: T
      • rules: readonly Rule<T, R>[]

      Returns R

      import { P, match, type Rule } from "matchigo";

      type Event = { kind: "click"; x: number } | { kind: "key"; key: string };

      const RULES: ReadonlyArray<Rule<Event, string>> = [
      { with: { kind: "click" }, then: (e) => `click@${e.x}` },
      { with: { kind: "key", key: "Escape" }, then: () => "escape" },
      { with: { kind: "key" }, then: (e) => `key:${e.key}` },
      ];

      match({ kind: "key", key: "a" } as Event, RULES); // "key:a"