view src/fetchAndParse.ts @ 103:f12feced00ce

WIP rewriting Layout
author drewp@bigasterisk.com
date Sat, 12 Mar 2022 00:42:00 -0800
parents ab7dca42afbd
children
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: "TriG" });
  await new Promise((res, rej) => {
    parser.parse(n3, (error, quad: Quad, prefixes: Prefixes) => {
      if (error) rej(error);
      if (quad) {
        store!.addQuad(quad);
      } else {
        res(undefined);
      }
    });
  });

  return store;
}