0.1.0 • Published 11 months ago

@eatsjobs/ts-pattern-match v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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

11 months ago