comparison src/fetchAndParse.ts @ 102:ab7dca42afbd

rewrite ViewConfig
author drewp@bigasterisk.com
date Fri, 11 Mar 2022 23:19:35 -0800
parents 47d3b5a5bd5e
children f12feced00ce
comparison
equal deleted inserted replaced
101:76c1a29a328f 102:ab7dca42afbd
1 import { Store, Parser } from "n3"; 1 import { Store, Parser, Quad, Prefixes } from "n3";
2 2
3 export async function fetchAndParse(url: string): Promise<Store> { 3 export async function fetchAndParse(
4 const store = new Store(); 4 url: string,
5 const res = await fetch(url); 5 store?: Store
6 const body = await res.text(); 6 ): Promise<Store> {
7 const parser = new Parser({ format: "N3" }); 7 const res = await fetch(url);
8 await new Promise((done, rej) => { 8 const body = await res.text();
9 parser.parse(body, (err, quad, prefixes) => { 9 return n3Graph(body, store);
10 if (err) { 10 }
11 throw err; 11
12 } 12 export async function n3Graph(n3: string, store?: Store): Promise<Store> {
13 if (quad) { 13 if (store === undefined) {
14 store.addQuad(quad); 14 store = new Store();
15 } else { 15 }
16 done(null); 16
17 } 17 const parser = new Parser({ format: "N3" });
18 }); 18 return new Promise((res, rej) => {
19 parser.parse(n3, (error, quad: Quad, prefixes: Prefixes) => {
20 if (quad) {
21 store!.addQuad(quad);
22 } else {
23 res(store!);
24 }
19 }); 25 });
20 return store; 26 });
21 } 27 }