Changeset - d1f86109e3cc
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 8 months ago 2024-05-30 08:08:45
drewp@bigasterisk.com
more *value getter variants
1 file changed with 38 insertions and 9 deletions:
0 comments (0 inline, 0 general)
web/SyncedGraph.ts
Show inline comments
 
@@ -215,13 +215,16 @@ export class SyncedGraph {
 
    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 @@ export class SyncedGraph {
 
    }
 
  }
 

	
 
  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 @@ export class SyncedGraph {
 
    }
 
    //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 @@ export class SyncedGraph {
 
    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 } }) {
0 comments (0 inline, 0 general)