npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

zoomatch

v1.2.1

Published

A small Typescript class for convenient pattern matching

Readme

Zoomatch

Zoomatch is a small pattern matching class written in Typescript. It draws inspiration from the excellent ts-pattern, though the two have rather different APIs. Zoomatch will probably be more convenient for matching primitives, while ts-pattern has more features for working with objects.

Installation

npm install zoomatch

Basic Usage

match is a factory function that returns an instance of Match<T, U> (in most cases) or a MatchNoArg<U> (described near the end). T is the type of the value being matched, and U is the type of the result.

import { match } from "zoomatch";

function getWaterPhase(
  temperatureC: number
): "solid" | "liquid" | "gas" {
  return match(temperature)
    .when((x) => x < 0, "solid")
    .when((x) => x > 100, "gas")
    .otherwise("liquid");
}

getWaterPhase(40); // "liquid"
getWaterPhase(120); // "gas"
getWaterPhase(-20); // "solid"

The last method must always be otherwise. The when method returns not the result itself, but another Match instance. Only the otherwise method returns the actual result.

You may also use functions of the matched value rather than constants for the result.

function calculateTotalCost(units: number): number {
  return match(units)
    .when(
      (x) => x >= 1000,
      (x) => x * 2
    )
    .when(
      (x) => x >= 500,
      (x) => x * 2.5
    )
    .otherwise((x) => x * 3);
}

calculateTotalCost(2000); // 2000 * 2 = 4000
calculateTotalCost(700); // 700 * 2.5 = 1750
calculateTotalCost(300); // 300 * 3 = 900

Notice that the order of conditions matters in the example above. Zoomatch stops looking after the first match it finds, so calculateTotalCost(2000) returned the result from the first when method, even though the following condition was also true.

Other Matching Techniques

The predicates of the when function (and most of its variants described below) can be either boolean function or constants. Using a constant is a convenience shortcut, where CONSTANT is equivalent to (x) => x === CONSTANT.

const classification = match(legs)
  .when(4, "quadriped")
  .when(8, "arachnid")
  .when(2, "biped")
  .when((x) => x > 8, "scary bug")
  .otherwise("unknown");

There are convenience methods for matching any, none, or all of a set of conditions. These methods accept an array of conditions as the first parameter. The whenAny and whenNone methods can use constants and/or boolean functions as the conditions, just as the basic when method does. The whenAll method, however, accepts only boolean functions since including constants would presumably be a logic error.

const triangle = match<{A: number, B: number: C:number}, string>(angles)
  .when((a) => a.A + a.B + a.C !== 180, "not a triangle")
  .whenAny([(a) => a.A === 90, (a) => a.B === 90, (a) => a.C === 90], 'right')
  .whenNone([(a) => a.A === a.B, (a) => a.B === a.C, (a) => a.A === a.C], 'scalene')
  .whenAll([(a) => a.A === 60, (a) => a.B === 60, (a) => a.C === 60], 'equilateral')
  .otherwise("isosceles");

The asIs Function

When the result is the same as the original value (or the intermediate value for wheWithFn), you can use the covnenience function asIs instead of (x) => x.

import { match, asIs } from "zoomatch";

function clamp(x, min, max): number {
  return match(x)
    .when((x) => x < min, min)
    .when((x) => x > max, max)
    .otherwise(asIs);
}
match(number);

clamp(5, 0, 100); // 5
clamp(500, 0, 100); // 100
clamp(-5, 0, 100); // 0

Transformations with whenWithFn

The whenWithFn method applies function fn (specified in the first argument) and uses its output both to evaluate the condition and to calculate the result. This can allow for some less repetitive code, and also removes the need to perform potentially expensive operations twice.

// assume that a function sum exists to sum the array elements
function tablesNeeded(groupSizes: number[]): string {
  return match(groupSizes)
    .whenWithFn(
      sum,
      (s) => s > 100,
      (s) => `The fire marshall will not allow ${s} people.`
    )
    .whenWithFn(
      sum,
      (s) => s > 0,
      (s) => `${Math.ceil(s / 10)} tables needed`
    )
    .otherwise("Sorry, nobody is coming.");
}

tablesNeeded([20, 30, 10, 50]); // 'The firs marshall will not allow 110 people'
tablesNeeded([10, 10, 25]); // '5 tables needed'

Streamlined Syntax With Expressions (New in version 1.2)

There are some circumstances in which you may find it better to use an alternate form of match which take no arguments in the constructor and uses boolean expressions rather than functions or constants. If you supply no arguments to match, the factory function, will return an instance of MatchNoArg<U> instead of the Match<T, U> instance that applied in the prior examples. With the MatchNoArg class, the when and related methods use boolean expressions instead of functions to determine if a condition passes, and the result will also be a expression rather than a function to be evaluated.

This form is designed for cases in which the conditions involved refer to a large number of variables, where it might be cumbersome to create an ad-hoc object to be the arguments for all of the conditions in your when, whenAny, etc. methods. The more attached you are to a functional(ish) style, the less likely you are to prefer this form, but it can be easier to read in some cases.

const a = 10;
const b = 20;
const c = 30;

let x = 5;

const result = match()
  .when(x === a, 50) // nope
  .whenAll([x > 0, b > a, x > c], 100) // nope, only 2 of 3
  .whenNone([x % 2 === 0, b - a > x], 200) // nope, 2nd passes
  .whenAny([x % 2 === 0, c === 20], x + 300) // yes, one passes
  .otherwise(400);
// result === 305

The underlying class for this form does not have a whenWithFn method since there is no argument to be transformed.

License

MIT (see license.md in this repository)