Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/mod.ts>decoder.match

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

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.fold((acc, err) => acc + countErrors(err), 0),
  A.fold((acc, err) => acc + countErrors(err), 0),
  A.fold((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