mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Support strings in addQuad.
This commit is contained in:
parent
a572825909
commit
feaac1cf56
@ -78,8 +78,8 @@ export class RepresentationMetadata {
|
|||||||
if (!Array.isArray(objects)) {
|
if (!Array.isArray(objects)) {
|
||||||
objects = [ objects ];
|
objects = [ objects ];
|
||||||
}
|
}
|
||||||
for (const object of objects.map(toObjectTerm)) {
|
for (const object of objects) {
|
||||||
this.store.addQuad(this.id, namedPredicate, object);
|
this.store.addQuad(this.id, namedPredicate, toObjectTerm(object, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,7 +149,7 @@ export class RepresentationMetadata {
|
|||||||
* @param object - Value to add.
|
* @param object - Value to add.
|
||||||
*/
|
*/
|
||||||
public add(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +159,7 @@ export class RepresentationMetadata {
|
|||||||
* @param object - Value to remove.
|
* @param object - Value to remove.
|
||||||
*/
|
*/
|
||||||
public remove(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,23 @@
|
|||||||
import type { Readable } from 'stream';
|
import type { Readable, PassThrough } from 'stream';
|
||||||
import arrayifyStream from 'arrayify-stream';
|
import arrayifyStream from 'arrayify-stream';
|
||||||
import { DataFactory, StreamParser, StreamWriter } from 'n3';
|
import { DataFactory, StreamParser, StreamWriter } from 'n3';
|
||||||
import type { Literal, NamedNode, Quad } from 'rdf-js';
|
import type { Literal, NamedNode, Quad } from 'rdf-js';
|
||||||
import streamifyArray from 'streamify-array';
|
import streamifyArray from 'streamify-array';
|
||||||
import type { Guarded } from './GuardedStream';
|
import type { Guarded } from './GuardedStream';
|
||||||
import { pipeSafely } from './StreamUtil';
|
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.
|
* Generates a quad with the given subject/predicate/object and pushes it to the given array.
|
||||||
*/
|
*/
|
||||||
export const pushQuad =
|
export const pushQuad = (
|
||||||
(quads: Quad[], subject: NamedNode, predicate: NamedNode, object: NamedNode | Literal): number =>
|
quads: Quad[] | PassThrough,
|
||||||
quads.push(DataFactory.quad(subject, predicate, object));
|
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.
|
* Helper function for serializing an array of quads, with as result a Readable object.
|
||||||
|
@ -2,6 +2,8 @@ import { DataFactory } from 'n3';
|
|||||||
import type { Literal, NamedNode, Term } from 'rdf-js';
|
import type { Literal, NamedNode, Term } from 'rdf-js';
|
||||||
import { CONTENT_TYPE } from './UriConstants';
|
import { CONTENT_TYPE } from './UriConstants';
|
||||||
|
|
||||||
|
const { namedNode, literal } = DataFactory;
|
||||||
|
|
||||||
// Shorthands for commonly used predicates
|
// Shorthands for commonly used predicates
|
||||||
const shorthands: Record<string, NamedNode> = {
|
const shorthands: Record<string, NamedNode> = {
|
||||||
contentType: DataFactory.namedNode(CONTENT_TYPE),
|
contentType: DataFactory.namedNode(CONTENT_TYPE),
|
||||||
@ -28,7 +30,7 @@ export const toCachedNamedNode = (name: NamedNode | string): NamedNode => {
|
|||||||
return shorthands[name];
|
return shorthands[name];
|
||||||
}
|
}
|
||||||
if (!termMap[name]) {
|
if (!termMap[name]) {
|
||||||
termMap[name] = DataFactory.namedNode(name);
|
termMap[name] = namedNode(name);
|
||||||
}
|
}
|
||||||
return termMap[name];
|
return termMap[name];
|
||||||
}
|
}
|
||||||
@ -36,11 +38,25 @@ export const toCachedNamedNode = (name: NamedNode | string): NamedNode => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an object to a literal when needed.
|
* Converts a subject to a named node when needed.
|
||||||
* @param object - Object to potentially transform.
|
* @param subject - Subject to potentially transform.
|
||||||
*/
|
*/
|
||||||
export const toObjectTerm = (object: NamedNode | Literal | string): NamedNode | Literal =>
|
export const toSubjectTerm = (subject: NamedNode | string): NamedNode =>
|
||||||
typeof object === 'string' ? DataFactory.literal(object) : object;
|
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.
|
* Creates a literal by first converting the dataType string to a named node.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import 'jest-rdf';
|
import 'jest-rdf';
|
||||||
import { DataFactory } from 'n3';
|
import { literal, namedNode, quad } from '@rdfjs/data-model';
|
||||||
import type { Quad } from 'rdf-js';
|
import type { Quad } from 'rdf-js';
|
||||||
import { parseQuads, pushQuad, serializeQuads } from '../../../src/util/QuadUtil';
|
import { parseQuads, pushQuad, serializeQuads } from '../../../src/util/QuadUtil';
|
||||||
import { guardedStreamFrom, readableToString } from '../../../src/util/StreamUtil';
|
import { guardedStreamFrom, readableToString } from '../../../src/util/StreamUtil';
|
||||||
@ -8,19 +8,27 @@ describe('QuadUtil', (): void => {
|
|||||||
describe('#pushQuad', (): void => {
|
describe('#pushQuad', (): void => {
|
||||||
it('creates a quad and adds it to the given array.', async(): Promise<void> => {
|
it('creates a quad and adds it to the given array.', async(): Promise<void> => {
|
||||||
const quads: Quad[] = [];
|
const quads: Quad[] = [];
|
||||||
pushQuad(quads, DataFactory.namedNode('sub'), DataFactory.namedNode('pred'), DataFactory.literal('obj'));
|
pushQuad(quads, namedNode('sub'), namedNode('pred'), literal('obj'));
|
||||||
expect(quads).toEqualRdfQuadArray([
|
expect(quads).toEqualRdfQuadArray([
|
||||||
DataFactory.quad(DataFactory.namedNode('sub'), DataFactory.namedNode('pred'), DataFactory.literal('obj')),
|
quad(namedNode('sub'), namedNode('pred'), literal('obj')),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates a quad from strings and adds it to the given array.', async(): Promise<void> => {
|
||||||
|
const quads: Quad[] = [];
|
||||||
|
pushQuad(quads, 'sub', 'pred', 'obj');
|
||||||
|
expect(quads).toEqualRdfQuadArray([
|
||||||
|
quad(namedNode('sub'), namedNode('pred'), namedNode('obj')),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#serializeQuads', (): void => {
|
describe('#serializeQuads', (): void => {
|
||||||
it('converts quads to the requested format.', async(): Promise<void> => {
|
it('converts quads to the requested format.', async(): Promise<void> => {
|
||||||
const quads = [ DataFactory.quad(
|
const quads = [ quad(
|
||||||
DataFactory.namedNode('pre:sub'),
|
namedNode('pre:sub'),
|
||||||
DataFactory.namedNode('pre:pred'),
|
namedNode('pre:pred'),
|
||||||
DataFactory.literal('obj'),
|
literal('obj'),
|
||||||
) ];
|
) ];
|
||||||
const stream = serializeQuads(quads, 'application/n-triples');
|
const stream = serializeQuads(quads, 'application/n-triples');
|
||||||
await expect(readableToString(stream)).resolves.toMatch('<pre:sub> <pre:pred> "obj" .');
|
await expect(readableToString(stream)).resolves.toMatch('<pre:sub> <pre:pred> "obj" .');
|
||||||
@ -30,10 +38,10 @@ describe('QuadUtil', (): void => {
|
|||||||
describe('#parseQuads', (): void => {
|
describe('#parseQuads', (): void => {
|
||||||
it('parses quads from the requested format.', async(): Promise<void> => {
|
it('parses quads from the requested format.', async(): Promise<void> => {
|
||||||
const stream = guardedStreamFrom([ '<pre:sub> <pre:pred> "obj".' ]);
|
const stream = guardedStreamFrom([ '<pre:sub> <pre:pred> "obj".' ]);
|
||||||
await expect(parseQuads(stream, 'application/n-triples')).resolves.toEqualRdfQuadArray([ DataFactory.quad(
|
await expect(parseQuads(stream, 'application/n-triples')).resolves.toEqualRdfQuadArray([ quad(
|
||||||
DataFactory.namedNode('pre:sub'),
|
namedNode('pre:sub'),
|
||||||
DataFactory.namedNode('pre:pred'),
|
namedNode('pre:pred'),
|
||||||
DataFactory.literal('obj'),
|
literal('obj'),
|
||||||
) ]);
|
) ]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
import 'jest-rdf';
|
import 'jest-rdf';
|
||||||
import { literal, namedNode } from '@rdfjs/data-model';
|
import { literal, namedNode } from '@rdfjs/data-model';
|
||||||
import { CONTENT_TYPE, XSD } from '../../../src/util/UriConstants';
|
import { CONTENT_TYPE, XSD } from '../../../src/util/UriConstants';
|
||||||
import { toCachedNamedNode, toObjectTerm, toLiteral, isTerm } from '../../../src/util/UriUtil';
|
import {
|
||||||
|
toCachedNamedNode,
|
||||||
|
toSubjectTerm,
|
||||||
|
toPredicateTerm,
|
||||||
|
toObjectTerm,
|
||||||
|
toLiteral,
|
||||||
|
isTerm,
|
||||||
|
} from '../../../src/util/UriUtil';
|
||||||
|
|
||||||
describe('An UriUtil', (): void => {
|
describe('An UriUtil', (): void => {
|
||||||
describe('isTerm function', (): void => {
|
describe('isTerm function', (): void => {
|
||||||
@ -14,7 +21,7 @@ describe('An UriUtil', (): void => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getNamedNode function', (): void => {
|
describe('toCachedNamedNode function', (): void => {
|
||||||
it('returns the input if it was a named node.', async(): Promise<void> => {
|
it('returns the input if it was a named node.', async(): Promise<void> => {
|
||||||
const term = namedNode('name');
|
const term = namedNode('name');
|
||||||
expect(toCachedNamedNode(term)).toBe(term);
|
expect(toCachedNamedNode(term)).toBe(term);
|
||||||
@ -35,7 +42,29 @@ describe('An UriUtil', (): void => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getObjectTerm function', (): void => {
|
describe('toSubjectTerm function', (): void => {
|
||||||
|
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||||
|
const nn = namedNode('name');
|
||||||
|
expect(toSubjectTerm(nn)).toBe(nn);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||||
|
expect(toSubjectTerm('nn')).toEqualRdfTerm(namedNode('nn'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toPredicateTerm function', (): void => {
|
||||||
|
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||||
|
const nn = namedNode('name');
|
||||||
|
expect(toPredicateTerm(nn)).toBe(nn);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||||
|
expect(toPredicateTerm('nn')).toEqualRdfTerm(namedNode('nn'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toObjectTerm function', (): void => {
|
||||||
it('returns the input if it was a term.', async(): Promise<void> => {
|
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||||
const nn = namedNode('name');
|
const nn = namedNode('name');
|
||||||
const lit = literal('lit');
|
const lit = literal('lit');
|
||||||
@ -43,12 +72,16 @@ describe('An UriUtil', (): void => {
|
|||||||
expect(toObjectTerm(lit)).toBe(lit);
|
expect(toObjectTerm(lit)).toBe(lit);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns a literal when a string is used.', async(): Promise<void> => {
|
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||||
expect(toObjectTerm('lit')).toEqualRdfTerm(literal('lit'));
|
expect(toObjectTerm('nn')).toEqualRdfTerm(namedNode('nn'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a literal when a string is used with preferLiteral.', async(): Promise<void> => {
|
||||||
|
expect(toObjectTerm('lit', true)).toEqualRdfTerm(literal('lit'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getTypedLiteral function', (): void => {
|
describe('toLiteral function', (): void => {
|
||||||
it('converts the input to a valid literal with the given type.', async(): Promise<void> => {
|
it('converts the input to a valid literal with the given type.', async(): Promise<void> => {
|
||||||
const expected = literal('5', namedNode(XSD.integer));
|
const expected = literal('5', namedNode(XSD.integer));
|
||||||
expect(toLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
|
expect(toLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user