UNPKG

rdf-data-factory

Version:

A TypeScript/JavaScript implementation of the RDF/JS data factory.

125 lines 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataFactory = void 0; const BlankNode_1 = require("./BlankNode"); const DefaultGraph_1 = require("./DefaultGraph"); const Literal_1 = require("./Literal"); const NamedNode_1 = require("./NamedNode"); const Quad_1 = require("./Quad"); const Variable_1 = require("./Variable"); let dataFactoryCounter = 0; /** * A factory for instantiating RDF terms and quads. */ class DataFactory { constructor(options) { this.blankNodeCounter = 0; options = options || {}; this.blankNodePrefix = options.blankNodePrefix || `df_${dataFactoryCounter++}_`; } /** * @param value The IRI for the named node. * @return A new instance of NamedNode. * @see NamedNode */ namedNode(value) { return new NamedNode_1.NamedNode(value); } /** * @param value The optional blank node identifier. * @return A new instance of BlankNode. * If the `value` parameter is undefined a new identifier * for the blank node is generated for each call. * @see BlankNode */ blankNode(value) { return new BlankNode_1.BlankNode(value || `${this.blankNodePrefix}${this.blankNodeCounter++}`); } /** * @param value The literal value. * @param languageOrDatatype The optional language, datatype, or directional language. * If `languageOrDatatype` is a NamedNode, * then it is used for the value of `NamedNode.datatype`. * If `languageOrDatatype` is a NamedNode, it is used for the value * of `NamedNode.language`. * Otherwise, it is used as a directional language, * from which the language is set to `languageOrDatatype.language` * and the direction to `languageOrDatatype.direction`. * @return A new instance of Literal. * @see Literal */ literal(value, languageOrDatatype) { return new Literal_1.Literal(value, languageOrDatatype); } /** * This method is optional. * @param value The variable name * @return A new instance of Variable. * @see Variable */ variable(value) { return new Variable_1.Variable(value); } /** * @return An instance of DefaultGraph. */ defaultGraph() { return DefaultGraph_1.DefaultGraph.INSTANCE; } /** * @param subject The quad subject term. * @param predicate The quad predicate term. * @param object The quad object term. * @param graph The quad graph term. * @return A new instance of Quad. * @see Quad */ quad(subject, predicate, object, graph) { return new Quad_1.Quad(subject, predicate, object, graph || this.defaultGraph()); } /** * Create a deep copy of the given term using this data factory. * @param original An RDF term. * @return A deep copy of the given term. */ fromTerm(original) { // TODO: remove nasty any casts when this TS bug has been fixed: // https://github.com/microsoft/TypeScript/issues/26933 switch (original.termType) { case 'NamedNode': return this.namedNode(original.value); case 'BlankNode': return this.blankNode(original.value); case 'Literal': if (original.language) { return this.literal(original.value, original.language); } if (!original.datatype.equals(Literal_1.Literal.XSD_STRING)) { return this.literal(original.value, this.fromTerm(original.datatype)); } return this.literal(original.value); case 'Variable': return this.variable(original.value); case 'DefaultGraph': return this.defaultGraph(); case 'Quad': return this.quad(this.fromTerm(original.subject), this.fromTerm(original.predicate), this.fromTerm(original.object), this.fromTerm(original.graph)); } } /** * Create a deep copy of the given quad using this data factory. * @param original An RDF quad. * @return A deep copy of the given quad. */ fromQuad(original) { return this.fromTerm(original); } /** * Reset the internal blank node counter. */ resetBlankNodeCounter() { this.blankNodeCounter = 0; } } exports.DataFactory = DataFactory; //# sourceMappingURL=DataFactory.js.map