view 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 source

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 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!);
      }
    });
  });
}