diff src/fetchAndParse.ts @ 102:ab7dca42afbd

rewrite ViewConfig
author drewp@bigasterisk.com
date Fri, 11 Mar 2022 23:19:35 -0800
parents 47d3b5a5bd5e
children f12feced00ce
line wrap: on
line diff
--- a/src/fetchAndParse.ts	Fri Feb 11 23:24:41 2022 -0800
+++ b/src/fetchAndParse.ts	Fri Mar 11 23:19:35 2022 -0800
@@ -1,21 +1,27 @@
-import { Store, Parser } from "n3";
+import { Store, Parser, Quad, Prefixes } from "n3";
+
+export async function fetchAndParse(
+  url: string,
+  store?: Store
+): Promise<Store> {
+  const res = await fetch(url);
+  const body = await res.text();
+  return n3Graph(body, store);
+}
 
-export async function fetchAndParse(url: string): Promise<Store> {
-    const store = new Store();
-    const res = await fetch(url);
-    const body = await res.text();
-    const parser = new Parser({ format: "N3" });
-    await new Promise((done, rej) => {
-        parser.parse(body, (err, quad, prefixes) => {
-            if (err) {
-                throw err;
-            }
-            if (quad) {
-                store.addQuad(quad);
-            } else {
-                done(null);
-            }
-        });
+export async function n3Graph(n3: string, store?: Store): Promise<Store> {
+  if (store === undefined) {
+    store = new Store();
+  }
+
+  const parser = new Parser({ format: "N3" });
+  return new Promise((res, rej) => {
+    parser.parse(n3, (error, quad: Quad, prefixes: Prefixes) => {
+      if (quad) {
+        store!.addQuad(quad);
+      } else {
+        res(store!);
+      }
     });
-    return store;
+  });
 }