summaryrefslogtreecommitdiff
path: root/lib/types/collections/jsonds.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/types/collections/jsonds.ts')
-rw-r--r--lib/types/collections/jsonds.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/types/collections/jsonds.ts b/lib/types/collections/jsonds.ts
new file mode 100644
index 0000000..2bfaeee
--- /dev/null
+++ b/lib/types/collections/jsonds.ts
@@ -0,0 +1,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;
+ }
+}