view src/layout/fetchAndParse.ts @ 122:2e8fa3fec0c8

support joining subjects into wider rows
author drewp@bigasterisk.com
date Sun, 20 Mar 2022 14:12:03 -0700
parents 2468f2227d22
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;
}