Skip to content

A utility function inspired by Rust's match for pattern matching in JavaScript/TypeScript.

License

Notifications You must be signed in to change notification settings

skorotkiewicz/rmatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rMatch

A utility function inspired by Rust's match for pattern matching in JavaScript/TypeScript.

Installation

npm install @skorotkiewicz/rmatch
# or
yarn add @skorotkiewicz/rmatch

Usage

// const { match } = require("@skorotkiewicz/rmatch");
import { rmatch } from "@skorotkiewicz/rmatch";

// Example usage:
let age = 8;
rmatch(age, [
  [(val) => val >= 1 && val <= 18, () => console.log("Important Birthday")],
  [(val) => val === 21 || val === 50, () => console.log("Important Birthday")],
  [
    (val) => val >= 65 && val <= Number.MAX_SAFE_INTEGER,
    () => console.log("Important Birthday"),
  ],
  [() => true, () => console.log("Not an Important Birthday")], // Default
]);

API

rmatch(value, patterns)

  • value: The value to be matched against patterns.
  • patterns: An array of patterns, where each pattern is a tuple of two elements:
    • The first function checks if the value matches the pattern.
    • The second function executes when a match is found.

License

This project is licensed under the MIT License.

About

A utility function inspired by Rust's match for pattern matching in JavaScript/TypeScript.

Topics

Resources

License

Stars

Watchers

Forks