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

match-values

v3.0.0

Published

Apply pattern matching in JavaScript

Readme

Match Values

match-values workflow

A lightweight, fully-typed TypeScript library that makes pattern matching simple, safe, and fun! 🎯

Installation

npm install match-values

Quick Start

match-values provides a simple and powerful way to handle conditional logic using pattern matching. Think of it as a super-powered switch statement that's more flexible, readable, and completely type-safe.

Basic Usage

import { match, last } from 'match-values'

// 1. Object Pattern Matching (for literal values)
const httpStatus = match(200, {
  200: 'OK',
  404: 'Not Found',
  500: 'Server Error',
  [last]: 'Unknown Status' // Default case
})
// Returns: 'OK'

// 2. Conditional Pattern Matching (with functions)
const getGeneration = match(1995, [
  [(year) => year >= 1997, 'Gen Z'],
  [(year) => year >= 1981, 'Millennial'],
  [(year) => year >= 1965, 'Gen X'],
  [last, 'Boomer'] // Default case
])
// Returns: 'Millennial'

Key Features

1. Object Pattern Matching

Use plain objects to match against string or number keys. This is the most efficient way to handle a fixed set of literal values.

import { match, last } from 'match-values'

const getStatusColor = match(user.status, {
  active: 'green',
  pending: 'orange',
  blocked: 'red',
  [last]: 'grey'
})

2. Conditional Pattern Matching

Use an array of [predicate, value] tuples for more complex logic. The first predicate to return true wins.

import { match, last } from 'match-values'

const getMembershipLevel = match(user.points, [
  [(points) => points >= 500, 'Gold'],
  [(points) => points >= 100, 'Silver'],
  [(points) => points < 100, 'Bronze']
  // No default case needed if all possibilities are covered
])

3. Lazy Matching

Perfect for function composition and processing arrays. lazyMatch creates a reusable function with the pattern "baked in."

import { lazyMatch, last } from 'match-values'

const sizePattern = {
  small: 12,
  medium: 16,
  large: 20,
  [last]: 14 // Default size
}

// Use with arrays
const sizes = ['small', 'medium', 'extra-large'].map(lazyMatch(sizePattern))
// Returns: [12, 16, 14]

// Use in a function pipeline
const getFinalSize = compose(
  (size) => size + 2, // Add padding
  lazyMatch(sizePattern),
  (item) => item.size
)({ size: 'medium' })
// Returns: 18

API Reference

Main Functions

  • match<T, R>(value: T, pattern: ObjectPattern<R> | ConditionalPattern<T, R>): R
    • Matches a value against a pattern and returns the result.
  • lazyMatch<T, R>(pattern: ObjectPattern<R> | ConditionalPattern<T, R>): (value: T) => R
    • Creates a reusable function that has the pattern baked in.
  • matchCond<T, R>(value: T, pattern: ConditionalPattern<T, R>): R
    • A standalone function for when you only need conditional matching.

Special Exports

  • last: A symbol used to define the default case in any pattern. Using a symbol prevents key collisions.

Pattern Types

  • Object Pattern: Record<string | number, R> & { [last]?: R }
    • A simple JavaScript object for matching literal string or number keys.
  • Conditional Pattern: Array<[Predicate<T> | typeof last, R]>
    • An array of tuples, where the first item is a predicate function ((value: T) => boolean) and the second is the result.

Code Coverage

View Test Report

License

MIT