Skip to main content
Module

x/fun/mod.ts>refinement.compose

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

Compose two refinements, A -> B and B -> C creating a Refinement<A, C>.

Examples

Example 1

import * as R from "./refinement.ts";
import { pipe } from "./fn.ts";

type Person = { name: string };
type Rec = Record<string, unknown>;

const nonnull = (u: unknown): u is Rec => u !== null && u !== undefined;
const hasKey =
  <K extends string>(key: K) => (u: Rec): u is Record<K, unknown> =>
    Object.hasOwn(u, key);
const person = (u: Record<"name", unknown>): u is Person =>
  typeof u.name ===
    "string";

const isPerson = pipe(nonnull, R.compose(hasKey("name")), R.compose(person));

const value1 = null;
const value2 = {};
const value3 = { name: 1 };
const value4 = { name: "Brandon" };

const result1 = isPerson(value1); // false
const result2 = isPerson(value2); // false
const result3 = isPerson(value3); // false
const result4 = isPerson(value4); // true, value4: Person

Type Parameters

A
B extends A
C extends B

Returns

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