Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skorotkiewicz/rmatch
A utility function inspired by Rust's match for pattern matching in JavaScript/TypeScript.
https://github.com/skorotkiewicz/rmatch
javascript match typescript utility
Last synced: about 1 month ago
JSON representation
A utility function inspired by Rust's match for pattern matching in JavaScript/TypeScript.
- Host: GitHub
- URL: https://github.com/skorotkiewicz/rmatch
- Owner: skorotkiewicz
- License: mit
- Created: 2024-01-02T12:36:05.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-02T12:57:23.000Z (about 1 year ago)
- Last Synced: 2024-11-29T11:11:41.781Z (about 1 month ago)
- Topics: javascript, match, typescript, utility
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@skorotkiewicz/rmatch
- Size: 45.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rMatch
A utility function inspired by Rust's match for pattern matching in JavaScript/TypeScript.
## Installation
```bash
npm install @skorotkiewicz/rmatch
# or
yarn add @skorotkiewicz/rmatch
```## Usage
```js
// 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.