File size: 601 Bytes
90cbf22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export function parseMap<Id, Serialized, Parsed>(
  records: Serialized[],
  constructor: new (r: Serialized) => Parsed,
  getId: (r: Parsed) => Id,
): Map<Id, Parsed> {
  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<Serialized, T extends { serialize(): Serialized }>(
  map: Map<string, T>,
): Serialized[] {
  return [...map.values()].map((v) => v.serialize());
}