A fun library emulating (some of) the upcoming [pattern matching proposal](https://github.com/tc39/proposal-pattern-matching).
npm install mchA fun library emulating (some of) the upcoming pattern matching proposal.
No dependencies and native TypeScript support.
Supports a variety of match types:
| Type | Examples | Match method |
| ---------------- | ------------------------------- | ------------------------------------- |
| Value | when(20, ...) | Strictly equals the exact value |
| Object | when({ status: Number }, ...) | Recursively passes each key condition |
| Constructor | when(String, ...) | Instance of the constructor |
| RegExp | when(/hi/, ...) | Tests positive against the RegExp |
| Compare Function | when(x => x === 10, ...) | Custom compare function |
| Default | otherwise(...) | Default fallback that maches anything |
npm install mch or yarn add mch
``ts`
import { match, otherwise, when } from "mch"
Example usage returning a phrase based on a number.
`tsYou have ${x}
const message = match(count, [
when(0, () => "None for you"),
when(1, () => "You have one"),
when(Number, x => ),`
otherwise(() => {
throw new Error("Is this even a number?") // it's not
}),
])
Example usage with React.
`tsx`
{match(value, [
when(0, () => You have none),
when(Number, x => You have {x}),
when(Error, error =>
])}
Example usage with async/await and a fetch response.
`ts
const response = await fetch(url)
const body = await match(response, [
when({ status: /2\d\d/ }, res => res.json()),
when({ status: 404 }, () => {
throw new Error("404 Not Found")
}),
])
`
Example usage the spirit of Rust's result convention.
`ts
function findValue(x: number): Result
return x > 0 ? new Ok(x) : new Err("Invalid number")
}
const result = match(findValue(number), [
when(Ok, ({ value }) => value),
when(Err, () => 0),
])
``