« get me outta code hell

wiki-data: TupleMapForBabies - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-11-14 19:48:06 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-15 20:40:13 -0400
commit9d91023e7c7827e640e8edcda3743cb0109f2170 (patch)
tree5716d4aa25a3184c850afd73381a4a4e2140ed58 /src/util
parent82a1f50865b461f9321c1bbf87a80174466351ab (diff)
wiki-data: TupleMapForBabies
Diffstat (limited to 'src/util')
-rw-r--r--src/util/wiki-data.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 33db526b..4855a7a2 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -413,3 +413,50 @@ export class TupleMap {
     return value;
   }
 }
+
+export class TupleMapForBabies {
+  #here = new WeakMap();
+  #next = new WeakMap();
+
+  set(...args) {
+    const first = args.at(0);
+    const last = args.at(-1);
+    const rest = args.slice(1, -1);
+
+    if (empty(rest)) {
+      this.#here.set(first, last);
+    } else if (this.#next.has(first)) {
+      this.#next.get(first).set(...rest, last);
+    } else {
+      const tupleMap = new TupleMapForBabies();
+      this.#next.set(first, tupleMap);
+      tupleMap.set(...rest, last);
+    }
+  }
+
+  get(...args) {
+    const first = args.at(0);
+    const rest = args.slice(1);
+
+    if (empty(rest)) {
+      return this.#here.get(first);
+    } else if (this.#next.has(first)) {
+      return this.#next.get(first).get(...rest);
+    } else {
+      return undefined;
+    }
+  }
+
+  has(...args) {
+    const first = args.at(0);
+    const rest = args.slice(1);
+
+    if (empty(rest)) {
+      return this.#here.has(first);
+    } else if (this.#next.has(first)) {
+      return this.#next.get(first).has(...rest);
+    } else {
+      return false;
+    }
+  }
+}