mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Rename UriUtil into TermUtil.
This commit is contained in:
parent
ae06e99067
commit
2e188551f7
@ -199,6 +199,6 @@ export * from './util/QuadUtil';
|
||||
export * from './util/RecordObject';
|
||||
export * from './util/SequenceHandler';
|
||||
export * from './util/StreamUtil';
|
||||
export * from './util/UriUtil';
|
||||
export * from './util/TermUtil';
|
||||
export * from './util/Vocabularies';
|
||||
export * from './util/WaterfallHandler';
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { DataFactory, Store } from 'n3';
|
||||
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js';
|
||||
import { getLoggerFor } from '../../logging/LogUtil';
|
||||
import { toObjectTerm, toCachedNamedNode, isTerm } from '../../util/UriUtil';
|
||||
import { toObjectTerm, toCachedNamedNode, isTerm } from '../../util/TermUtil';
|
||||
import { CONTENT_TYPE_TERM } from '../../util/Vocabularies';
|
||||
import type { ResourceIdentifier } from './ResourceIdentifier';
|
||||
import { isResourceIdentifier } from './ResourceIdentifier';
|
||||
|
||||
@ -16,7 +16,7 @@ import type { Guarded } from '../../util/GuardedStream';
|
||||
import { isContainerIdentifier } from '../../util/PathUtil';
|
||||
import { parseQuads, pushQuad, serializeQuads } from '../../util/QuadUtil';
|
||||
import { generateContainmentQuads, generateResourceQuads } from '../../util/ResourceUtil';
|
||||
import { toLiteral } from '../../util/UriUtil';
|
||||
import { toLiteral } from '../../util/TermUtil';
|
||||
import { CONTENT_TYPE, DC, LDP, POSIX, RDF, XSD } from '../../util/Vocabularies';
|
||||
import type { FileIdentifierMapper, ResourceLink } from '../mapping/FileIdentifierMapper';
|
||||
import type { DataAccessor } from './DataAccessor';
|
||||
|
||||
@ -5,7 +5,7 @@ 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';
|
||||
import { toSubjectTerm, toPredicateTerm, toObjectTerm } from './TermUtil';
|
||||
|
||||
/**
|
||||
* Generates a quad with the given subject/predicate/object and pushes it to the given array.
|
||||
|
||||
@ -10,33 +10,32 @@ const shorthands: Record<string, NamedNode> = {
|
||||
};
|
||||
|
||||
// Caches named node conversions
|
||||
const termMap: Record<string, NamedNode> = {};
|
||||
const cachedNamedNodes: Record<string, NamedNode> = {
|
||||
...shorthands,
|
||||
};
|
||||
|
||||
/**
|
||||
* @param input - Checks if this is a {@link Term}.
|
||||
*/
|
||||
export const isTerm = (input?: any): input is Term => input?.termType;
|
||||
|
||||
/**
|
||||
* Converts the incoming name to a named node if needed.
|
||||
* In case of string, first checks if it is a shorthand, if not a new named node gets made.
|
||||
* The generated terms get cached to prevent the amount of named nodes that get created,
|
||||
* Converts the incoming name (URI or shorthand) to a named node.
|
||||
* The generated terms get cached to reduce the number of created nodes,
|
||||
* so only use this for internal constants!
|
||||
* @param name - Predicate to potentially transform.
|
||||
*/
|
||||
export const toCachedNamedNode = (name: NamedNode | string): NamedNode => {
|
||||
if (typeof name === 'string') {
|
||||
if (shorthands[name]) {
|
||||
return shorthands[name];
|
||||
}
|
||||
if (!termMap[name]) {
|
||||
termMap[name] = namedNode(name);
|
||||
}
|
||||
return termMap[name];
|
||||
if (typeof name !== 'string') {
|
||||
return name;
|
||||
}
|
||||
return name;
|
||||
if (!(name in cachedNamedNodes)) {
|
||||
cachedNamedNodes[name] = namedNode(name);
|
||||
}
|
||||
return cachedNamedNodes[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* @param input - Checks if this is a {@link Term}.
|
||||
*/
|
||||
export const isTerm = (input?: any): input is Term =>
|
||||
input && typeof input.termType === 'string';
|
||||
|
||||
/**
|
||||
* Converts a subject to a named node when needed.
|
||||
* @param subject - Subject to potentially transform.
|
||||
@ -64,4 +63,4 @@ export const toObjectTerm = <T extends Term>(object: T | string, preferLiteral =
|
||||
* @param dataType - Object data type (as string).
|
||||
*/
|
||||
export const toLiteral = (object: string | number, dataType: NamedNode): Literal =>
|
||||
DataFactory.literal(object, toCachedNamedNode(dataType));
|
||||
literal(object, dataType);
|
||||
@ -12,7 +12,7 @@ import type { SystemError } from '../../../../src/util/errors/SystemError';
|
||||
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
|
||||
import type { Guarded } from '../../../../src/util/GuardedStream';
|
||||
import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil';
|
||||
import { toLiteral } from '../../../../src/util/UriUtil';
|
||||
import { toLiteral } from '../../../../src/util/TermUtil';
|
||||
import { CONTENT_TYPE, DC, LDP, POSIX, RDF, XSD } from '../../../../src/util/Vocabularies';
|
||||
import { mockFs } from '../../../util/Util';
|
||||
|
||||
|
||||
@ -7,10 +7,10 @@ import {
|
||||
toObjectTerm,
|
||||
toLiteral,
|
||||
isTerm,
|
||||
} from '../../../src/util/UriUtil';
|
||||
import { CONTENT_TYPE, XSD } from '../../../src/util/Vocabularies';
|
||||
} from '../../../src/util/TermUtil';
|
||||
import { CONTENT_TYPE_TERM, XSD } from '../../../src/util/Vocabularies';
|
||||
|
||||
describe('An UriUtil', (): void => {
|
||||
describe('TermUtil', (): void => {
|
||||
describe('isTerm function', (): void => {
|
||||
it('checks if any input is a Term.', async(): Promise<void> => {
|
||||
expect(isTerm(namedNode('name'))).toBeTruthy();
|
||||
@ -38,7 +38,7 @@ describe('An UriUtil', (): void => {
|
||||
});
|
||||
|
||||
it('supports URI shorthands.', async(): Promise<void> => {
|
||||
expect(toCachedNamedNode('contentType')).toEqualRdfTerm(namedNode(CONTENT_TYPE));
|
||||
expect(toCachedNamedNode('contentType')).toEqualRdfTerm(CONTENT_TYPE_TERM);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user