diff src/graph_queries.ts @ 53:601a604c097a

new module with getStringValue
author drewp@bigasterisk.com
date Mon, 13 Jan 2020 00:06:36 -0800
parents
children 0c188ed3bcd8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/graph_queries.ts	Mon Jan 13 00:06:36 2020 -0800
@@ -0,0 +1,18 @@
+import { N3Store, NamedNode } from "n3";
+
+export function getStringValue(
+  store: N3Store | undefined,
+  subj: NamedNode,
+  pred: NamedNode,
+  defaultValue: string = ""
+): string {
+  if (store === undefined) {
+    // this is so you can use the function before you have a graph
+    return "...";
+  }
+  const objs = store.getObjects(subj, pred, null);
+  if (objs.length == 0) {
+    return defaultValue;
+  }
+  return objs[0].value;
+}