UNPKG

rdf-dataset-fragmenter

Version:
43 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuadTransformerDistributeIri = void 0; const rdf_data_factory_1 = require("rdf-data-factory"); const QuadTransformerTerms_1 = require("./QuadTransformerTerms"); const DF = new rdf_data_factory_1.DataFactory(); /** * A quad transformer that that replaces (parts of) IRIs, * deterministically distributing the replacements over a list of multiple destination IRI, based on a matched number. * * This requires at least one group-based replacement, of which the first group must match a number. * * The matched number is used to choose one of the `replacementStrings` in a deterministic way: * replacementStrings[number % replacementStrings.length] */ class QuadTransformerDistributeIri extends QuadTransformerTerms_1.QuadTransformerTerms { search; replacements; constructor(searchRegex, replacementStrings) { super(); this.search = new RegExp(searchRegex, 'u'); this.replacements = replacementStrings; } transformTerm(term) { if (term.termType === 'NamedNode') { const match = this.search.exec(term.value); if (match) { if (match.length < 2) { throw new Error(`'searchRegex' did not contain any groups, while QuadTransformerDistributeIri requires at least one group-based replacement, of which the first group must match a number.`); } const nr = Number.parseInt(match[1], 10); if (Number.isNaN(nr)) { throw new TypeError(`The first capture group in 'searchRegex' must always match a number, but it matched "${match[1]}" instead.`); } const value = term.value.replace(this.search, this.replacements[nr % this.replacements.length]); return DF.namedNode(value); } } return term; } } exports.QuadTransformerDistributeIri = QuadTransformerDistributeIri; //# sourceMappingURL=QuadTransformerDistributeIri.js.map