blob: 2bfaeee505296a45fa0a6804a0040e1a34020aa9 (
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
|
export class JSONSet<T extends Object> {
constructor(private readonly items: Set<string> = new Set()) {}
public add(item: T): void {
const itemJson = JSON.stringify(item, Object.keys(item).sort());
this.items.add(itemJson);
}
public has(item: T): boolean {
const itemJson = JSON.stringify(item, Object.keys(item).sort());
return this.items.has(itemJson);
}
public delete(item: T): boolean {
const itemJson = JSON.stringify(item, Object.keys(item).sort());
return this.items.delete(itemJson);
}
public clear(): void {
this.items.clear();
}
public size(): number {
return this.items.size;
}
}
export class JSONHashMap<T extends Object, R extends Object> {
constructor(private readonly map: Map<string, R> = new Map()) {}
public set(key: T, value: R): void {
const keyJson = JSON.stringify(key, Object.keys(key).sort());
this.map.set(keyJson, value);
}
public get(key: T): R | undefined {
const keyJson = JSON.stringify(key, Object.keys(key).sort());
return this.map.get(keyJson);
}
public has(key: T): boolean {
const keyJson = JSON.stringify(key, Object.keys(key).sort());
return this.map.has(keyJson);
}
public keys(): T[] {
return Array.from(this.map.keys()).map((x) => JSON.parse(x) as T);
}
public delete(key: T): boolean {
const keyJson = JSON.stringify(key, Object.keys(key).sort());
return this.map.delete(keyJson);
}
public clear(): void {
this.map.clear();
}
public size(): number {
return this.map.size;
}
}
|