UNPKG

lincd

Version:

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

72 lines 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NQuads = void 0; const models_1 = require("../models"); const Shape_1 = require("../shapes/Shape"); class NQuads { static fromGraphs(graphs, includeGraphs = true) { let res = ''; graphs.forEach((graph) => { res += this.fromQuads(graph.getContents(), includeGraphs); }); return res; } static fromQuads(quadset, includeGraphs = true, fixedGraph = null) { let resultString = ''; models_1.BlankNode.includeBlankNodes(quadset); quadset.forEach((quad) => { //we check for graph.node.uri not to be empty (as it can be for the default graph) //so that we always print an actual URI for the graph resultString += this.toString(quad.subject) + ' ' + this.toString(quad.predicate) + ' ' + this.toString(quad.object) + (fixedGraph && fixedGraph.node.uri !== '' ? ' ' + this.toString(fixedGraph) : (includeGraphs && quad.graph && quad.graph.node.uri !== '' ? ' ' + this.toString(quad.graph) : '')) + '.\n'; }); return resultString; } static checkUri(uri) { if (uri.startsWith(' ') || uri.endsWith(' ')) { throw new Error('URIs cannot start or end with a space: ' + uri); } } static toString(element, escapeNewLines = true) { if (element instanceof Shape_1.Shape) { return this.toString(element.node); } else if (element instanceof models_1.BlankNode) { return element.uri; } else if (element instanceof models_1.Graph) { this.checkUri(element.node.uri); return '<' + element.node.uri + '>'; } else if (element instanceof models_1.NamedNode) { this.checkUri(element.uri); return '<' + element.uri + '>'; } else if (element instanceof models_1.Literal) { //TODO: if there are every problems with this (the enters as escaped \\n) then we should check for indexOf \n //and if found we use quad quotes (add another 2 on both sides) return element.toString(); // return escapeNewLines ? element.toString().replace(/\n/g, "\\n") : element.toString(); } else if (element instanceof models_1.Quad) { return (this.toString(element.subject, escapeNewLines) + ' ' + this.toString(element.predicate, escapeNewLines) + ' ' + this.toString(element.object, escapeNewLines) + '.\n'); } else if (typeof element === 'string') { return '"' + (escapeNewLines ? element.replace(/\n/g, '\\n') : element) + '"'; } throw new Error('Unsupported type given, cannot convert to SPARQL'); } } exports.NQuads = NQuads; //# sourceMappingURL=NQuads.js.map