From 5a303202529bfea40b29e5c6b43fe3879e440753 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 31 Jul 2025 22:27:55 -0700 Subject: Adds JSON datatypes from advent of code utils --- lib/types/collections/index.ts | 1 + lib/types/collections/jsonds.ts | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 lib/types/collections/jsonds.ts diff --git a/lib/types/collections/index.ts b/lib/types/collections/index.ts index 8a12ad8..7b968fe 100644 --- a/lib/types/collections/index.ts +++ b/lib/types/collections/index.ts @@ -1 +1,2 @@ export * from './cons'; +export * from './jsonds'; 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 { + constructor(private readonly items: Set = 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 { + constructor(private readonly map: Map = 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; + } +} -- cgit v1.2.3-70-g09d2