UNPKG

@rdfc/sparql-ingest-processor-ts

Version:
41 lines (40 loc) 1.52 kB
import { XSD } from "@treecg/types"; import { DataFactory } from "rdf-data-factory"; const df = new DataFactory(); export function getSubjects(store, predicate, object, graph) { return store.getQuads(null, predicate, object, graph).map((quad) => { return quad.subject; }); } export function getObjects(store, subject, predicate, graph) { return store.getQuads(subject, predicate, null, graph).map((quad) => { return quad.object; }); } export function sanitizeQuads(store) { for (const q of store.getQuads()) { if (q.object.termType === "Literal" && q.object.datatype.value === XSD.integer) { if (/\+\d+/.test(q.object.value) && q.object.value.startsWith("+")) { store.removeQuad(q); store.addQuad(df.quad(q.subject, q.predicate, df.literal(q.object.value.substring(1), df.namedNode(XSD.integer)), q.graph)); } } } } export async function doSPARQLRequest(query, url) { const res = await fetch(url, { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', }, body: `update=${fixedEncodeURIComponent(query)}` }); if (!res.ok) { throw new Error(`HTTP request failed with code ${res.status} and message: \n${await res.text()}`); } } function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { return '%' + c.charCodeAt(0).toString(16); }); }