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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@azvaliev/match

v0.1.1

Published

Rust-style pattern matching brought to TypeScript

Readme

Match

Everything that switch could've been. 🚀
Inspired from Rust. 🦀

pnpm install @azvaliev/match

Introduction

match is a typesafe pattern matching library, designed to make writing and reading conditional logic simple, concise, and extensible. You can think of it like everything switch could've been.

Simply put, goal of this library is to make your conditional logic simpler, more typesafe, and easier to use.

Side Benefits

  • MIT Licensed
  • Zero dependencies
  • TypeScript is optional, but well integrated
  • Works in both NodeJS and the browser
  • Under 3kb when minified and gzipped

Getting Started / Installation

First, install with package manager of choice

npm install @azvaliev/match
# OR
yarn add @azvaliev/match
# OR
pnpm install @azvaliev/match

Then, import as needed!

import { match } from '@azvaliev/match';

match(myString, [
  ['hello world', () => {
    console.log('Matched!');
  }],
  () => {
    console.log('default');
  }
]);

Basic Usage

The match function takes two arguments, your value and an array of matchers.

Each matcher is a tuple containing a [Pattern, MatchHandler] except for the last value in the array which is just a MatchHandler, outside a tuple, serving as a default case.

When the first Pattern that matches your value is found, the corresponding MatchHandler will be executed. If none are matching, the last MatchHandler will executed.

match(
  Value, 
  [...Array<[Pattern, MatchHandler]>, MatchHandler],
)

Value

string | number | boolean

Whatever value you want to pattern match against.

Pattern

Given a certain type for the Value, this shows the corresponding Pattern types available.

Value: number

  • number
  • Array<number>
  • Set<number>
  • Exclusive Range* "start..end"
  • Inclusive Range* "start..=end"
  • Greater than ">number"
  • Less than "<number"

Value: string

  • string
  • Array<string>
  • Set<string>
  • RegExp

Value: boolean

  • true
  • false

* exclusive range meaning the high number is not included, inclusve range meaning the high number is included

MatchHandler

(val: string | number | boolean) => unknown

A callback function which recieves your value as the first and only parameter. It can can optionally return any value which will be passed through and returned from match.

Return Values

The return type from match() is a union of all its different MatchHandler return types.

const result = match(someString, [
  // first MatchHandler returns 'a'
  ['foo', () => 'a'],

  // second MatchHandler returns 'b' 
  ['bar', () => 'b'],

  // default case MatchHandler returns 'c'
  () => 'c'
]);

result; // 'a' | 'b' | 'c'

Should TypeScript fail to infer the return type properly (or not specific enough), you can also specify this explicitly in the generic constraint.

// TypeScript will still validate this generic constraint is met,
// via the return types of your MatchHandler(s)
const result = match<string, 'x' | 'y' | 'z'>(someString, [/* ... */]);

result; // 'x' | 'y' | 'z'

Error Handling

Philosophy

Generally, match will not throw exceptions, and prefer logging them under console.error. There are two main reasons that match will throw an exception for.

  1. Missing a default case handler. TypeScript will warn you about this. The default case handler is required, even if it's just an empty function.
  2. Unsupported type supplied. match only works with strings, numbers, and booleans as of this time. Expect supplying any other types to throw an error.

MatchError

match will only ever throw errors that are instances of MatchError. An error can be easily identified by checking it's status property.

try {
  match(someValue, [/* ... */])
} catch (err) {
  if (err instanceof MatchError) {
    err.name; // "MatchError"
    err.status; // member of enum MatchError.StatusCodes
  }
}