UNPKG

@rdfc/sds-storage-writer-ts

Version:

An RDF-Connect processor to write SDS streams into a given storage system

48 lines (47 loc) 1.71 kB
import { DataFactory, Parser } from "n3"; import canonize from "rdf-canonize"; const df = DataFactory; export function maybe_parse(data) { if (typeof data === "string" || data instanceof String) { const parse = new Parser(); return parse.parse(data); } else { return data; } } export function filterMember(quads, id, blacklist = [], done) { const d = done === undefined ? new Set() : done; const quadIsBlacklisted = (q) => blacklist.some((b) => b(q, id)); d.add(id.value); const out = quads.filter((q) => q.subject.equals(id) && !quadIsBlacklisted(q)); const newObjects = quads .filter((q) => q.subject.equals(id) && !quadIsBlacklisted(q)) .map((q) => q.object) .filter((o) => o.termType === "BlankNode" || o.termType === "NamedNode"); for (const id of newObjects) { if (d.has(id.value)) continue; out.push(...filterMember(quads, id, blacklist, d)); } const newSubjects = quads .filter((q) => q.object.equals(id) && !quadIsBlacklisted(q)) .map((q) => q.subject) .filter((o) => o.termType === "BlankNode" || o.termType === "NamedNode"); for (const id of newSubjects) { if (d.has(id.value)) continue; out.push(...filterMember(quads, id, blacklist, d)); } return out; } export async function pathString(thing) { if (!thing) { return; } const quads = [ df.quad(df.namedNode(""), df.namedNode("http://purl.org/dc/terms/subject"), thing.id), ...thing.quads.map((x) => df.quad(x.subject, x.predicate, x.object)), ]; return await canonize.canonize(quads, { algorithm: "RDFC-1.0" }); }