0.40.0 • Published 8 months ago

@effect/match v0.40.0

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

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"
import { pipe } from "@effect/data/Function"

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 })) // 0
console.log(match({ b: "hello" })) // "hello"

You can also create a Matcher from a value using the value constructor function.

For example:

import * as Match from "@effect/match"
import { pipe } from "@effect/data/Function"

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) // "John is 30 years old"

Types of patterns

Predicates

Values can be tested against arbitrary functions.

import * as Match from "@effect/match"
import { pipe } from "@effect/data/Function"

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 })) // "Age: 5"
console.log(match({ age: 4 })) // "4 is too young"

not patterns

not lets you match on everything but a specific value.

import * as Match from "@effect/match"
import { pipe } from "@effect/data/Function"

const match = pipe(
  Match.type<string | number>(),
  Match.not("hi", (_) => "a"),
  Match.orElse(() => "b"),
)

console.log(match("hello")) // "a"
console.log(match("hi")) // "b"

tag patterns

Matches against the tag in a Discriminated Union

import * as Match from "@effect/match"
import * as E from "@effect/data/Either"
import { pipe } from "@effect/data/Function"

// type Either<L, R> = { _tag: "Right", right: R } | { _tag: "Left", left: L }
const match = pipe(
  Match.type<E.Either<string, number>>(),
  Match.tag("Right", (_) => _.right),
  Match.tag("Left", (_) => _.left),
  Match.exhaustive,
)

console.log(match(E.right(123))) // 123

Evaluating a Matcher

option

A Matcher that might match a value. Returns an Option.

import * as Match from "@effect/match"
import * as E from "@effect/data/Either"
import { pipe } from "@effect/data/Function"

// type Either<L, R> = { _tag: "Right", right: R } | { _tag: "Left", left: L }
// type Option<T> = { _tag: "Some", value: T } | { _tag: "None" }
const result = pipe(
  Match.value(E.right(0)),
  Match.when({ _tag: "Right" }, (_) => _.right),
  Match.option,
)

console.log(result) // { _tag: "Some", value: 0 }

exhaustive

A Matcher that marks the end of the matching process and checks if all possible matches were made. Returns the match (for Match.value) or the evaluation function (for Match.type).

import * as Match from "@effect/match"
import * as E from "@effect/data/Either"
import { pipe } from "@effect/data/Function"

// type Either<L, R> = { _tag: "Right", right: R } | { _tag: "Left", left: L }
const result = pipe(
  Match.value(E.right(0)),
  Match.when({ _tag: "Right" }, (_) => _.right),
  Match.exhaustive, // TypeError! { _tag: "left", left: never } is not assignable to never
)

orElse

A Matcher that marks the end of the matcher and allows to provide a fallback value if no patterns match. Returns the match (for Match.value) or the evaluation function (for Match.type).

import * as Match from "@effect/match"
import { pipe } from "@effect/data/Function"

const match = pipe(
  Match.type<string | number>(),
  Match.when("hi", (_) => "hello"),
  Match.orElse(() => "I literally do not understand"),
)

console.log(match("hello")) // "I literally do not understand"
console.log(match("hi")) // "hello"

either

A Matcher that might match a value. Returns an Either in the shape of Either<NoMatchResult, MatchResult>.

import * as Match from "@effect/match"
import { pipe } from "@effect/data/Function"

const match = pipe(
  Match.type<string>(),
  Match.when("hi", (_) => "hello"),
  Match.either,
)

// type Either<L, R> = { _tag: "Right", right: R } | { _tag: "Left", left: L }
console.log(match("hi")) // { _tag: "Right", value: "hello" }
console.log(match("shigidigi")) // { _tag: "Left", value: "shigidigi" }

License

The MIT License (MIT)

0.38.0

8 months ago

0.36.1

8 months ago

0.36.0

8 months ago

0.34.0

9 months ago

0.32.0

10 months ago

0.30.0

10 months ago

0.29.0

10 months ago

0.27.2

10 months ago

0.27.1

10 months ago

0.27.0

10 months ago

0.25.1

11 months ago

0.25.0

11 months ago

0.29.1

10 months ago

0.40.0

8 months ago

0.39.0

8 months ago

0.37.0

8 months ago

0.35.2

9 months ago

0.35.1

9 months ago

0.35.0

9 months ago

0.33.0

9 months ago

0.31.0

10 months ago

0.28.0

10 months ago

0.26.0

10 months ago

0.20.1

1 year ago

0.20.0

1 year ago

0.19.0

1 year ago

0.19.1

1 year ago

0.11.0

1 year ago

0.13.0

1 year ago

0.15.0

1 year ago

0.17.0

1 year ago

0.1.0

1 year ago

0.23.4

1 year ago

0.3.0

1 year ago

0.23.3

1 year ago

0.1.1

1 year ago

0.23.2

1 year ago

0.23.1

1 year ago

0.23.0

1 year ago

0.21.1

1 year ago

0.9.0

1 year ago

0.5.0

1 year ago

0.7.0

1 year ago

0.5.1

1 year ago

0.21.0

1 year ago

0.18.1

1 year ago

0.18.2

1 year ago

0.18.3

1 year ago

0.18.4

1 year ago

0.18.5

1 year ago

0.10.1

1 year ago

0.12.0

1 year ago

0.12.1

1 year ago

0.14.0

1 year ago

0.16.0

1 year ago

0.18.0

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.10.0

1 year ago

0.24.5

11 months ago

0.24.4

11 months ago

0.24.3

11 months ago

0.2.1

1 year ago

0.24.2

11 months ago

0.2.0

1 year ago

0.24.1

11 months ago

0.20.5

1 year ago

0.24.0

12 months ago

0.20.4

1 year ago

0.20.3

1 year ago

0.22.0

1 year ago

0.20.2

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.8.2

1 year ago

0.4.0

1 year ago

0.6.0

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago