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

x-match-expression

v0.2.1

Published

Javascript Pattern matching library developed in typescript

Downloads

1,158

Readme

Overview

x-match-expression is a javascript pattern matching library developed in typescript that can be used standalone in the browser, in node or as ES module.

Installation

  • Install it with npm install --save x-match-expression or reference it from CDN in the browser

Usage

In the browser (with a script tag)

  • Add a script tag <script src="https://unpkg.com/x-match-expression/dist/browser/index.js" type="application/javascript"></script>
  • And later on, use the global match function
 const financialStatus = match(42)
    .caseGreaterEqualThan(1000000, "I'm rich!")
    .caseLessThan(0, "I'm ruined")
    .default("I keep trying");
 console.log(financialStatus); // outputs I keep trying

In Node

const {match} = require("x-match-expression");

const isEven = match(2)
    .case(n => n % 2 === 0, true)
    .default(false);
 console.log(isEven); // outputs true

as ES Module

import {match} from "x-match-expression";

const areWeInTheFuture = match(new Date())
    .caseNewerThan(new Date(2050, 0, 1), true)
    .default(false);
 console.log(areWeInTheFuture); // outputs false

API

If you are using typescript or an IDE with autocompletion, just write match. and explore all the options available. Basically you have cases for every javascript primitive type + instance checks. Almost every case method has a companion method ended in "If"; This allows to put an extra predicate if the case is not enough. To end the match expression use default method.

Advanced Usage

Instance check example

import {match} from "x-match-expression";

class FatalError {
    constructor(readonly id: string, readonly date: Date) {}
}

class Warning {
    constructor(readonly title: string, readonly message: string) {}
}

class MailMessage {
    constructor(readonly sender: string, readonly subject: string, readonly message: string) {}
}

type Whatever =
    FatalError |
    Warning |
    MailMessage;

const info = getInfo(); // returns type Whatever

const infoDetails = match(info)
    .caseInstanceIf(FatalError, _ => _.id === 404 => `No content found at ${e.date.toISOString()}`)
    .caseInstance(FatalError, _ => `Error #${_.id} received`)
    .caseInstance(Warning, _ => `Warning ${_.title}: ${_.message}`)
    .caseInstance(MailMessage, _ => `You received a message from ${_.sender}`)
    .default("Unknown information received");

Float comparison

import {match} from "x-match-expression";

const a = 0.2;
const b = 0.4;
const c = 0.6;
const whatHappens = match(a+b)
    .caseEqual(c, "This number is 0.6")
    .caseAlmostEqual(c, "The number is not exactly 0.6, but it is very close") // <-- this will match
    .default("This number is not 0.6 at all");

Custom test that fails

import {match} from "x-match-expression";

function isSomething(x) {
    throw "Error!"; // <-- look at this
}

const value = match(x)
    .case(isSomething, "This is something") // <-- custom tests doesn't raise exceptions, they simple return false
    .default("This is nothing");

With union type as return

import {match} from "x-match-expression";

const twice: number | string = match("hello world")
    .caseString(s => `${s}${s}`)
    .caseNumber(n => n*2)
    .default(0);