Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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();

```