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

ts-match-case

v0.0.3

Published

matchCase, function like php match() structure

Readme

ts-match-case

A small and simple function to create match/case style branching in TypeScript and JavaScript.
Replaces long if/else or switch statements with a more declarative and type-safe approach.

Best way to simulate PHP match structure


<?php
    $food = 'cake';

    $return_value = match ($food) {
        'apple' => 'This food is an apple',
        'bar' => 'This food is a bar',
        'cake' => 'This food is a cake',
    };

    var_dump($return_value);
?>
// see in https://www.php.net/manual/en/control-structures.match.php

Installation

npm install ts-match-case

or

yarn add ts-match-case

Basic Usage

import { matchCase } from "ts-match-case";

const result = matchCase<number, string>(2, {
    1: () => "one",
    2: () => "two",
    defaultCase: () => "unknown"
});

console.log(result); // "two"

Supported cases formats

You can pass cases in three formats:

  1. Object JSON
import { matchCase } from "ts-match-case"

const myFavoriteHero = "Dragonfly"

const witchIsYourFavoriteHero =  matchCase<string, string>(myFavoriteHero, {
    "SpiderMan" : () => "It's spider man",
    "Batman" : () => "It's Batman",
    "Dragonfly" : () => "It´s Dragonfly"
});

console.log(witchIsYourFavoriteHero); // Dragonfly

With default case

import { defaultCase, matchCase } from "ts-match-case"

const car = "Tesla";

// using
const result = matchCase<string, string>(car, {
    'Tesla': () => "Electric luxury",
    'Ferrari': () => "Super fast",
    'Toyota': () => "Reliable",
    defaultCase: () => "Unknown car" // defaultCase is "defaultCase" string value
});

console.log(result); // Electric luxury
  1. Array of pairs
import { defaultCase, matchCase } from "ts-match-case"

const myFavoriteCartoon = "Care Bears";

const result = matchCase<string, string>(
    myFavoriteCartoon,
    [
        ["Tom and Jerry", () => "Classic slapstick comedy"],
        ["Scooby-Doo", () => "Mystery-solving fun"],
        ["Care Bears", () => "Heartwarming adventures"],
        [defaultCase, () => "Unknown cartoon"] // or ["defaultCase", () => "Unknown cartoon"] 
    ]);

console.log(result); // Heartwarming adventures

Strong typing

The function is fully generic, preserving both input and output types.

const result = matchCase<"a" | "b", number>("a", {
    a: () => 10,
    b: () => 20,
    defaultCase: () => 0
});
// result is inferred as number

Using without default cases

When you use without a defaultCase or "default" a Exception will be thorned

import { matchCase } from "ts-match-case";

const myFavoriteSport = "chess";

const result = matchCase<string, string>(
    myFavoriteSport, {
        'soccer' : () => "The beautiful game, played worldwide",
        'basketball': () => "Fast-paced and full of slam dunks",
        'tennis' : () => "Precision, endurance, and skill",
    }
);// throw new Error(`No match found for value: "chess"`);

console.log(result); // won´t be called 

⚠️ Important: Using boolean keys

When using true or false as keys in matchCase, never use the object (JSON) format. In JavaScript/TypeScript, object keys are always strings, so true/false will be converted to "true" and "false", which can cause unexpected behavior.

import { defaultCase, matchCase } from "ts-match-case";

const isLoggedIn = true;

const message = matchCase<boolean, string>(
    isLoggedIn, [
        [true, () => "Welcome back, user!"],
        [false, () => "Please log in to continue"],
    ]
);

console.log(message); // Welcome back, user!


function foo(): boolean {
    // boolean logic
}

function bar(): boolean {
    // some other boolean logic
}

const result = matchCase<boolean, string>(true, [ // or false, in this case, in order, the firs methods returns boolean correctly, the referent CaseValue will be called
    [foo(), () => "foo is correct"],
    [bar(), () => "bar is correct"],
    [defaultCase, () => "no correct functions"]
]) // or false

console.log(result);

Using Functions as Keys in matchCase

The matchCase utility also supports functions as keys (just as list methods). This allows you to defer the evaluation of a case until the pipeline actually checks it, which can optimize performance for expensive or conditional computations.

import { defaultCase, matchCase } from "ts-match-case";

function isEven(a: number): boolean {
    return a % 2 === 0;
}

function isOdd(a: number): boolean {
    return a % 2 === 1;
}

const number = rand(); // generic function

const result = matchCase<boolean, string>(true, [
    [isEven(number), () => "Number is even"],
    [isOdd(number), () => "Number is positive"],
    [defaultCase, () => "No conditions matched"]
]); // the isEven and isOdd will be called before matchCase

const result = matchCase<boolean, string>(true, [
    [() => isEven(number), () => "Number is even"],
    [() => isOdd(number), () => "Number is positive"],
    [defaultCase, () => "No conditions matched"]
]); // the isEven and isOdd and others methods cases will be called in order inside matchCase