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

@baconsfoster/matches-with

v0.1.0

Published

Simple pattern matching

Readme

MatchesWith

A simple pattern matching library

yarn add @baconsfoster/matches-with

// or

npm install --save @baconsfoster/matches-with

Overview

Provides:

  • matchesWith : the pattern matching function
  • MatchesWith : A class with both a static and instance member assigned the pattern matching function
  • Any : A default-case predicate
  • isInstanceOf: A function that returns a predicate for a given class constructor
  • isTypeOf : A function that returns a predicate for a given string based on the typeof operator

Examples:

class ErrorA{}
class ErrorB{}

const isErrorA = isInstanceOf(ErrorA);
const isErrorB = isInstanceOf(ErrorB);

const isNumber = isTypeOf('number');
const isBoolean = isTypeOf('boolean');

try {
 await someFunctionThatCouldThrowManyDifferentErrors();
 await someOtherFunctionThatMightThrowAStringOrNumber();
} catch (e) {
 matchesWith(e, [
   [isErrorA, () => console.log('e is of type ErrorA')],
   [isErrorB, () => console.log('e is of type ErrorB')],
   [isNumber, () => console.log(`e is a number: ${e})`],
   [isBoolean, () => console.log('who throws booleans?')],
   [Any, () => console.log('this is the generic fallback handler')]
 ]);
}

Note that the order of predicates matters:

matchesWith(val, [
 [Any, () => console.log('`Any` matches anything, so the rest aren\'t tested')],
 [somethingElse, () => console.log('can never be reached')]
])

Note that matchesWith returns the value of invoking the matched clause. If no cases are matched, NO_MATCH is returned instead.

import { matchesWith, isTypeof, NO_MATCH } from '@baconsfoster/matchesWith';

const isString = isTypeOf('string');

const x = matchesWith(true, [
 [isString, () => 'a string was matched']
]);

x === NO_MATCH; // true

Inheriting and Mixing In matching

MatchesWith is a class that can be utilized for inheritance or mixing in. Examples:

class BaseClass extends MatchesWith {}

BaseClass.matchesWith(someVal, [
 // ... predicate clauses
]);

const test = new BaseClass();

test.matchesWith([
 // predicate clauses tested against the 'test' variable (i.e. self)
]);

This is mostly handy where you are operating on a sum type- say a value that is one of several child classes. For example:

class BaseClass extends MatchesWith{}
class ChildA extends BaseClass{}
class ChildB extends BaseClass{}

function returnChildAOrB() { /* returns either an instance of ChildA or ChildB */}

const test = returnChildAOrB();
test.matchesWith(test, [
 [isChildA, () => console.log('got child a')],
 [isChildB, () => console.log('got child b')]
]);

TypeScript Caveats and Using as a Mixin

Rather than inheriting from MatchesWith, which may not be practical, you can use implements in TypeScript to treat the class's public members as an interface; from there, you just copy them over. Example:

class B implements MatchesWith {
  static matchesWith = MatchesWith.matchesWith;

  matchesWith = MatchesWith.prototype.matchesWith;
}

This way, you can get both a parent from a different inheritance chain while "mixing in" the MatchesWith behavior, if so desired.