mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Rename UriUtil functions
Forgot to do this when rebasing
This commit is contained in:
parent
e349e04119
commit
e1533a0869
@ -1,6 +1,6 @@
|
||||
import { DataFactory, Store } from 'n3';
|
||||
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js';
|
||||
import { getObjectTerm, getNamedNode, isTerm } from '../../util/UriUtil';
|
||||
import { toObjectTerm, toNamedNode, isTerm } from '../../util/UriUtil';
|
||||
|
||||
export type MetadataOverrideValue = NamedNode | Literal | string | (NamedNode | Literal | string)[];
|
||||
|
||||
@ -58,14 +58,14 @@ export class RepresentationMetadata {
|
||||
|
||||
private setOverrides(overrides: { [pred: string]: MetadataOverrideValue}): void {
|
||||
for (const predicate of Object.keys(overrides)) {
|
||||
const namedPredicate = getNamedNode(predicate);
|
||||
const namedPredicate = toNamedNode(predicate);
|
||||
this.removeAll(namedPredicate);
|
||||
|
||||
let objects = overrides[predicate];
|
||||
if (!Array.isArray(objects)) {
|
||||
objects = [ objects ];
|
||||
}
|
||||
for (const object of objects.map(getObjectTerm)) {
|
||||
for (const object of objects.map(toObjectTerm)) {
|
||||
this.store.addQuad(this.id, namedPredicate, object);
|
||||
}
|
||||
}
|
||||
@ -136,7 +136,7 @@ export class RepresentationMetadata {
|
||||
* @param object - Value to add.
|
||||
*/
|
||||
public add(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
|
||||
this.store.addQuad(this.id, getNamedNode(predicate), getObjectTerm(object));
|
||||
this.store.addQuad(this.id, toNamedNode(predicate), toObjectTerm(object));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -146,7 +146,7 @@ export class RepresentationMetadata {
|
||||
* @param object - Value to remove.
|
||||
*/
|
||||
public remove(predicate: NamedNode | string, object: NamedNode | Literal | string): this {
|
||||
this.store.removeQuad(this.id, getNamedNode(predicate), getObjectTerm(object));
|
||||
this.store.removeQuad(this.id, toNamedNode(predicate), toObjectTerm(object));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ export class RepresentationMetadata {
|
||||
* @param predicate - Predicate to remove.
|
||||
*/
|
||||
public removeAll(predicate: NamedNode | string): this {
|
||||
this.removeQuads(this.store.getQuads(this.id, getNamedNode(predicate), null, null));
|
||||
this.removeQuads(this.store.getQuads(this.id, toNamedNode(predicate), null, null));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -166,7 +166,7 @@ export class RepresentationMetadata {
|
||||
* @returns An array with all matches.
|
||||
*/
|
||||
public getAll(predicate: NamedNode | string): Term[] {
|
||||
return this.store.getQuads(this.id, getNamedNode(predicate), null, null)
|
||||
return this.store.getQuads(this.id, toNamedNode(predicate), null, null)
|
||||
.map((quad): Term => quad.object);
|
||||
}
|
||||
|
||||
@ -209,10 +209,10 @@ export class RepresentationMetadata {
|
||||
* Shorthand for the CONTENT_TYPE predicate.
|
||||
*/
|
||||
public get contentType(): string | undefined {
|
||||
return this.get(getNamedNode('contentType'))?.value;
|
||||
return this.get(toNamedNode('contentType'))?.value;
|
||||
}
|
||||
|
||||
public set contentType(input) {
|
||||
this.set(getNamedNode('contentType'), input);
|
||||
this.set(toNamedNode('contentType'), input);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTy
|
||||
import type { InteractionController } from '../util/InteractionController';
|
||||
import type { MetadataController } from '../util/MetadataController';
|
||||
import { CONTENT_TYPE, DCTERMS, HTTP, POSIX, RDF, XSD } from '../util/UriConstants';
|
||||
import { getTypedLiteral } from '../util/UriUtil';
|
||||
import { toTypedLiteral } from '../util/UriUtil';
|
||||
import { ensureTrailingSlash } from '../util/Util';
|
||||
import type { ExtensionBasedMapper } from './ExtensionBasedMapper';
|
||||
import type { ResourceStore } from './ResourceStore';
|
||||
@ -225,8 +225,8 @@ export class FileResourceStore implements ResourceStore {
|
||||
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
|
||||
}
|
||||
const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path)).addQuads(rawMetadata)
|
||||
.set(DCTERMS.modified, getTypedLiteral(stats.mtime.toISOString(), XSD.dateTime))
|
||||
.set(POSIX.size, getTypedLiteral(stats.size, XSD.integer));
|
||||
.set(DCTERMS.modified, toTypedLiteral(stats.mtime.toISOString(), XSD.dateTime))
|
||||
.set(POSIX.size, toTypedLiteral(stats.size, XSD.integer));
|
||||
metadata.contentType = contentType;
|
||||
return { metadata, data: readStream, binary: true };
|
||||
}
|
||||
@ -259,7 +259,7 @@ export class FileResourceStore implements ResourceStore {
|
||||
}
|
||||
|
||||
const metadata = new RepresentationMetadata(containerURI).addQuads(rawMetadata)
|
||||
.set(DCTERMS.modified, getTypedLiteral(stats.mtime.toISOString(), XSD.dateTime))
|
||||
.set(DCTERMS.modified, toTypedLiteral(stats.mtime.toISOString(), XSD.dateTime))
|
||||
.set(CONTENT_TYPE, INTERNAL_QUADS);
|
||||
|
||||
return {
|
||||
|
@ -7,7 +7,7 @@ import streamifyArray from 'streamify-array';
|
||||
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
|
||||
import { TEXT_TURTLE } from './ContentTypes';
|
||||
import { DCTERMS, LDP, POSIX, RDF, XSD } from './UriConstants';
|
||||
import { getNamedNode, getTypedLiteral } from './UriUtil';
|
||||
import { toNamedNode, toTypedLiteral } from './UriUtil';
|
||||
import { pipeStreamsAndErrors } from './Util';
|
||||
|
||||
export class MetadataController {
|
||||
@ -21,13 +21,13 @@ export class MetadataController {
|
||||
public generateResourceQuads(uri: string, stats: Stats): Quad[] {
|
||||
const metadata = new RepresentationMetadata(uri);
|
||||
if (stats.isDirectory()) {
|
||||
metadata.add(RDF.type, getNamedNode(LDP.Container));
|
||||
metadata.add(RDF.type, getNamedNode(LDP.BasicContainer));
|
||||
metadata.add(RDF.type, toNamedNode(LDP.Container));
|
||||
metadata.add(RDF.type, toNamedNode(LDP.BasicContainer));
|
||||
}
|
||||
metadata.add(RDF.type, getNamedNode(LDP.Resource));
|
||||
metadata.add(POSIX.size, getTypedLiteral(stats.size, XSD.integer));
|
||||
metadata.add(DCTERMS.modified, getTypedLiteral(stats.mtime.toISOString(), XSD.dateTime));
|
||||
metadata.add(POSIX.mtime, getTypedLiteral(Math.floor(stats.mtime.getTime() / 1000), XSD.integer));
|
||||
metadata.add(RDF.type, toNamedNode(LDP.Resource));
|
||||
metadata.add(POSIX.size, toTypedLiteral(stats.size, XSD.integer));
|
||||
metadata.add(DCTERMS.modified, toTypedLiteral(stats.mtime.toISOString(), XSD.dateTime));
|
||||
metadata.add(POSIX.mtime, toTypedLiteral(Math.floor(stats.mtime.getTime() / 1000), XSD.integer));
|
||||
|
||||
return metadata.quads();
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
const createSuffixFn = (prefix: string): ((suf: string) => string) => (suffix: string): string => `${prefix}${suffix}`;
|
||||
const createNamespace = (prefix: string): ((suf: string) => string) => (suffix: string): string => `${prefix}${suffix}`;
|
||||
|
||||
const ACL_PREFIX = createSuffixFn('http://www.w3.org/ns/auth/acl#');
|
||||
const ACL_PREFIX = createNamespace('http://www.w3.org/ns/auth/acl#');
|
||||
export const ACL = {
|
||||
accessTo: ACL_PREFIX('accessTo'),
|
||||
agent: ACL_PREFIX('agent'),
|
||||
@ -15,23 +15,23 @@ export const ACL = {
|
||||
Control: ACL_PREFIX('Control'),
|
||||
};
|
||||
|
||||
const DCTERMS_PREFIX = createSuffixFn('http://purl.org/dc/terms/');
|
||||
const DCTERMS_PREFIX = createNamespace('http://purl.org/dc/terms/');
|
||||
export const DCTERMS = {
|
||||
modified: DCTERMS_PREFIX('modified'),
|
||||
};
|
||||
|
||||
const FOAF_PREFIX = createSuffixFn('http://xmlns.com/foaf/0.1/');
|
||||
const FOAF_PREFIX = createNamespace('http://xmlns.com/foaf/0.1/');
|
||||
export const FOAF = {
|
||||
Agent: FOAF_PREFIX('Agent'),
|
||||
AuthenticatedAgent: FOAF_PREFIX('AuthenticatedAgent'),
|
||||
};
|
||||
|
||||
const HTTP_PREFIX = createSuffixFn('urn:solid:http:');
|
||||
const HTTP_PREFIX = createNamespace('urn:solid:http:');
|
||||
export const HTTP = {
|
||||
slug: HTTP_PREFIX('slug'),
|
||||
};
|
||||
|
||||
const LDP_PREFIX = createSuffixFn('http://www.w3.org/ns/ldp#');
|
||||
const LDP_PREFIX = createNamespace('http://www.w3.org/ns/ldp#');
|
||||
export const LDP = {
|
||||
contains: LDP_PREFIX('contains'),
|
||||
|
||||
@ -40,23 +40,23 @@ export const LDP = {
|
||||
Resource: LDP_PREFIX('Resource'),
|
||||
};
|
||||
|
||||
const MA_PREFIX = createSuffixFn('http://www.w3.org/ns/ma-ont#');
|
||||
const MA_PREFIX = createNamespace('http://www.w3.org/ns/ma-ont#');
|
||||
export const MA = {
|
||||
format: MA_PREFIX('format'),
|
||||
};
|
||||
|
||||
const POSIX_PREFIX = createSuffixFn('http://www.w3.org/ns/posix/stat#');
|
||||
const POSIX_PREFIX = createNamespace('http://www.w3.org/ns/posix/stat#');
|
||||
export const POSIX = {
|
||||
mtime: POSIX_PREFIX('mtime'),
|
||||
size: POSIX_PREFIX('size'),
|
||||
};
|
||||
|
||||
const RDF_PREFIX = createSuffixFn('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
|
||||
const RDF_PREFIX = createNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
|
||||
export const RDF = {
|
||||
type: RDF_PREFIX('type'),
|
||||
};
|
||||
|
||||
const XSD_PREFIX = createSuffixFn('http://www.w3.org/2001/XMLSchema#');
|
||||
const XSD_PREFIX = createNamespace('http://www.w3.org/2001/XMLSchema#');
|
||||
export const XSD = {
|
||||
dateTime: XSD_PREFIX('dateTime'),
|
||||
integer: XSD_PREFIX('integer'),
|
||||
|
@ -22,7 +22,7 @@ export const isTerm = (input?: any): input is Term => input?.termType;
|
||||
* so only use this for internal constants!
|
||||
* @param name - Predicate to potentially transform.
|
||||
*/
|
||||
export const getNamedNode = (name: NamedNode | string): NamedNode => {
|
||||
export const toNamedNode = (name: NamedNode | string): NamedNode => {
|
||||
if (typeof name === 'string') {
|
||||
if (shorthands[name]) {
|
||||
return shorthands[name];
|
||||
@ -39,7 +39,7 @@ export const getNamedNode = (name: NamedNode | string): NamedNode => {
|
||||
* Converts an object to a literal when needed.
|
||||
* @param object - Object to potentially transform.
|
||||
*/
|
||||
export const getObjectTerm = (object: NamedNode | Literal | string): NamedNode | Literal =>
|
||||
export const toObjectTerm = (object: NamedNode | Literal | string): NamedNode | Literal =>
|
||||
typeof object === 'string' ? DataFactory.literal(object) : object;
|
||||
|
||||
/**
|
||||
@ -47,5 +47,5 @@ export const getObjectTerm = (object: NamedNode | Literal | string): NamedNode |
|
||||
* @param object - Object value.
|
||||
* @param dataType - Object data type (as string).
|
||||
*/
|
||||
export const getTypedLiteral = (object: string | number, dataType: string): Literal =>
|
||||
DataFactory.literal(object, getNamedNode(dataType));
|
||||
export const toTypedLiteral = (object: string | number, dataType: string): Literal =>
|
||||
DataFactory.literal(object, toNamedNode(dataType));
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { literal, namedNode } from '@rdfjs/data-model';
|
||||
import { CONTENT_TYPE, XSD } from '../../../src/util/UriConstants';
|
||||
import { getNamedNode, getObjectTerm, getTypedLiteral, isTerm } from '../../../src/util/UriUtil';
|
||||
import { toNamedNode, toObjectTerm, toTypedLiteral, isTerm } from '../../../src/util/UriUtil';
|
||||
|
||||
describe('An UriUtil', (): void => {
|
||||
describe('isTerm function', (): void => {
|
||||
@ -16,21 +16,21 @@ describe('An UriUtil', (): void => {
|
||||
describe('getNamedNode function', (): void => {
|
||||
it('returns the input if it was a named node.', async(): Promise<void> => {
|
||||
const term = namedNode('name');
|
||||
expect(getNamedNode(term)).toBe(term);
|
||||
expect(toNamedNode(term)).toBe(term);
|
||||
});
|
||||
|
||||
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||
expect(getNamedNode('name')).toEqualRdfTerm(namedNode('name'));
|
||||
expect(toNamedNode('name')).toEqualRdfTerm(namedNode('name'));
|
||||
});
|
||||
|
||||
it('caches generated named nodes.', async(): Promise<void> => {
|
||||
const result = getNamedNode('name');
|
||||
const result = toNamedNode('name');
|
||||
expect(result).toEqualRdfTerm(namedNode('name'));
|
||||
expect(getNamedNode('name')).toBe(result);
|
||||
expect(toNamedNode('name')).toBe(result);
|
||||
});
|
||||
|
||||
it('supports URI shorthands.', async(): Promise<void> => {
|
||||
expect(getNamedNode('contentType')).toEqualRdfTerm(namedNode(CONTENT_TYPE));
|
||||
expect(toNamedNode('contentType')).toEqualRdfTerm(namedNode(CONTENT_TYPE));
|
||||
});
|
||||
});
|
||||
|
||||
@ -38,19 +38,19 @@ describe('An UriUtil', (): void => {
|
||||
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||
const nn = namedNode('name');
|
||||
const lit = literal('lit');
|
||||
expect(getObjectTerm(nn)).toBe(nn);
|
||||
expect(getObjectTerm(lit)).toBe(lit);
|
||||
expect(toObjectTerm(nn)).toBe(nn);
|
||||
expect(toObjectTerm(lit)).toBe(lit);
|
||||
});
|
||||
|
||||
it('returns a literal when a string is used.', async(): Promise<void> => {
|
||||
expect(getObjectTerm('lit')).toEqualRdfTerm(literal('lit'));
|
||||
expect(toObjectTerm('lit')).toEqualRdfTerm(literal('lit'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTypedLiteral function', (): void => {
|
||||
it('converts the input to a valid literal with the given type.', async(): Promise<void> => {
|
||||
const expected = literal('5', namedNode(XSD.integer));
|
||||
expect(getTypedLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
|
||||
expect(toTypedLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user