UNPKG

@unsplash/sum-types-fp-ts

Version:
34 lines (33 loc) 950 B
import * as Sum from "@unsplash/sum-types"; import { fromEquals } from "fp-ts/Eq"; import { fromCompare } from "fp-ts/Ord"; import { Ord as ordString } from "fp-ts/string"; export const getEq = (eqs) => fromEquals((x, y) => { const [xk, xv] = Sum.serialize(x); const [yk, yv] = Sum.serialize(y); if (xk !== yk) return false; const eq = eqs[xk]; return (eq === undefined || eq.equals(xv, yv)); }); export const getOrd = (ords) => fromCompare((x, y) => { const [xk, xv] = Sum.serialize(x); const [yk, yv] = Sum.serialize(y); if (xk !== yk) return ordString.compare(xk, yk); const ord = ords[xk]; return ord === undefined ? 0 : ord.compare(xv, yv); }); export const getShow = (shows) => ({ show: x => { const [k, v] = Sum.serialize(x); return k in shows ? `${k}(${shows[k] .show(v)})` : k; }, });