import { decoder } from "https://deno.land/x/fun@v.2.0.0-alpha.11/mod.ts";
const { match } = decoder;
Construct a catamorphism over DecodeError, mapping each case of a DecodeError into the single type O.
Examples
Example 1
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: () => O
Index: () => O
Union: (errors: readonly [DecodeError, DecodeError, ...DecodeError[]]) => O
Intersection: (errors: readonly [DecodeError, DecodeError, ...DecodeError[]]) => O
Many: (errors: readonly DecodeError[]) => O
Returns
(e: DecodeError) => O