Skip to main content
Module

x/fun/mod.ts>decoder.compose

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 decoder.compose
import { decoder } from "https://deno.land/x/fun@v.2.0.0-alpha.11/mod.ts";
const { compose } = decoder;

Compose two Decoders where the input to second aligns with the output of first.

Examples

Example 1

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

const prefixed = <T extends string>(prefix: T) =>
  D.fromPredicate(S.startsWith(prefix), `prefixed with "${prefix}"`);

const eventMethod = pipe(
  D.string,
  D.compose(prefixed("on")),
);

const result1 = eventMethod(null); // Left(DecodeError)
const result2 = eventMethod("hello"); // Left(DecodeError)
const result3 = eventMethod("onClick"); // Right("onClick");

Returns

<A>(first: Decoder<A, B>) => Decoder<A, C>