The type of the custom elements.
The type of the mapped primitive keys.
An iterable of elements using the custom element type.
A function that converts a custom element K
to a primitive
key KM
. This function must be deterministic and produce unique values
for unique elements.
A function that converts a primitive key KM
back to the
custom element K
. This should be the inverse of toKey
.
A new ISetMapped instance containing all unique elements from the iterable.
type Point = Readonly<{ x: number; tag: string }>;
const toKey = (point: Point) => JSON.stringify(point);
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
const fromKey = (key: string) => JSON.parse(key) as Point;
const set = ISetMapped.create<Point, string>(
[
{ x: 1, tag: 'a' },
{ x: 1, tag: 'a' },
{ x: 2, tag: 'b' },
],
toKey,
fromKey,
);
assert.deepStrictEqual(Array.from(set), [
{ x: 1, tag: 'a' },
{ x: 2, tag: 'b' },
]);
Creates a new ISetMapped instance with custom element transformation functions.
This factory function creates an immutable set that can use complex objects as elements by providing bidirectional transformation functions. The
toKey
function converts your custom element type to a primitive type that can be efficiently stored, whilefromKey
reconstructs the original element type for iteration and access.Performance: O(n) where n is the number of elements in the iterable.