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

x/fun/mod.ts>decoder.union

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

Provide an alternative Decoder to fallback against if the first one fails. This is an alias of alt.

Examples

Example 1

import * as D from "./decoder.ts";
import { pipe } from "./fn.ts";

const numOrStr = pipe(
  D.string,
  D.union(D.number),
);

const result1 = numOrStr(0); // Right(0)
const result2 = numOrStr("Hello"); // Right("Hello")
const result3 = numOrStr(null); // Left(DecodeError)

Returns

<A>(left: Decoder<B, A>) => Decoder<B, A | I>