Skip to main content
Module

x/fun/decoder.ts>match

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function match
import { match } from "https://deno.land/x/fun@v2.0.0-alpha.10/decoder.ts";

Construct a catamorphism over DecodeError, mapping each case of a DecodeError into the single type O.

Examples

Example 1

import type { DecodeError } from "./decoder.ts";
import * as D from "./decoder.ts";
import * as A from "./array.ts";

const countErrors: (err: DecodeError) => number = D.match(
  () => 1,
  (_, err) => countErrors(err),
  (_, __, err) => countErrors(err),
  (_, __, err) => countErrors(err),
  A.reduce((acc, err) => acc + countErrors(err), 0),
  A.reduce((acc, err) => acc + countErrors(err), 0),
  A.reduce((acc, err) => acc + countErrors(err), 0),
);

const result1 = countErrors(D.leafErr(1, "expected string")); // 1
const result2 = countErrors(D.manyErr(
  D.leafErr(1, "expected string"),
  D.leafErr(1, "expected string"),
  D.leafErr(1, "expected string"),
)); // 3

Parameters

Leaf: (value: unknown, reason: string) => O
Wrap: (context: string, error: DecodeError) => O
Key: (
key: string,
property: Property,
error: DecodeError,
) => O
Index: (
index: number,
property: Property,
error: DecodeError,
) => O
Union: (errors: readonly [DecodeError, DecodeError, ...DecodeError[]]) => O
Intersection: (errors: readonly [DecodeError, DecodeError, ...DecodeError[]]) => O
Many: (errors: readonly DecodeError[]) => O