Socket
Book a DemoInstallSign in
Socket

@driimus/matchbox

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@driimus/matchbox

All-purpose matchers in JS

latest
Source
npmnpm
Version
0.3.0
Version published
Maintainers
0
Created
Source

matchbox

All-purpose matchers in JS

Installation

pnpm add @driimus/matchbox

Usage

Matcher functions are created by providing an array of tuples, where each entry consists of a predicate function and the output value for a successful match.

sync

Synchronous matching evaluates each predicates in order until a match is found.

import { matchbox } from '@driimus/matchbox';

const match = matchbox.sync([
    [(x) => x === 0, 'none'],
    [(x) => x > 0 && x <= 9, 'a few'],
    [() => true, 'lots'],
]);

console.log(match(5)); // 'a few'

async

Asynchronous matching evaluates predicates concurrently and resolves with the first fulfilled match.

import { matchbox } from "@driimus/matchbox";

const match = matchbox.async([
  [async (x) => x === 0, "none"],
  [async (x) => x > 0 && x <= 9, "a few"],
  [async (x) => x > 10, "lots"],
  [
    async (x) =>
      new Promise((resolve, reject) =>
        setTimeout(x > 50 ? resolve : reject, 10_000)
      ),
    "too many",
  ],
]);

console.log(await match(99)); // 'lots'

FAQs

Package last updated on 15 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts