mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Support writer prefixes.
Closes https://github.com/solid/community-server/issues/470
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { DataFactory } from 'n3';
|
||||
import type { Literal, NamedNode, Term } from 'rdf-js';
|
||||
import type { NamedNode, Literal, Term } from 'rdf-js';
|
||||
import { CONTENT_TYPE_TERM } from './Vocabularies';
|
||||
|
||||
const { namedNode, literal } = DataFactory;
|
||||
@@ -41,7 +41,10 @@ export function isTerm(input?: any): input is Term {
|
||||
* Converts a subject to a named node when needed.
|
||||
* @param subject - Subject to potentially transform.
|
||||
*/
|
||||
export function toSubjectTerm(subject: NamedNode | string): NamedNode {
|
||||
export function toSubjectTerm(subject: string): NamedNode;
|
||||
export function toSubjectTerm<T extends Term>(subject: T): T;
|
||||
export function toSubjectTerm<T extends Term>(subject: T | string): T | NamedNode;
|
||||
export function toSubjectTerm(subject: Term | string): Term {
|
||||
return typeof subject === 'string' ? namedNode(subject) : subject;
|
||||
}
|
||||
|
||||
@@ -52,7 +55,10 @@ export const toPredicateTerm = toSubjectTerm;
|
||||
* @param object - Object to potentially transform.
|
||||
* @param preferLiteral - Whether strings are converted to literals or named nodes.
|
||||
*/
|
||||
export function toObjectTerm<T extends Term>(object: T | string, preferLiteral = false): T {
|
||||
export function toObjectTerm(object: string, preferLiteral?: boolean): NamedNode;
|
||||
export function toObjectTerm<T extends Term>(object: T, preferLiteral?: boolean): T;
|
||||
export function toObjectTerm<T extends Term>(object: T | string, preferLiteral?: boolean): T | NamedNode;
|
||||
export function toObjectTerm(object: Term | string, preferLiteral = false): Term {
|
||||
if (typeof object === 'string') {
|
||||
return (preferLiteral ? literal(object) : namedNode(object)) as any;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { namedNode } from '@rdfjs/data-model';
|
||||
import type { NamedNode } from 'rdf-js';
|
||||
|
||||
type PrefixResolver<T> = (localName: string) => T;
|
||||
type PrefixResolver<T> = (localName?: string) => T;
|
||||
type RecordOf<TKey extends any[], TValue> = Record<TKey[number], TValue>;
|
||||
|
||||
export type Namespace<TKey extends any[], TValue> =
|
||||
@@ -19,7 +19,7 @@ export function createNamespace<TKey extends string, TValue>(
|
||||
Namespace<typeof localNames, TValue> {
|
||||
// Create a function that expands local names
|
||||
const expanded = {} as Record<string, TValue>;
|
||||
const namespace = ((localName: string): TValue => {
|
||||
const namespace = ((localName = ''): TValue => {
|
||||
if (!(localName in expanded)) {
|
||||
expanded[localName] = toValue(`${baseUri}${localName}`);
|
||||
}
|
||||
@@ -114,11 +114,17 @@ export const RDF = createUriAndTermNamespace('http://www.w3.org/1999/02/22-rdf-s
|
||||
'type',
|
||||
);
|
||||
|
||||
export const VANN = createUriAndTermNamespace('http://purl.org/vocab/vann/',
|
||||
'preferredNamespacePrefix',
|
||||
);
|
||||
|
||||
export const XSD = createUriAndTermNamespace('http://www.w3.org/2001/XMLSchema#',
|
||||
'dateTime',
|
||||
'integer',
|
||||
);
|
||||
|
||||
// Alias for most commonly used URI
|
||||
// Alias for commonly used types
|
||||
export const CONTENT_TYPE = MA.format;
|
||||
export const CONTENT_TYPE_TERM = MA.terms.format;
|
||||
export const PREFERRED_PREFIX = VANN.preferredNamespacePrefix;
|
||||
export const PREFERRED_PREFIX_TERM = VANN.terms.preferredNamespacePrefix;
|
||||
|
||||
Reference in New Issue
Block a user