UNPKG

lincd

Version:

LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)

97 lines 3.9 kB
import { defaultGraph as _default, Graph, Literal, NamedNode, Quad, } from './models.js'; import { NodeURIMappings } from './collections/NodeURIMappings.js'; import { QuadSet } from './collections/QuadSet.js'; import { CoreMap } from './collections/CoreMap.js'; import { CoreSet } from './collections/CoreSet.js'; export class Datafactory { constructor(config) { this.quads = new QuadSet(); this.emitEvents = true; this.triggerStorage = false; for (let key in config) { this[key] = config[key]; } if (!(config === null || config === void 0 ? void 0 : config.nodeMap)) { this.nodeMap = new NodeURIMappings(); } this.quad = this.quad.bind(this); this.blankNode = this.blankNode.bind(this); this.namedNode = this.namedNode.bind(this); this.literal = this.literal.bind(this); if (config === null || config === void 0 ? void 0 : config.overwriteData) { this.clearedProps = new CoreMap(); } } //TODO: // Variable variable(DOMString value); // Term fromTerm(Term original); // Quad fromQuad(Quad original); namedNode(uri) { return NamedNode.getOrCreate(uri); } literal(value, languageOrDatatype) { if (languageOrDatatype instanceof NamedNode) { return new Literal(value, languageOrDatatype); } else { return new Literal(value, null, languageOrDatatype); } } blankNode(value) { //when using start/end blanknode space you can let the factory reuse the same blank nodes // if (this.nodeMap) { return this.nodeMap.getOrCreateBlankNode(value); // } // return BlankNode.getOrCreate(value); } defaultGraph() { return _default; } quad(subject, predicate, object, graph) { //if a target graph is given, we always use that, regardless of whether there was any graph present in the data //else if a graph was in the data, use that, or fall back to default graph if (this.targetGraph) { graph = this.targetGraph; } else if (!graph) { graph = _default; } //in LINCD we use Graph objects which extend NamedNode //but when parsing with N3 we get NamedNode objects if (graph instanceof NamedNode) { graph = Graph.getOrCreate(graph.uri); } //sometimes we want to update the graph with new data coming in from JSONLD //so if overwrite data is true, we clear old data for any subj/pred combination we find if (this.overwriteData && (!this.clearedProps.has(subject) || !this.clearedProps .get(subject) .has(predicate))) { //remove without triggering storage events subject.getQuads(predicate).removeAll(false); if (!this.clearedProps.has(subject)) { this.clearedProps.set(subject, new CoreSet()); } this.clearedProps.get(subject).add(predicate); } let quad; if (this.preventNewQuads) { quad = Quad.get(subject, predicate, object, graph); if (quad) { this.quads.add(quad); } else { //NOTE: this is not standard, so preventNewQuads will not work with other tools //But it is useful for LINCD, where we want to prevent new quads from being created. Like when we share quads to be removed with other threads return true; } } else { quad = Quad.getOrCreate(subject, predicate, object, graph, false, this.triggerStorage, this.emitEvents); this.quads.add(quad); } return quad; } } //# sourceMappingURL=Datafactory.js.map