export function parseMap( records: Serialized[], constructor: new (r: Serialized) => Parsed, getId: (r: Parsed) => Id, ): Map { const out = new Map(); for (const record of records) { const parsed = new constructor(record); const id = getId(parsed); if (out.has(id)) { throw new Error(`Duplicate ID ${id}`); } out.set(id, parsed); } return out; } export function serializeMap( map: Map, ): Serialized[] { return [...map.values()].map((v) => v.serialize()); }