• Adds equality functions to a data object. This freezes an object. HashCode is lazily evaluated and memoized.

    This function also acts as an error boundary; you may assume both arguments for equals() are of the same type as the data object, any exceptions thrown by the equality function will be caught and treated as false.

    Example

    import * as io from "@prelude-io/core";
    import { BusOutputType } from "@prelude-io/core";
    import { stringHashCode } from "prelude-ts";

    const Pet = io.Complex("NestedBus", {
    species: io.string,
    name: io.string,
    });

    const suku = (
    await Pet.deserialize({ name: "Suku", species: "Cat" })
    ).getOrThrow() as BusOutputType<typeof Pet>;

    const skadi = (
    await Pet.deserialize({ name: "Skadi", species: "Cat" })
    ).getOrThrow() as BusOutputType<typeof Pet>;

    const sukuWithEquality = io.addEquality(
    suku,
    (a, b) => a.name === b.name && a.species === b.species,
    (a) => stringHashCode(a.name + a.species)
    );

    console.log(sukuWithEquality.equals(suku)); // true
    console.log(sukuWithEquality.equals(skadi)); // false

    Type Parameters

    • D extends Record<string, unknown>

      The type of the data object

    Parameters

    • data: D

      The data object to add equality to

    • equals: ((a, b) => boolean)

      The equality function

        • (a, b): boolean
        • A comparison function

          Parameters

          • a: D
          • b: D

          Returns boolean

    • hashCode: ((data) => number)

      The hash code function

        • (data): number
        • A hashcode generating function

          Parameters

          • data: D

          Returns number

    Returns D & HasEquals

    The data object with equality added

Generated using TypeDoc