From 2e188551f7d53191e8d591ffb6da58fefbe29287 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Sat, 2 Jan 2021 23:11:57 +0100 Subject: [PATCH] refactor: Rename UriUtil into TermUtil. --- src/index.ts | 2 +- .../representation/RepresentationMetadata.ts | 2 +- src/storage/accessors/FileDataAccessor.ts | 2 +- src/util/QuadUtil.ts | 2 +- src/util/{UriUtil.ts => TermUtil.ts} | 37 +++++++++---------- .../accessors/FileDataAccessor.test.ts | 2 +- .../{UriUtil.test.ts => TermUtil.test.ts} | 8 ++-- 7 files changed, 27 insertions(+), 28 deletions(-) rename src/util/{UriUtil.ts => TermUtil.ts} (71%) rename test/unit/util/{UriUtil.test.ts => TermUtil.test.ts} (92%) diff --git a/src/index.ts b/src/index.ts index 6ad5ccad5..c702d3b02 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/ldp/representation/RepresentationMetadata.ts b/src/ldp/representation/RepresentationMetadata.ts index 879790586..f71cf167b 100644 --- a/src/ldp/representation/RepresentationMetadata.ts +++ b/src/ldp/representation/RepresentationMetadata.ts @@ -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'; diff --git a/src/storage/accessors/FileDataAccessor.ts b/src/storage/accessors/FileDataAccessor.ts index 0bd6786da..ca832f986 100644 --- a/src/storage/accessors/FileDataAccessor.ts +++ b/src/storage/accessors/FileDataAccessor.ts @@ -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'; diff --git a/src/util/QuadUtil.ts b/src/util/QuadUtil.ts index d8444c51a..1c6dd4082 100644 --- a/src/util/QuadUtil.ts +++ b/src/util/QuadUtil.ts @@ -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. diff --git a/src/util/UriUtil.ts b/src/util/TermUtil.ts similarity index 71% rename from src/util/UriUtil.ts rename to src/util/TermUtil.ts index 6a9c4391b..940e2bf00 100644 --- a/src/util/UriUtil.ts +++ b/src/util/TermUtil.ts @@ -10,33 +10,32 @@ const shorthands: Record = { }; // Caches named node conversions -const termMap: Record = {}; +const cachedNamedNodes: Record = { + ...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 = (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); diff --git a/test/unit/storage/accessors/FileDataAccessor.test.ts b/test/unit/storage/accessors/FileDataAccessor.test.ts index 74ebb09c3..a317492ea 100644 --- a/test/unit/storage/accessors/FileDataAccessor.test.ts +++ b/test/unit/storage/accessors/FileDataAccessor.test.ts @@ -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'; diff --git a/test/unit/util/UriUtil.test.ts b/test/unit/util/TermUtil.test.ts similarity index 92% rename from test/unit/util/UriUtil.test.ts rename to test/unit/util/TermUtil.test.ts index 4290963e8..5c300d8ac 100644 --- a/test/unit/util/UriUtil.test.ts +++ b/test/unit/util/TermUtil.test.ts @@ -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 => { expect(isTerm(namedNode('name'))).toBeTruthy(); @@ -38,7 +38,7 @@ describe('An UriUtil', (): void => { }); it('supports URI shorthands.', async(): Promise => { - expect(toCachedNamedNode('contentType')).toEqualRdfTerm(namedNode(CONTENT_TYPE)); + expect(toCachedNamedNode('contentType')).toEqualRdfTerm(CONTENT_TYPE_TERM); }); });