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

ciaplu

v2.5.0

Published

A TypeScript library implementing Pattern Matching for declarative matching of values or types, with support for both synchronous and asynchronous handlers.

Readme

Ciaplu - Pattern Matching Library

Ciaplu is a TypeScript library that implements Pattern Matching to declaratively match values or types. It allows you to define handlers for both synchronous and asynchronous operations, making it easy to structure your code for complex conditional logic. Whether you're matching basic values or more complex types, Ciaplu provides a clean and powerful way to handle various conditions with a minimalistic API. It is also well-suited for AI applications such as managing prompts and conditional logic for dynamic actions.

Features

  • Support for synchronous and asynchronous functions: Execute actions based on the matched value or condition.
  • Clean API: Use match, with, e when methods for readable and maintainable code.
  • Versatile: Ideal for use cases such as exception handling, value matching, or dynamic action selection and AI integration..

Installation

Install ciaplu using npm:

npm install ciaplu

Usage Examples

Matching String Values

import { match } from 'ciaplu';

async function example() {
  const result = await match('bagna cauda')
    .with('taiarin', async () => await asyncFunction1())
    .with('bagna cauda', async () => await asyncFunction2())
    .otherwise(async () => await asyncCereaFunction());

  console.log(result); // Output: asyncFunction2 output...
}

Matching Class Instances (Synchronous)

import { match } from 'ciaplu';

try {
  throw new BoiaFausError();
} catch (ex) {
  const res = match(ex)
    .withType(TurnaSiError, () => 'Handled TurnaSiError')
    .withType(BoiaFausError, () => 'Handled BoiaFausError')
    .otherwise(() => 'Cerea!')
    .return();

  console.log(res); // Output: 'Handled BoiaFausError'
}

Collecting Multiple Matches with .any() and .all()

import { match } from 'ciaplu';

const res = await match('test string with multiple conditions')
  .extracting((value: string) => Promise.resolve(value.split(' ')))
  .matching(async (words, wordCount) => Promise.resolve(words.length === wordCount))
  .any()
  .with(5, async (value: any) => Promise.resolve(`cerea (${value})`))
  .with(3, async () => Promise.resolve('tinca'))
  .with(5, async (value: any) => Promise.resolve(`buta (${value})`))
  .otherwise(async () => Promise.resolve('no match found!'))
  .all();

console.log(res);
// Output:
// [
//   "cerea (5)",
//   "buta (5)",
// ]

API

.match(value)

Initializes a matcher for the given value. The value can be a primitive, an object, or an exception.

.with(condition, handler)

Defines a condition and its corresponding handler:

  • condition: A value or class to match against.
  • handler: A function to execute if the condition is met. It can be synchronous or asynchronous. Chain multiple .with methods to handle different cases.

.withType(classConstructor, handler)

Defines a condition that matches instances of a class:

  • classConstructor: The class to match.
  • handler: A function to execute when an instance of the class is matched.

.when(condition, handler)

Defines a condition to match based on a function that returns a boolean or a promise:

  • condition: A function that returns true or false.
  • handler: A function to execute when the condition is met.

.extracting(extractor)

Extracts a value from the matched input:

  • extractor: A function that takes the input value and returns a transformed result.

.matching(matcher)

Changes the matcher to be applied from that point forward:

  • matcher: A function that determines how subsequent matching will be applied.

.first()

Executes only the first handler that matches the condition. Useful for cases where you only need to handle the first match. This is the default behavior

.any()

Executes all handlers that match the condition. Useful for cases where you want to handle multiple matches.

.not()

Negates the condition, meaning the next statement will match only if the condition is false.

.yet()

Affirms the condition, meaning the next statement will match only if the condition is true.

.one()

Returns only the first matched value. This is the default behavior.

.all()

Returns an array of results, allowing you to capture all matched values.

.otherwise(handler)

Defines a fallback handler to execute if no conditions match:

  • handler: A function to execute as a fallback. It can be synchronous or asynchronous.

.return()

Executes the matching logic synchronously and returns the final result immediately.
This is useful when all handlers are synchronous and you don't need to await a Promise.

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Implement your changes, including tests if applicable.
  4. Submit a pull request with a detailed explanation of your work.

If you encounter bugs or have suggestions for improvements, please open an issue!