Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eatsjobs/ts-pattern-match
Simple pattern matching for javascript
https://github.com/eatsjobs/ts-pattern-match
Last synced: 9 days ago
JSON representation
Simple pattern matching for javascript
- Host: GitHub
- URL: https://github.com/eatsjobs/ts-pattern-match
- Owner: eatsjobs
- Created: 2023-05-29T11:03:21.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-06-13T16:29:20.000Z (over 1 year ago)
- Last Synced: 2024-10-31T14:05:08.685Z (16 days ago)
- Language: TypeScript
- Size: 85.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple Pattern Matching
For complex logic sometime switch is not enough. There's a pattern matching proposal but it's not ready yet.
https://github.com/tc39/proposal-pattern-matching## Getting Started
```sh
npm i @eatsjobs/ts-pattern-matching
```## Usage
```typescript
import type { Predicate, Handler } from "@eatsjobs/ts-pattern-matching";
import match from "@eatsjobs/ts-pattern-matching";type Input = { a: boolean };
const predicate: Predicate = (input) => {
return input.a
};const handler: Handler = (input: Input) => {
return "this will executed if predicate returns true"
};
const string = match({ a: true })
.when(predicate, handler)
.when(..., ...)
.otherwise(() => "default string") // default handler
.run();```