Functional pattern matching with the full power of Typescript.
Getting started
To install from npm:
npm install @effect/match
Once you have installed the library, you can import the necessary types and functions from the @effect/match
module.
import * as Match from "@effect/match"
Defining a Matcher
To define a Matcher
from a given type, you can use the type
constructor function.
You can then use the when
, not
& tag
combinators to specify the patterns to match against.
For example:
import * as Match from "@effect/match"
const match = pipe(
Match.type<{ a: number } | { b: string }>(),
Match.when({ a: Match.number }, (_) => _.a),
Match.when({ b: Match.string }, (_) => _.b),
Match.exhaustive,
)
console.log(match({ a: 0 }))
console.log(match({ b: "hello" }))
You can also create a Matcher
from a value using the value
constructor function.
For example:
import * as Match from "@effect/match"
const result = pipe(
Match.value({ name: "John", age: 30 }),
Match.when(
{ name: "John" },
(user) => `${user.name} is ${user.age} years old`,
),
Match.orElse(() => "Oh, not John"),
)
console.log(result)
Types of patterns
Predicates
Values can be tested against arbitrary functions.
import * as Match from "@effect/match"
const match = pipe(
Match.type<{ age: number }>(),
Match.when({ age: (age) => age >= 5 }, (user) => `Age: ${user.age}`),
Match.orElse((user) => `${user.age} is too young`),
)
console.log(match({ age: 5 }))
console.log(match({ age: 4 }))
not
patterns
not
lets you match on everything but a specific value or Schema.
import * as Match from "@effect/match"
const match = pipe(
Match.type<string | number>(),
Match.not("hi", (_) => "a"),
Match.orElse(() => "b"),
)
console.log(match("hello"))
console.log(match("hi"))
Credits
This library is built upon the great work of @fp-ts/schema.
License
The MIT License (MIT)