feat: Support strings in addQuad.

This commit is contained in:
Ruben Verborgh
2021-01-02 18:13:28 +01:00
parent a572825909
commit feaac1cf56
5 changed files with 93 additions and 30 deletions

View File

@@ -78,8 +78,8 @@ export class RepresentationMetadata {
if (!Array.isArray(objects)) {
objects = [ objects ];
}
for (const object of objects.map(toObjectTerm)) {
this.store.addQuad(this.id, namedPredicate, object);
for (const object of objects) {
this.store.addQuad(this.id, namedPredicate, toObjectTerm(object, true));
}
}
}
@@ -149,7 +149,7 @@ export class RepresentationMetadata {
* @param object - Value to add.
*/
public add(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
this.store.addQuad(this.id, toCachedNamedNode(predicate), toObjectTerm(object));
this.store.addQuad(this.id, toCachedNamedNode(predicate), toObjectTerm(object, true));
return this;
}
@@ -159,7 +159,7 @@ export class RepresentationMetadata {
* @param object - Value to remove.
*/
public remove(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
this.store.removeQuad(this.id, toCachedNamedNode(predicate), toObjectTerm(object));
this.store.removeQuad(this.id, toCachedNamedNode(predicate), toObjectTerm(object, true));
return this;
}

View File

@@ -1,17 +1,23 @@
import type { Readable } from 'stream';
import type { Readable, PassThrough } from 'stream';
import arrayifyStream from 'arrayify-stream';
import { DataFactory, StreamParser, StreamWriter } from 'n3';
import type { Literal, NamedNode, Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
import type { Guarded } from './GuardedStream';
import { pipeSafely } from './StreamUtil';
import { toSubjectTerm, toPredicateTerm, toObjectTerm } from './UriUtil';
/**
* Generates a quad with the given subject/predicate/object and pushes it to the given array.
*/
export const pushQuad =
(quads: Quad[], subject: NamedNode, predicate: NamedNode, object: NamedNode | Literal): number =>
quads.push(DataFactory.quad(subject, predicate, object));
export const pushQuad = (
quads: Quad[] | PassThrough,
subject: string | NamedNode,
predicate: string | NamedNode,
object: string | NamedNode | Literal,
): void => {
quads.push(DataFactory.quad(toSubjectTerm(subject), toPredicateTerm(predicate), toObjectTerm(object)));
};
/**
* Helper function for serializing an array of quads, with as result a Readable object.

View File

@@ -2,6 +2,8 @@ import { DataFactory } from 'n3';
import type { Literal, NamedNode, Term } from 'rdf-js';
import { CONTENT_TYPE } from './UriConstants';
const { namedNode, literal } = DataFactory;
// Shorthands for commonly used predicates
const shorthands: Record<string, NamedNode> = {
contentType: DataFactory.namedNode(CONTENT_TYPE),
@@ -28,7 +30,7 @@ export const toCachedNamedNode = (name: NamedNode | string): NamedNode => {
return shorthands[name];
}
if (!termMap[name]) {
termMap[name] = DataFactory.namedNode(name);
termMap[name] = namedNode(name);
}
return termMap[name];
}
@@ -36,11 +38,25 @@ export const toCachedNamedNode = (name: NamedNode | string): NamedNode => {
};
/**
* Converts an object to a literal when needed.
* @param object - Object to potentially transform.
* Converts a subject to a named node when needed.
* @param subject - Subject to potentially transform.
*/
export const toObjectTerm = (object: NamedNode | Literal | string): NamedNode | Literal =>
typeof object === 'string' ? DataFactory.literal(object) : object;
export const toSubjectTerm = (subject: NamedNode | string): NamedNode =>
typeof subject === 'string' ? namedNode(subject) : subject;
export const toPredicateTerm = toSubjectTerm;
/**
* Converts an object term when needed.
* @param object - Object to potentially transform.
* @param preferLiteral - Whether strings are converted to literals or named nodes.
*/
export const toObjectTerm = <T extends Term>(object: T | string, preferLiteral = false): T => {
if (typeof object === 'string') {
return (preferLiteral ? literal(object) : namedNode(object)) as any;
}
return object;
};
/**
* Creates a literal by first converting the dataType string to a named node.