Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tpluscode/rdf-string

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tpluscode/rdf-string - npm Package Compare versions

Comparing version 0.2.5 to 0.2.6

dist/es/lib/ntriples.js

9

CHANGELOG.md

@@ -5,2 +5,11 @@ # Changelog

### [0.2.6](https://github.com/tpluscode/rdf-string/compare/v0.2.5...v0.2.6) (2020-02-27)
### Features
* **turtle:** compress output by removing subject/predicate repetition ([0d2f121](https://github.com/tpluscode/rdf-string/commit/0d2f121c510cd3327f56fc7317116db7e34685ca))
* **turtle:** options to do cheap compression ([618ffa0](https://github.com/tpluscode/rdf-string/commit/618ffa0d1ee12bc961fb95023b20b8c11f4c1959))
* added ntriples support ([62aef77](https://github.com/tpluscode/rdf-string/commit/62aef77774bcbe545cde840ca0492bb750fec108))
### [0.2.5](https://github.com/tpluscode/rdf-string/compare/v0.2.4...v0.2.5) (2020-02-26)

@@ -7,0 +16,0 @@

7

dist/es/index.js

@@ -1,3 +0,4 @@

export { sparql, SparqlTemplateResult } from './lib/sparql';
export { turtle, TurtleTemplateResult } from './lib/turtle';
export { nquads, NQuadsTemplateResult } from './lib/nquads';
export { sparql } from './lib/sparql';
export { turtle } from './lib/turtle';
export { nquads } from './lib/nquads';
export { ntriples } from './lib/ntriples';

@@ -1,12 +0,12 @@

import { TemplateResult } from './TemplateResult';
import { SerializationStrategy, TemplateResult } from './TemplateResult';
import * as ntriples from './syntax/ntriples';
export class NQuadsTemplateResult extends TemplateResult {
// eslint-disable-next-line no-useless-constructor
constructor(strings, values, turtle) {
super(strings, values, turtle);
}
_evaluateDataset(dataset, options) {
export class NQuadsStrategy extends SerializationStrategy {
evaluateDataset(dataset, options) {
const [first, ...rest] = dataset;
let firstQuadString = { value: '', prefixes: [] };
if (first) {
firstQuadString = this.evaluateQuad(first, options);
}
return rest.reduce((result, quad) => {
const nextQuad = this._evaluateQuad(quad, options);
const nextQuad = this.evaluateQuad(quad, options);
return {

@@ -16,9 +16,9 @@ value: `${result.value}\n${nextQuad.value}`,

};
}, this._evaluateQuad(first, options));
}, firstQuadString);
}
_evaluateQuad(quad, options) {
const subject = this._evaluateTerm(quad.subject, options);
const predicate = this._evaluateTerm(quad.predicate, options);
const object = this._evaluateTerm(quad.object, options);
const graph = this._evaluateTerm(quad.graph, options);
evaluateQuad(quad, options) {
const subject = this.evaluateTerm(quad.subject, options);
const predicate = this.evaluateTerm(quad.predicate, options);
const object = this.evaluateTerm(quad.object, options);
const graph = this.evaluateTerm(quad.graph, options);
return {

@@ -29,3 +29,3 @@ value: `${subject.value} ${predicate.value} ${object.value} ${graph.value} .`,

}
_evaluateLiteral(term) {
evaluateLiteral(term) {
return {

@@ -36,3 +36,3 @@ value: ntriples.literal(term),

}
_evaluateNamedNode(term) {
evaluateNamedNode(term) {
return {

@@ -43,3 +43,3 @@ value: ntriples.namedNode(term),

}
_evaluateBlankNode(term) {
evaluateBlankNode(term) {
return {

@@ -50,11 +50,20 @@ value: ntriples.blankNode(term),

}
get __defaultOptions() {
return {
sortGraphs: false,
};
}
_getFinalString(result) {
return result;
}
evaluateVariable() {
throw new Error('N-Quads cannot serialize variables');
}
getFinalString(result) {
return result;
}
}
export const nquads = (strings, ...values) => new NQuadsTemplateResult(strings, values, nquads);
export const nquads = (strings, ...values) => new TemplateResult({
strings,
values,
tag: nquads,
strategy: new NQuadsStrategy(),
defaultOptions: {
sortGraphs: false,
},
});
import { prefixes as knownPrefixes } from '@zazuko/rdf-vocabularies';
import { TemplateResult } from './TemplateResult';
import { SerializationStrategy, TemplateResult } from './TemplateResult';
import * as turtleSyntax from './syntax/turtle';

@@ -9,19 +9,10 @@ function prefixDeclarations(prefixes) {

}
export class SparqlTemplateResult extends TemplateResult {
// eslint-disable-next-line no-useless-constructor
constructor(strings, values, turtle) {
super(strings, values, turtle);
}
get __defaultOptions() {
return {
prologue: true,
};
}
_evaluateLiteral(term, options) {
export class SparqlStrategy extends SerializationStrategy {
evaluateLiteral(term, options) {
return turtleSyntax.literal(term, options.base);
}
_evaluateNamedNode(term, options) {
evaluateNamedNode(term, options) {
return turtleSyntax.namedNode(term, options.base);
}
_evaluateBlankNode(term) {
evaluateBlankNode(term) {
return {

@@ -32,3 +23,3 @@ value: turtleSyntax.blankNode(term),

}
_evaluateVariable(term) {
evaluateVariable(term) {
return {

@@ -39,3 +30,3 @@ value: `?${term.value}`,

}
_getFinalString(result, prefixes, options) {
getFinalString(result, prefixes, options) {
const prologue = options.prologue || typeof options.prologue === 'undefined';

@@ -54,3 +45,17 @@ let prologueLines = [];

}
evaluateDataset() {
throw new Error('Method not implemented');
}
evaluateQuad() {
throw new Error('Method not implemented');
}
}
export const sparql = (strings, ...values) => new SparqlTemplateResult(strings, values, sparql);
export const sparql = (strings, ...values) => new TemplateResult({
strings,
values,
tag: sparql,
strategy: new SparqlStrategy(),
defaultOptions: {
prologue: true,
},
});

@@ -0,25 +1,27 @@

export class SerializationStrategy {
evaluateTerm(value, options) {
switch (value.termType) {
case 'Literal':
return this.evaluateLiteral(value, options);
case 'NamedNode':
return this.evaluateNamedNode(value, options);
case 'BlankNode':
return this.evaluateBlankNode(value);
case 'Variable':
return this.evaluateVariable(value);
}
return {
value: '',
prefixes: [],
};
}
}
export class TemplateResult {
constructor(strings, values, tag) {
constructor({ strings, values, tag, strategy, defaultOptions, }) {
this.strings = strings;
this.values = values;
this._tag = tag;
this.__strategy = strategy;
this.__defaultOptions = defaultOptions;
}
_evaluateLiteral(term, options) {
throw new Error(`${this._tag.name} cannot interpolate literals`);
}
_evaluateNamedNode(term, options) {
throw new Error(`${this._tag.name} cannot interpolate named nodes`);
}
_evaluateVariable(term) {
throw new Error(`${this._tag.name} cannot interpolate variables`);
}
_evaluateBlankNode(term) {
throw new Error(`${this._tag.name} cannot interpolate blank nodes`);
}
_evaluateQuad(quad, options) {
throw new Error(`${this._tag.name} cannot interpolate quads`);
}
_evaluateDataset(dataset, options) {
throw new Error(`${this._tag.name} cannot interpolate datasets`);
}
toString(options) {

@@ -31,3 +33,3 @@ let actualOptions = this.__defaultOptions;

const { value, prefixes } = this._toPartialString(actualOptions);
return this._getFinalString(value, prefixes, actualOptions);
return this.__strategy.getFinalString(value, prefixes, actualOptions);
}

@@ -54,9 +56,9 @@ _toPartialString(options) {

else if ('subject' in value) {
partialResult = this._evaluateQuad(value, options);
partialResult = this.__strategy.evaluateQuad(value, options);
}
else if ('match' in value) {
partialResult = this._evaluateDataset(value, options);
partialResult = this.__strategy.evaluateDataset(value, options);
}
else {
partialResult = this._evaluateTerm(value, options);
partialResult = this.__strategy.evaluateTerm(value, options);
}

@@ -72,18 +74,2 @@ result += partialResult.value;

}
_evaluateTerm(value, options) {
switch (value.termType) {
case 'Literal':
return this._evaluateLiteral(value, options);
case 'NamedNode':
return this._evaluateNamedNode(value, options);
case 'BlankNode':
return this._evaluateBlankNode(value);
case 'Variable':
return this._evaluateVariable(value);
}
return {
value: '',
prefixes: [],
};
}
}
import { defaultGraph } from '@rdfjs/data-model';
import { prefixes as knownPrefixes } from '@zazuko/rdf-vocabularies';
import { TemplateResult } from './TemplateResult';
import { SerializationStrategy, TemplateResult } from './TemplateResult';
import * as syntax from './syntax/turtle';

@@ -10,15 +10,5 @@ function prefixDeclarations(prefixes) {

}
export class TurtleTemplateResult extends TemplateResult {
// eslint-disable-next-line no-useless-constructor
constructor(strings, values, turtle) {
super(strings, values, turtle);
}
get __defaultOptions() {
export class TurtleStrategy extends SerializationStrategy {
evaluateBlankNode(term) {
return {
directives: true,
graph: defaultGraph(),
};
}
_evaluateBlankNode(term) {
return {
value: syntax.blankNode(term),

@@ -28,19 +18,35 @@ prefixes: [],

}
_evaluateLiteral(term) {
evaluateLiteral(term) {
return syntax.literal(term);
}
_evaluateNamedNode(term) {
evaluateNamedNode(term) {
return syntax.namedNode(term);
}
_evaluateDataset(dataset, options) {
evaluateVariable() {
throw new Error('Turtle cannot serialize variables');
}
evaluateDataset(dataset, options) {
const graphQuads = [...dataset.match(null, null, null, options.graph)];
return graphQuads.reduce((result, quad) => {
const quadResult = this._evaluateQuad(quad, options);
if (options.cheapCompression) {
return this.__evaluateQuads(graphQuads, options);
}
const subjectMap = graphQuads.reduce((map, quad) => {
let quads = map.get(quad.subject.value);
if (!quads) {
quads = new Set();
map.set(quad.subject.value, quads);
}
quads.add(quad);
return map;
}, new Map());
return [...subjectMap.values()].reduce((result, quads, index) => {
const nextSubject = this.__evaluateQuads([...quads], options);
const separator = index ? '\n' : '';
return {
value: result.value + '\n' + quadResult.value,
prefixes: [...result.prefixes, ...quadResult.prefixes],
value: `${result.value}${separator}${nextSubject.value}`,
prefixes: [...result.prefixes, ...nextSubject.prefixes],
};
}, { value: '', prefixes: [] });
}
_evaluateQuad(quad, options) {
evaluateQuad(quad, options, { terminate = true, newLineAfterSubject = false } = {}) {
if (!options.graph.equals(quad.graph)) {

@@ -52,7 +58,8 @@ return {

}
const subject = this._evaluateTerm(quad.subject, options);
const predicate = this._evaluateTerm(quad.predicate, options);
const object = this._evaluateTerm(quad.object, options);
const subject = this.evaluateTerm(quad.subject, options);
const predicate = this.evaluateTerm(quad.predicate, options);
const object = this.evaluateTerm(quad.object, options);
const predicateSeparator = newLineAfterSubject ? '\n ' : ' ';
return {
value: `${subject.value} ${predicate.value} ${object.value} .`,
value: `${subject.value}${predicateSeparator}${predicate.value} ${object.value}${terminate ? ' .' : ''}`,
prefixes: [

@@ -65,3 +72,3 @@ ...subject.prefixes,

}
_getFinalString(result, prefixes, options) {
getFinalString(result, prefixes, options) {
const prologue = options.directives || typeof options.directives === 'undefined';

@@ -77,3 +84,76 @@ let prologueLines = [];

}
__evaluateQuads(quads, options) {
if (quads.length === 0) {
return {
value: '',
prefixes: [],
};
}
let orderedQuads = quads;
if (!options.cheapCompression) {
orderedQuads = quads.sort((left, right) => left.predicate.value.localeCompare(right.predicate.value));
}
const result = orderedQuads.reduce((context, quad) => {
if (!context.previous) {
return {
...this.evaluateQuad(quad, options, {
terminate: false,
newLineAfterSubject: true,
}),
previous: quad,
};
}
if (context.previous.subject.equals(quad.subject) && context.previous.predicate.equals(quad.predicate)) {
return {
...this.__appendObject(context, quad, options),
previous: quad,
};
}
if (context.previous.subject.equals(quad.subject)) {
return {
...this.__appendPredicateObject(context, quad, options),
previous: quad,
};
}
const quadResult = this.evaluateQuad(quad, options, {
terminate: false,
newLineAfterSubject: true,
});
return {
value: context.value + ' .\n' + quadResult.value,
prefixes: [...context.prefixes, ...quadResult.prefixes],
previous: quad,
};
}, { value: '', prefixes: [] });
return {
...result,
value: result.value + ' .',
};
}
__appendPredicateObject(context, quad, options) {
const currentPredicateResult = this.evaluateTerm(quad.predicate, options);
const currentObjectResult = this.evaluateTerm(quad.object, options);
return {
value: `${context.value} ;\n ${currentPredicateResult.value} ${currentObjectResult.value}`,
prefixes: [...context.prefixes, ...currentPredicateResult.prefixes, ...currentObjectResult.prefixes],
};
}
__appendObject(context, quad, options) {
const currentObjectResult = this.evaluateTerm(quad.object, options);
return {
value: `${context.value} ,\n ${currentObjectResult.value}`,
prefixes: [...context.prefixes, ...currentObjectResult.prefixes],
};
}
}
export const turtle = (strings, ...values) => new TurtleTemplateResult(strings, values, turtle);
export const turtle = (strings, ...values) => new TemplateResult({
strings,
values,
tag: turtle,
strategy: new TurtleStrategy(),
defaultOptions: {
directives: true,
graph: defaultGraph(),
cheapCompression: false,
},
});

@@ -5,8 +5,7 @@ "use strict";

exports.sparql = sparql_1.sparql;
exports.SparqlTemplateResult = sparql_1.SparqlTemplateResult;
var turtle_1 = require("./lib/turtle");
exports.turtle = turtle_1.turtle;
exports.TurtleTemplateResult = turtle_1.TurtleTemplateResult;
var nquads_1 = require("./lib/nquads");
exports.nquads = nquads_1.nquads;
exports.NQuadsTemplateResult = nquads_1.NQuadsTemplateResult;
var ntriples_1 = require("./lib/ntriples");
exports.ntriples = ntriples_1.ntriples;

@@ -12,11 +12,11 @@ "use strict";

const ntriples = __importStar(require("./syntax/ntriples"));
class NQuadsTemplateResult extends TemplateResult_1.TemplateResult {
// eslint-disable-next-line no-useless-constructor
constructor(strings, values, turtle) {
super(strings, values, turtle);
}
_evaluateDataset(dataset, options) {
class NQuadsStrategy extends TemplateResult_1.SerializationStrategy {
evaluateDataset(dataset, options) {
const [first, ...rest] = dataset;
let firstQuadString = { value: '', prefixes: [] };
if (first) {
firstQuadString = this.evaluateQuad(first, options);
}
return rest.reduce((result, quad) => {
const nextQuad = this._evaluateQuad(quad, options);
const nextQuad = this.evaluateQuad(quad, options);
return {

@@ -26,9 +26,9 @@ value: `${result.value}\n${nextQuad.value}`,

};
}, this._evaluateQuad(first, options));
}, firstQuadString);
}
_evaluateQuad(quad, options) {
const subject = this._evaluateTerm(quad.subject, options);
const predicate = this._evaluateTerm(quad.predicate, options);
const object = this._evaluateTerm(quad.object, options);
const graph = this._evaluateTerm(quad.graph, options);
evaluateQuad(quad, options) {
const subject = this.evaluateTerm(quad.subject, options);
const predicate = this.evaluateTerm(quad.predicate, options);
const object = this.evaluateTerm(quad.object, options);
const graph = this.evaluateTerm(quad.graph, options);
return {

@@ -39,3 +39,3 @@ value: `${subject.value} ${predicate.value} ${object.value} ${graph.value} .`,

}
_evaluateLiteral(term) {
evaluateLiteral(term) {
return {

@@ -46,3 +46,3 @@ value: ntriples.literal(term),

}
_evaluateNamedNode(term) {
evaluateNamedNode(term) {
return {

@@ -53,3 +53,3 @@ value: ntriples.namedNode(term),

}
_evaluateBlankNode(term) {
evaluateBlankNode(term) {
return {

@@ -60,12 +60,21 @@ value: ntriples.blankNode(term),

}
get __defaultOptions() {
return {
sortGraphs: false,
};
}
_getFinalString(result) {
return result;
}
evaluateVariable() {
throw new Error('N-Quads cannot serialize variables');
}
getFinalString(result) {
return result;
}
}
exports.NQuadsTemplateResult = NQuadsTemplateResult;
exports.nquads = (strings, ...values) => new NQuadsTemplateResult(strings, values, exports.nquads);
exports.NQuadsStrategy = NQuadsStrategy;
exports.nquads = (strings, ...values) => new TemplateResult_1.TemplateResult({
strings,
values,
tag: exports.nquads,
strategy: new NQuadsStrategy(),
defaultOptions: {
sortGraphs: false,
},
});

@@ -18,19 +18,10 @@ "use strict";

}
class SparqlTemplateResult extends TemplateResult_1.TemplateResult {
// eslint-disable-next-line no-useless-constructor
constructor(strings, values, turtle) {
super(strings, values, turtle);
}
get __defaultOptions() {
return {
prologue: true,
};
}
_evaluateLiteral(term, options) {
class SparqlStrategy extends TemplateResult_1.SerializationStrategy {
evaluateLiteral(term, options) {
return turtleSyntax.literal(term, options.base);
}
_evaluateNamedNode(term, options) {
evaluateNamedNode(term, options) {
return turtleSyntax.namedNode(term, options.base);
}
_evaluateBlankNode(term) {
evaluateBlankNode(term) {
return {

@@ -41,3 +32,3 @@ value: turtleSyntax.blankNode(term),

}
_evaluateVariable(term) {
evaluateVariable(term) {
return {

@@ -48,3 +39,3 @@ value: `?${term.value}`,

}
_getFinalString(result, prefixes, options) {
getFinalString(result, prefixes, options) {
const prologue = options.prologue || typeof options.prologue === 'undefined';

@@ -63,4 +54,18 @@ let prologueLines = [];

}
evaluateDataset() {
throw new Error('Method not implemented');
}
evaluateQuad() {
throw new Error('Method not implemented');
}
}
exports.SparqlTemplateResult = SparqlTemplateResult;
exports.sparql = (strings, ...values) => new SparqlTemplateResult(strings, values, exports.sparql);
exports.SparqlStrategy = SparqlStrategy;
exports.sparql = (strings, ...values) => new TemplateResult_1.TemplateResult({
strings,
values,
tag: exports.sparql,
strategy: new SparqlStrategy(),
defaultOptions: {
prologue: true,
},
});
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class SerializationStrategy {
evaluateTerm(value, options) {
switch (value.termType) {
case 'Literal':
return this.evaluateLiteral(value, options);
case 'NamedNode':
return this.evaluateNamedNode(value, options);
case 'BlankNode':
return this.evaluateBlankNode(value);
case 'Variable':
return this.evaluateVariable(value);
}
return {
value: '',
prefixes: [],
};
}
}
exports.SerializationStrategy = SerializationStrategy;
class TemplateResult {
constructor(strings, values, tag) {
constructor({ strings, values, tag, strategy, defaultOptions, }) {
this.strings = strings;
this.values = values;
this._tag = tag;
this.__strategy = strategy;
this.__defaultOptions = defaultOptions;
}
_evaluateLiteral(term, options) {
throw new Error(`${this._tag.name} cannot interpolate literals`);
}
_evaluateNamedNode(term, options) {
throw new Error(`${this._tag.name} cannot interpolate named nodes`);
}
_evaluateVariable(term) {
throw new Error(`${this._tag.name} cannot interpolate variables`);
}
_evaluateBlankNode(term) {
throw new Error(`${this._tag.name} cannot interpolate blank nodes`);
}
_evaluateQuad(quad, options) {
throw new Error(`${this._tag.name} cannot interpolate quads`);
}
_evaluateDataset(dataset, options) {
throw new Error(`${this._tag.name} cannot interpolate datasets`);
}
toString(options) {

@@ -33,3 +36,3 @@ let actualOptions = this.__defaultOptions;

const { value, prefixes } = this._toPartialString(actualOptions);
return this._getFinalString(value, prefixes, actualOptions);
return this.__strategy.getFinalString(value, prefixes, actualOptions);
}

@@ -56,9 +59,9 @@ _toPartialString(options) {

else if ('subject' in value) {
partialResult = this._evaluateQuad(value, options);
partialResult = this.__strategy.evaluateQuad(value, options);
}
else if ('match' in value) {
partialResult = this._evaluateDataset(value, options);
partialResult = this.__strategy.evaluateDataset(value, options);
}
else {
partialResult = this._evaluateTerm(value, options);
partialResult = this.__strategy.evaluateTerm(value, options);
}

@@ -74,19 +77,3 @@ result += partialResult.value;

}
_evaluateTerm(value, options) {
switch (value.termType) {
case 'Literal':
return this._evaluateLiteral(value, options);
case 'NamedNode':
return this._evaluateNamedNode(value, options);
case 'BlankNode':
return this._evaluateBlankNode(value);
case 'Variable':
return this._evaluateVariable(value);
}
return {
value: '',
prefixes: [],
};
}
}
exports.TemplateResult = TemplateResult;

@@ -19,15 +19,5 @@ "use strict";

}
class TurtleTemplateResult extends TemplateResult_1.TemplateResult {
// eslint-disable-next-line no-useless-constructor
constructor(strings, values, turtle) {
super(strings, values, turtle);
}
get __defaultOptions() {
class TurtleStrategy extends TemplateResult_1.SerializationStrategy {
evaluateBlankNode(term) {
return {
directives: true,
graph: data_model_1.defaultGraph(),
};
}
_evaluateBlankNode(term) {
return {
value: syntax.blankNode(term),

@@ -37,19 +27,35 @@ prefixes: [],

}
_evaluateLiteral(term) {
evaluateLiteral(term) {
return syntax.literal(term);
}
_evaluateNamedNode(term) {
evaluateNamedNode(term) {
return syntax.namedNode(term);
}
_evaluateDataset(dataset, options) {
evaluateVariable() {
throw new Error('Turtle cannot serialize variables');
}
evaluateDataset(dataset, options) {
const graphQuads = [...dataset.match(null, null, null, options.graph)];
return graphQuads.reduce((result, quad) => {
const quadResult = this._evaluateQuad(quad, options);
if (options.cheapCompression) {
return this.__evaluateQuads(graphQuads, options);
}
const subjectMap = graphQuads.reduce((map, quad) => {
let quads = map.get(quad.subject.value);
if (!quads) {
quads = new Set();
map.set(quad.subject.value, quads);
}
quads.add(quad);
return map;
}, new Map());
return [...subjectMap.values()].reduce((result, quads, index) => {
const nextSubject = this.__evaluateQuads([...quads], options);
const separator = index ? '\n' : '';
return {
value: result.value + '\n' + quadResult.value,
prefixes: [...result.prefixes, ...quadResult.prefixes],
value: `${result.value}${separator}${nextSubject.value}`,
prefixes: [...result.prefixes, ...nextSubject.prefixes],
};
}, { value: '', prefixes: [] });
}
_evaluateQuad(quad, options) {
evaluateQuad(quad, options, { terminate = true, newLineAfterSubject = false } = {}) {
if (!options.graph.equals(quad.graph)) {

@@ -61,7 +67,8 @@ return {

}
const subject = this._evaluateTerm(quad.subject, options);
const predicate = this._evaluateTerm(quad.predicate, options);
const object = this._evaluateTerm(quad.object, options);
const subject = this.evaluateTerm(quad.subject, options);
const predicate = this.evaluateTerm(quad.predicate, options);
const object = this.evaluateTerm(quad.object, options);
const predicateSeparator = newLineAfterSubject ? '\n ' : ' ';
return {
value: `${subject.value} ${predicate.value} ${object.value} .`,
value: `${subject.value}${predicateSeparator}${predicate.value} ${object.value}${terminate ? ' .' : ''}`,
prefixes: [

@@ -74,3 +81,3 @@ ...subject.prefixes,

}
_getFinalString(result, prefixes, options) {
getFinalString(result, prefixes, options) {
const prologue = options.directives || typeof options.directives === 'undefined';

@@ -86,4 +93,65 @@ let prologueLines = [];

}
__evaluateQuads(quads, options) {
if (quads.length === 0) {
return {
value: '',
prefixes: [],
};
}
let orderedQuads = quads;
if (!options.cheapCompression) {
orderedQuads = quads.sort((left, right) => left.predicate.value.localeCompare(right.predicate.value));
}
const result = orderedQuads.reduce((context, quad) => {
if (!context.previous) {
return Object.assign(Object.assign({}, this.evaluateQuad(quad, options, {
terminate: false,
newLineAfterSubject: true,
})), { previous: quad });
}
if (context.previous.subject.equals(quad.subject) && context.previous.predicate.equals(quad.predicate)) {
return Object.assign(Object.assign({}, this.__appendObject(context, quad, options)), { previous: quad });
}
if (context.previous.subject.equals(quad.subject)) {
return Object.assign(Object.assign({}, this.__appendPredicateObject(context, quad, options)), { previous: quad });
}
const quadResult = this.evaluateQuad(quad, options, {
terminate: false,
newLineAfterSubject: true,
});
return {
value: context.value + ' .\n' + quadResult.value,
prefixes: [...context.prefixes, ...quadResult.prefixes],
previous: quad,
};
}, { value: '', prefixes: [] });
return Object.assign(Object.assign({}, result), { value: result.value + ' .' });
}
__appendPredicateObject(context, quad, options) {
const currentPredicateResult = this.evaluateTerm(quad.predicate, options);
const currentObjectResult = this.evaluateTerm(quad.object, options);
return {
value: `${context.value} ;\n ${currentPredicateResult.value} ${currentObjectResult.value}`,
prefixes: [...context.prefixes, ...currentPredicateResult.prefixes, ...currentObjectResult.prefixes],
};
}
__appendObject(context, quad, options) {
const currentObjectResult = this.evaluateTerm(quad.object, options);
return {
value: `${context.value} ,\n ${currentObjectResult.value}`,
prefixes: [...context.prefixes, ...currentObjectResult.prefixes],
};
}
}
exports.TurtleTemplateResult = TurtleTemplateResult;
exports.turtle = (strings, ...values) => new TurtleTemplateResult(strings, values, exports.turtle);
exports.TurtleStrategy = TurtleStrategy;
exports.turtle = (strings, ...values) => new TemplateResult_1.TemplateResult({
strings,
values,
tag: exports.turtle,
strategy: new TurtleStrategy(),
defaultOptions: {
directives: true,
graph: data_model_1.defaultGraph(),
cheapCompression: false,
},
});
export { sparql, SparqlTemplateResult, SparqlValue } from './lib/sparql';
export { turtle, TurtleTemplateResult, TurtleValue } from './lib/turtle';
export { nquads, NQuadsTemplateResult, NQuadsValue } from './lib/nquads';
export { ntriples, NTriplesTemplateResult, NTriplesValue } from './lib/ntriples';
//# sourceMappingURL=index.d.ts.map
import { BlankNode, DatasetCore, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { Value } from './value';
import { PartialString, TemplateResult } from './TemplateResult';
import { PartialString, SerializationStrategy, TemplateResult } from './TemplateResult';
export declare type NQuadsValue<T extends Term = Term> = Value<NQuadsTemplateResult, T>;

@@ -8,14 +8,15 @@ interface NQuadsOptions {

}
export declare class NQuadsTemplateResult extends TemplateResult<NQuadsTemplateResult, NQuadsValue, NQuadsOptions> {
constructor(strings: TemplateStringsArray, values: NQuadsValue[], turtle: (strings: TemplateStringsArray, ...values: NQuadsValue<any>[]) => NQuadsTemplateResult);
protected _evaluateDataset(dataset: DatasetCore, options: NQuadsOptions): PartialString;
protected _evaluateQuad(quad: Quad, options: NQuadsOptions): PartialString;
protected _evaluateLiteral(term: Literal): PartialString;
protected _evaluateNamedNode(term: NamedNode): PartialString;
protected _evaluateBlankNode(term: BlankNode): PartialString;
protected get __defaultOptions(): NQuadsOptions;
export declare type NQuadsTemplateResult = TemplateResult<NQuadsOptions>;
export declare class NQuadsStrategy<TOptions extends NQuadsOptions = NQuadsOptions> extends SerializationStrategy<TOptions> {
evaluateDataset(dataset: DatasetCore, options: TOptions): PartialString;
evaluateQuad(quad: Quad, options: TOptions): PartialString;
evaluateLiteral(term: Literal): PartialString;
evaluateNamedNode(term: NamedNode): PartialString;
evaluateBlankNode(term: BlankNode): PartialString;
protected _getFinalString(result: string): string;
evaluateVariable(): PartialString;
getFinalString(result: string): string;
}
export declare const nquads: (strings: TemplateStringsArray, ...values: Value<NQuadsTemplateResult, NamedNode | BlankNode | Literal>[]) => NQuadsTemplateResult;
export declare const nquads: (strings: TemplateStringsArray, ...values: Value<TemplateResult<NQuadsOptions>, NamedNode | BlankNode | Literal>[]) => TemplateResult<NQuadsOptions>;
export {};
//# sourceMappingURL=nquads.d.ts.map
import { BlankNode, Literal, NamedNode, Term, Variable } from 'rdf-js';
import { Value } from './value';
import { PartialString, TemplateResult } from './TemplateResult';
import { PartialString, SerializationStrategy, TemplateResult } from './TemplateResult';
export declare type SparqlValue<T extends Term = Term> = Value<SparqlTemplateResult, T>;
interface SparqlOptions {
base?: string;
prologue?: boolean;
prologue: boolean;
}
export declare class SparqlTemplateResult extends TemplateResult<SparqlTemplateResult, SparqlValue, SparqlOptions> {
constructor(strings: TemplateStringsArray, values: SparqlValue[], turtle: (strings: TemplateStringsArray, ...values: SparqlValue<any>[]) => SparqlTemplateResult);
protected get __defaultOptions(): SparqlOptions;
protected _evaluateLiteral(term: Literal, options: SparqlOptions): PartialString;
protected _evaluateNamedNode(term: NamedNode, options: SparqlOptions): PartialString;
protected _evaluateBlankNode(term: BlankNode): PartialString;
protected _evaluateVariable(term: Variable): PartialString;
protected _getFinalString(result: string, prefixes: Iterable<string>, options: SparqlOptions): string;
export declare type SparqlTemplateResult = TemplateResult<SparqlOptions>;
export declare class SparqlStrategy extends SerializationStrategy<SparqlOptions> {
evaluateLiteral(term: Literal, options: SparqlOptions): PartialString;
evaluateNamedNode(term: NamedNode, options: SparqlOptions): PartialString;
evaluateBlankNode(term: BlankNode): PartialString;
evaluateVariable(term: Variable): PartialString;
getFinalString(result: string, prefixes: Iterable<string>, options: SparqlOptions): string;
evaluateDataset(): PartialString;
evaluateQuad(): PartialString;
}
export declare const sparql: (strings: TemplateStringsArray, ...values: Value<SparqlTemplateResult, Term>[]) => SparqlTemplateResult;
export declare const sparql: (strings: TemplateStringsArray, ...values: Value<SparqlTemplateResult, Term>[]) => TemplateResult<SparqlOptions>;
export {};
//# sourceMappingURL=sparql.d.ts.map
import { BlankNode, DatasetCore, Literal, NamedNode, Quad, Term, Variable } from 'rdf-js';
import { Value } from './value';
declare type TagFunction<TImpl extends TemplateResult<TImpl, TValue, TOptions>, TValue extends Value<TImpl>, TOptions> = {
declare type TagFunction<TImpl extends TemplateResult<TOptions>, TValue extends Value<TImpl>, TOptions> = {
(strings: TemplateStringsArray, ...values: TValue[]): TImpl;

@@ -10,20 +10,30 @@ };

}
export declare abstract class TemplateResult<TImpl extends TemplateResult<TImpl, TValue, TOptions>, TValue extends Value<TImpl>, TOptions> {
export declare abstract class SerializationStrategy<TOptions> {
abstract evaluateLiteral(term: Literal, options: TOptions): PartialString;
abstract evaluateNamedNode(term: NamedNode, options: TOptions): PartialString;
abstract evaluateVariable(term: Variable): PartialString;
abstract evaluateBlankNode(term: BlankNode): PartialString;
abstract evaluateQuad(quad: Quad, options: TOptions): PartialString;
abstract evaluateDataset(dataset: DatasetCore, options: TOptions): PartialString;
abstract getFinalString(result: string, prefixes: Iterable<string>, options: TOptions): string;
evaluateTerm(value: Term, options: TOptions): PartialString;
}
export interface TemplateResultInit<TOptions> {
strings: TemplateStringsArray;
values: Value<any>[];
tag: TagFunction<any, Value<any>, TOptions>;
strategy: SerializationStrategy<TOptions>;
defaultOptions: TOptions;
}
export declare class TemplateResult<TOptions> {
protected readonly strings: TemplateStringsArray;
protected readonly values: readonly Value<TImpl>[];
protected readonly _tag: TagFunction<TImpl, DatasetCore | Quad | TImpl | Term | string | undefined | null, TOptions>;
protected constructor(strings: TemplateStringsArray, values: Value<TImpl>[], tag: TagFunction<TImpl, Value<TImpl>, TOptions>);
protected abstract get __defaultOptions(): TOptions;
protected abstract _getFinalString(result: string, prefixes: Iterable<string>, options: TOptions): string;
protected _evaluateLiteral(term: Literal, options: TOptions): PartialString;
protected _evaluateNamedNode(term: NamedNode, options: TOptions): PartialString;
protected _evaluateVariable(term: Variable): PartialString;
protected _evaluateBlankNode(term: BlankNode): PartialString;
protected _evaluateQuad(quad: Quad, options: TOptions): PartialString;
protected _evaluateDataset(dataset: DatasetCore, options: TOptions): PartialString;
toString(options?: TOptions): string;
protected readonly values: readonly Value<TemplateResult<TOptions>>[];
protected readonly _tag: TagFunction<TemplateResult<TOptions>, DatasetCore | Quad | Term | string | undefined | null, TOptions>;
private readonly __strategy;
private readonly __defaultOptions;
constructor({ strings, values, tag, strategy, defaultOptions, }: TemplateResultInit<TOptions>);
toString(options?: Partial<TOptions>): string;
protected _toPartialString(options: TOptions): PartialString;
protected _evaluateTerm(value: Term, options: TOptions): PartialString;
}
export {};
//# sourceMappingURL=TemplateResult.d.ts.map
import { BlankNode, DatasetCore, DefaultGraph, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { Value } from './value';
import { PartialString, TemplateResult } from './TemplateResult';
import { PartialString, SerializationStrategy, TemplateResult } from './TemplateResult';
export declare type TurtleValue<T extends Term = Term> = Value<TurtleTemplateResult, T>;
interface TurtleOptions {
directives?: boolean;
directives: boolean;
graph: NamedNode | DefaultGraph;
cheapCompression: boolean;
}
export declare class TurtleTemplateResult extends TemplateResult<TurtleTemplateResult, TurtleValue, TurtleOptions> {
constructor(strings: TemplateStringsArray, values: TurtleValue[], turtle: (strings: TemplateStringsArray, ...values: TurtleValue<any>[]) => TurtleTemplateResult);
protected get __defaultOptions(): TurtleOptions;
protected _evaluateBlankNode(term: BlankNode): PartialString;
protected _evaluateLiteral(term: Literal): PartialString;
protected _evaluateNamedNode(term: NamedNode): PartialString;
protected _evaluateDataset(dataset: DatasetCore, options: TurtleOptions): PartialString;
protected _evaluateQuad(quad: Quad, options: TurtleOptions): PartialString;
protected _getFinalString(result: string, prefixes: Iterable<string>, options: TurtleOptions): string;
export declare type TurtleTemplateResult = TemplateResult<TurtleOptions>;
export declare class TurtleStrategy extends SerializationStrategy<TurtleOptions> {
evaluateBlankNode(term: BlankNode): PartialString;
evaluateLiteral(term: Literal): PartialString;
evaluateNamedNode(term: NamedNode): PartialString;
evaluateVariable(): PartialString;
evaluateDataset(dataset: DatasetCore, options: TurtleOptions): PartialString;
evaluateQuad(quad: Quad, options: TurtleOptions, { terminate, newLineAfterSubject }?: {
terminate?: boolean | undefined;
newLineAfterSubject?: boolean | undefined;
}): PartialString;
getFinalString(result: string, prefixes: Iterable<string>, options: TurtleOptions): string;
private __evaluateQuads;
private __appendPredicateObject;
private __appendObject;
}
export declare const turtle: (strings: TemplateStringsArray, ...values: Value<TurtleTemplateResult, NamedNode | BlankNode | Literal>[]) => TurtleTemplateResult;
export declare const turtle: (strings: TemplateStringsArray, ...values: Value<TemplateResult<TurtleOptions>, NamedNode | BlankNode | Literal>[]) => TemplateResult<TurtleOptions>;
export {};
//# sourceMappingURL=turtle.d.ts.map
{
"name": "@tpluscode/rdf-string",
"version": "0.2.5",
"version": "0.2.6",
"description": "Simplifies creating of RDF strings using ECMAscript templates",

@@ -39,2 +39,3 @@ "main": "dist/node/index.js",

"@types/rdf-ext": "^1.3.5",
"@types/rdfjs__dataset": "^1.0.0",
"@types/rdfjs__formats-common": "^2.0.0",

@@ -41,0 +42,0 @@ "@types/rdfjs__namespace": "^1.1.1",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc