refactor: Rename UriUtil into TermUtil.

This commit is contained in:
Ruben Verborgh
2021-01-02 23:11:57 +01:00
parent ae06e99067
commit 2e188551f7
7 changed files with 27 additions and 28 deletions

View File

@@ -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.

View File

@@ -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);