summaryrefslogtreecommitdiff
path: root/utils/jsonds.ts
blob: c717232b233cccc6b8ab1bc1ebba982b84f017aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export class JSONSet<T extends Object> {
  private items: Set<string>;

  constructor() {
    this.items = new Set<string>();
  }

  add(item: T): void {
    const itemJson = JSON.stringify(item, Object.keys(item).sort());
    this.items.add(itemJson);
  }

  has(item: T): boolean {
    const itemJson = JSON.stringify(item, Object.keys(item).sort());
    return this.items.has(itemJson);
  }

  delete(item: T): boolean {
    const itemJson = JSON.stringify(item, Object.keys(item).sort());
    return this.items.delete(itemJson);
  }

  clear(): void {
    this.items.clear();
  }

  get size(): number {
    return this.items.size;
  }
}

export class JSONHashMap<T extends Object> {
  private map: Map<string, T>;

  constructor() {
    this.map = new Map<string, T>();
  }

  set(key: T, value: T): void {
    const keyJson = JSON.stringify(key, Object.keys(key).sort());
    this.map.set(keyJson, value);
  }

  get(key: T): T | undefined {
    const keyJson = JSON.stringify(key, Object.keys(key).sort());
    return this.map.get(keyJson);
  }

  has(key: T): boolean {
    const keyJson = JSON.stringify(key, Object.keys(key).sort());
    return this.map.has(keyJson);
  }

  delete(key: T): boolean {
    const keyJson = JSON.stringify(key, Object.keys(key).sort());
    return this.map.delete(keyJson);
  }

  clear(): void {
    this.map.clear();
  }

  get size(): number {
    return this.map.size;
  }
}