Skip to content

Commit

Permalink
refactor: Rename UriUtil into TermUtil.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 2, 2021
1 parent ae06e99 commit 2e18855
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 1 addition & 1 deletion src/ldp/representation/RepresentationMetadata.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/storage/accessors/FileDataAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/util/QuadUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 18 additions & 19 deletions src/util/UriUtil.ts → src/util/TermUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,32 @@ const shorthands: Record<string, NamedNode> = {
};

// Caches named node conversions
const termMap: Record<string, NamedNode> = {};

/**
* @param input - Checks if this is a {@link Term}.
*/
export const isTerm = (input?: any): input is Term => input?.termType;
const cachedNamedNodes: Record<string, NamedNode> = {
...shorthands,
};

/**
* 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.
Expand Down Expand Up @@ -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);
2 changes: 1 addition & 1 deletion test/unit/storage/accessors/FileDataAccessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 2e18855

Please sign in to comment.