diff web/SyncedGraph.ts @ 2440:d1f86109e3cc

more *value getter variants
author drewp@bigasterisk.com
date Thu, 30 May 2024 01:08:45 -0700
parents ac55319a2eac
children
line wrap: on
line diff
--- a/web/SyncedGraph.ts	Thu May 30 01:08:07 2024 -0700
+++ b/web/SyncedGraph.ts	Thu May 30 01:08:45 2024 -0700
@@ -215,13 +215,16 @@
     this.autoDeps.runHandler(func, label);
   }
 
-  _singleValue(s: Quad_Subject, p: Quad_Predicate) {
+  _singleValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError: boolean) {
     this.autoDeps.askedFor(s, p, null, null);
     const quads = this.graph.getQuads(s, p, null, null);
     const objs = new Set(Array.from(quads).map((q: Quad) => q.object));
 
     switch (objs.size) {
       case 0:
+        if (_nullNotError) {
+          return null;
+        }
         throw new Error("no value for " + s.value + " " + p.value);
       case 1:
         var obj = objs.values().next().value;
@@ -231,7 +234,7 @@
     }
   }
 
-  floatValue(s: Quad_Subject, p: Quad_Predicate) {
+  floatValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError = false) {
     const key = s.value + "|" + p.value;
     const hit = this.cachedFloatValues.get(key);
     if (hit !== undefined) {
@@ -239,7 +242,9 @@
     }
     //log('float miss', s, p)
 
-    const v = this._singleValue(s, p).value;
+    const gv = this._singleValue(s, p, _nullNotError);
+    if (_nullNotError && gv == null) return null;
+    const v = gv.value;
     const ret = parseFloat(v);
     if (isNaN(ret)) {
       throw new Error(`${s.value} ${p.value} -> ${v} not a float`);
@@ -248,20 +253,44 @@
     return ret;
   }
 
-  stringValue(s: any, p: any) {
-    return this._singleValue(s, p).value;
+  _booleanValue(s: any, p: any, _nullNotError = false) {
+    const gv = this._singleValue(s, p, _nullNotError);
+    if (_nullNotError && gv == null) return null;
+    return gv.value == "true";
+  }
+  booleanValue(s: any, p: any): boolean {
+    return this._booleanValue(s, p, false) as boolean;
+  }
+  stringValue(s: any, p: any, _nullNotError = false) {
+    const gv = this._singleValue(s, p, _nullNotError);
+    if (_nullNotError && gv == null) return null;
+    return gv.value;
   }
 
-  uriValue(s: Quad_Subject, p: Quad_Predicate) {
+  uriValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError = false) {
     const key = s.value + "|" + p.value;
     const hit = this.cachedUriValues.get(key);
     if (hit !== undefined) {
       return hit;
     }
 
-    const ret = this._singleValue(s, p);
-    this.cachedUriValues.set(key, ret);
-    return ret;
+    const gv = this._singleValue(s, p, _nullNotError);
+    if (_nullNotError && gv == null) return null;
+    this.cachedUriValues.set(key, gv);
+    return gv;
+  }
+
+  optionalFloatValue(s: Quad_Subject, p: Quad_Predicate): number | null {
+    return this.floatValue(s, p, true);
+  }
+  optionalBooleanValue(s: Quad_Subject, p: Quad_Predicate): boolean | null {
+    return this._booleanValue(s, p, true);
+  }
+  optionalStringValue(s: Quad_Subject, p: Quad_Predicate): string | null {
+    return this.stringValue(s, p, true);
+  }
+  optionalUriValue(s: Quad_Subject, p: Quad_Predicate): N3.NamedNode | null {
+    return this.uriValue(s, p, true);
   }
 
   labelOrTail(uri: { value: { split: (arg0: string) => any } }) {