diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-07-31 22:27:55 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-07-31 22:28:00 -0700 |
commit | 5a303202529bfea40b29e5c6b43fe3879e440753 (patch) | |
tree | e04bb044d327518b1fccb6a9415e005e15e6e40f | |
parent | ab7c67e564cb0e155ab07612bbed9770006c55c9 (diff) | |
download | pengueno-5a303202529bfea40b29e5c6b43fe3879e440753.tar.gz pengueno-5a303202529bfea40b29e5c6b43fe3879e440753.zip |
Adds JSON datatypes from advent of code utils
-rw-r--r-- | lib/types/collections/index.ts | 1 | ||||
-rw-r--r-- | lib/types/collections/jsonds.ts | 62 |
2 files changed, 63 insertions, 0 deletions
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<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; + } +} |