refactor: Make createNamespace return regular objects.

This commit is contained in:
Ruben Verborgh 2021-01-06 11:54:12 +01:00
parent 87752ddf20
commit 8343dad7f1
3 changed files with 11 additions and 30 deletions

View File

@ -2,11 +2,10 @@
import { namedNode } from '@rdfjs/data-model';
import type { NamedNode } from 'rdf-js';
type PrefixResolver<T> = (localName?: string) => T;
type RecordOf<TKey extends any[], TValue> = Record<TKey[number], TValue>;
export type Namespace<TKey extends any[], TValue> =
PrefixResolver<TValue> & RecordOf<TKey, TValue>;
{ namespace: TValue } & RecordOf<TKey, TValue>;
/**
* Creates a function that expands local names from the given base URI,
@ -17,20 +16,14 @@ export function createNamespace<TKey extends string, TValue>(
toValue: (expanded: string) => TValue,
...localNames: TKey[]):
Namespace<typeof localNames, TValue> {
// Create a function that expands local names
const expanded = {} as Record<string, TValue>;
const namespace = ((localName = ''): TValue => {
if (!(localName in expanded)) {
expanded[localName] = toValue(`${baseUri}${localName}`);
}
return expanded[localName];
}) as Namespace<typeof localNames, TValue>;
const expanded: Namespace<typeof localNames, TValue> = {} as any;
// Expose the main namespace
expanded.namespace = toValue(baseUri);
// Expose the listed local names as properties
for (const localName of localNames) {
(namespace as RecordOf<typeof localNames, TValue>)[localName] = namespace(localName);
(expanded as RecordOf<TKey[], TValue>)[localName] = toValue(`${baseUri}${localName}`);
}
return namespace;
return expanded;
}
/**

View File

@ -76,7 +76,7 @@ describe('A QuadToRdfConverter', (): void => {
});
it('converts quads with prefixes to turtle.', async(): Promise<void> => {
metadata.addQuad(DC.terms(), PREFERRED_PREFIX_TERM, 'dc');
metadata.addQuad(DC.terms.namespace, PREFERRED_PREFIX_TERM, 'dc');
metadata.addQuad('http://test.com/', PREFERRED_PREFIX_TERM, 'test');
const representation = {
data: streamifyArray([ triple(

View File

@ -3,20 +3,12 @@ import { LDP } from '../../../src/util/Vocabularies';
describe('Vocabularies', (): void => {
describe('LDP', (): void => {
it('can return its own URI.', (): void => {
expect(LDP()).toBe('http://www.w3.org/ns/ldp#');
it('contains its own URI.', (): void => {
expect(LDP.namespace).toBe('http://www.w3.org/ns/ldp#');
});
it('can create new properties.', (): void => {
expect(LDP('new')).toBe('http://www.w3.org/ns/ldp#new');
});
it('can create new properties as terms.', (): void => {
expect(LDP.terms('new')).toEqual(namedNode('http://www.w3.org/ns/ldp#new'));
});
it('caches new properties as terms.', (): void => {
expect(LDP.terms('new')).toBe(LDP.terms('new'));
it('contains its own URI as a term.', (): void => {
expect(LDP.terms.namespace).toEqual(namedNode('http://www.w3.org/ns/ldp#'));
});
it('exposes ldp:contains.', (): void => {
@ -26,9 +18,5 @@ describe('Vocabularies', (): void => {
it('exposes ldp:contains as a term.', (): void => {
expect(LDP.terms.contains).toEqual(namedNode('http://www.w3.org/ns/ldp#contains'));
});
it('caches ldp:contains as a term.', (): void => {
expect(LDP.terms.contains).toBe(LDP.terms.contains);
});
});
});