@eatsjobs/ts-pattern-match
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

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

npm i @eatsjobs/ts-pattern-matching

Usage

  import type { Predicate, Handler } from "@eatsjobs/ts-pattern-matching";
  import match from "@eatsjobs/ts-pattern-matching";

  type Input = { a: boolean };
  const predicate: Predicate<Input> = (input) => {
    return input.a
  };

  const handler: Handler<string> = (input: Input) => {
    return "this will executed if predicate returns true"
  };

  
  const string = match<Input, string>({ a: true })
    .when(predicate, handler)
    .when(..., ...)
    .otherwise(() => "default string") // default handler
    .run();

Readme

Keywords

none

Package Sidebar

Install

npm i @eatsjobs/ts-pattern-match

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

11.5 kB

Total Files

11

Last publish

Collaborators

  • eatsjobs