import { newtype } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { prism } = newtype;
If the Newtype and its underlying value are not referentially transparent (meaning they can always be swapped) then you can create an instance of Prism for the Newtype in order to optionally map into the Newtype given some Predicate.
Examples
Example 1
Example 1
import type { Option } from "./option.ts";
import * as N from "./newtype.ts";
import * as O from "./option.ts";
import { pipe } from "./fn.ts";
type Integer = N.Newtype<"Integer", number>;
const prismInteger = N.prism<Integer>(Number.isInteger);
// Turn number into an Option<Integer>
const result1: Option<Integer> = prismInteger.view(1);
// Turn an Option<Integer> into a number or fall back to 0 if Option is None
const result2: number = pipe(
result1,
O.map(prismInteger.review),
O.getOrElse(() => 0)
);
Type Parameters
T extends AnyNewtype