import { comparable } from "https://deno.land/x/fun@v2.0.0-alpha.12/mod.ts";
const { premap } = comparable;
Create a Comparable using a Comparable and a function that takes a type L and returns a type D.
Examples
Example 1
Example 1
import { premap, number } from "./comparable.ts";
import { pipe } from "./fn.ts";
const dateToNumber = (d: Date): number => d.valueOf();
const date = pipe(number, premap(dateToNumber));
const now = new Date();
const alsoNow = new Date(Date.now());
const later = new Date(Date.now() + 60 * 60 * 1000);
const result1 = date.compare(now)(alsoNow); // true
const result2 = date.compare(now)(later); // false
Another use for premap with eq is to check for equality after normalizing data. In the following we can compare strings ignoring case by normalizing to lowercase strings.
Example 2
Example 2
import { premap, string } from "./comparable.ts";
import { pipe } from "./fn.ts";
const lowercase = (s: string) => s.toLowerCase();
const insensitive = pipe(
string, // exact string compare
premap(lowercase), // makes all strings lowercase
);
const result1 = insensitive.compare("Hello")("World"); // false
const result2 = insensitive.compare("hello")("Hello"); // true
Returns
(eq: Comparable<D>) => Comparable<L>