Mercurial > code > home > repos > light9
comparison 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 |
comparison
equal
deleted
inserted
replaced
2439:06da5db2fafe | 2440:d1f86109e3cc |
---|---|
213 //label = label + @serial | 213 //label = label + @serial |
214 | 214 |
215 this.autoDeps.runHandler(func, label); | 215 this.autoDeps.runHandler(func, label); |
216 } | 216 } |
217 | 217 |
218 _singleValue(s: Quad_Subject, p: Quad_Predicate) { | 218 _singleValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError: boolean) { |
219 this.autoDeps.askedFor(s, p, null, null); | 219 this.autoDeps.askedFor(s, p, null, null); |
220 const quads = this.graph.getQuads(s, p, null, null); | 220 const quads = this.graph.getQuads(s, p, null, null); |
221 const objs = new Set(Array.from(quads).map((q: Quad) => q.object)); | 221 const objs = new Set(Array.from(quads).map((q: Quad) => q.object)); |
222 | 222 |
223 switch (objs.size) { | 223 switch (objs.size) { |
224 case 0: | 224 case 0: |
225 if (_nullNotError) { | |
226 return null; | |
227 } | |
225 throw new Error("no value for " + s.value + " " + p.value); | 228 throw new Error("no value for " + s.value + " " + p.value); |
226 case 1: | 229 case 1: |
227 var obj = objs.values().next().value; | 230 var obj = objs.values().next().value; |
228 return obj; | 231 return obj; |
229 default: | 232 default: |
230 throw new Error("too many different values: " + JSON.stringify(quads)); | 233 throw new Error("too many different values: " + JSON.stringify(quads)); |
231 } | 234 } |
232 } | 235 } |
233 | 236 |
234 floatValue(s: Quad_Subject, p: Quad_Predicate) { | 237 floatValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError = false) { |
235 const key = s.value + "|" + p.value; | 238 const key = s.value + "|" + p.value; |
236 const hit = this.cachedFloatValues.get(key); | 239 const hit = this.cachedFloatValues.get(key); |
237 if (hit !== undefined) { | 240 if (hit !== undefined) { |
238 return hit; | 241 return hit; |
239 } | 242 } |
240 //log('float miss', s, p) | 243 //log('float miss', s, p) |
241 | 244 |
242 const v = this._singleValue(s, p).value; | 245 const gv = this._singleValue(s, p, _nullNotError); |
246 if (_nullNotError && gv == null) return null; | |
247 const v = gv.value; | |
243 const ret = parseFloat(v); | 248 const ret = parseFloat(v); |
244 if (isNaN(ret)) { | 249 if (isNaN(ret)) { |
245 throw new Error(`${s.value} ${p.value} -> ${v} not a float`); | 250 throw new Error(`${s.value} ${p.value} -> ${v} not a float`); |
246 } | 251 } |
247 this.cachedFloatValues.set(key, ret); | 252 this.cachedFloatValues.set(key, ret); |
248 return ret; | 253 return ret; |
249 } | 254 } |
250 | 255 |
251 stringValue(s: any, p: any) { | 256 _booleanValue(s: any, p: any, _nullNotError = false) { |
252 return this._singleValue(s, p).value; | 257 const gv = this._singleValue(s, p, _nullNotError); |
253 } | 258 if (_nullNotError && gv == null) return null; |
254 | 259 return gv.value == "true"; |
255 uriValue(s: Quad_Subject, p: Quad_Predicate) { | 260 } |
261 booleanValue(s: any, p: any): boolean { | |
262 return this._booleanValue(s, p, false) as boolean; | |
263 } | |
264 stringValue(s: any, p: any, _nullNotError = false) { | |
265 const gv = this._singleValue(s, p, _nullNotError); | |
266 if (_nullNotError && gv == null) return null; | |
267 return gv.value; | |
268 } | |
269 | |
270 uriValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError = false) { | |
256 const key = s.value + "|" + p.value; | 271 const key = s.value + "|" + p.value; |
257 const hit = this.cachedUriValues.get(key); | 272 const hit = this.cachedUriValues.get(key); |
258 if (hit !== undefined) { | 273 if (hit !== undefined) { |
259 return hit; | 274 return hit; |
260 } | 275 } |
261 | 276 |
262 const ret = this._singleValue(s, p); | 277 const gv = this._singleValue(s, p, _nullNotError); |
263 this.cachedUriValues.set(key, ret); | 278 if (_nullNotError && gv == null) return null; |
264 return ret; | 279 this.cachedUriValues.set(key, gv); |
280 return gv; | |
281 } | |
282 | |
283 optionalFloatValue(s: Quad_Subject, p: Quad_Predicate): number | null { | |
284 return this.floatValue(s, p, true); | |
285 } | |
286 optionalBooleanValue(s: Quad_Subject, p: Quad_Predicate): boolean | null { | |
287 return this._booleanValue(s, p, true); | |
288 } | |
289 optionalStringValue(s: Quad_Subject, p: Quad_Predicate): string | null { | |
290 return this.stringValue(s, p, true); | |
291 } | |
292 optionalUriValue(s: Quad_Subject, p: Quad_Predicate): N3.NamedNode | null { | |
293 return this.uriValue(s, p, true); | |
265 } | 294 } |
266 | 295 |
267 labelOrTail(uri: { value: { split: (arg0: string) => any } }) { | 296 labelOrTail(uri: { value: { split: (arg0: string) => any } }) { |
268 let ret: any; | 297 let ret: any; |
269 try { | 298 try { |