102
|
1 import { Store, Parser, Quad, Prefixes } from "n3";
|
|
2
|
|
3 export async function fetchAndParse(
|
|
4 url: string,
|
|
5 store?: Store
|
|
6 ): Promise<Store> {
|
|
7 const res = await fetch(url);
|
|
8 const body = await res.text();
|
|
9 return n3Graph(body, store);
|
|
10 }
|
95
|
11
|
102
|
12 export async function n3Graph(n3: string, store?: Store): Promise<Store> {
|
|
13 if (store === undefined) {
|
|
14 store = new Store();
|
|
15 }
|
|
16
|
103
|
17 const parser = new Parser({ format: "TriG" });
|
|
18 await new Promise((res, rej) => {
|
102
|
19 parser.parse(n3, (error, quad: Quad, prefixes: Prefixes) => {
|
103
|
20 if (error) rej(error);
|
102
|
21 if (quad) {
|
|
22 store!.addQuad(quad);
|
|
23 } else {
|
103
|
24 res(undefined);
|
102
|
25 }
|
95
|
26 });
|
102
|
27 });
|
103
|
28
|
|
29 return store;
|
95
|
30 }
|