IF Commons GitHub

rdf-term

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0001-rdf FR-001

RDF terms as typed values (commons 0012-ontology FR-001, FR-002): an IRI branded once checked, a namespace that pairs a prefix with its IRI, a named node branded with the classes its term is an individual of, blank nodes, literals typed by their language or their datatype, and triples and quads of them. A vocabulary is a typed value made from a namespace and the local names of its terms, so a term it does not list does not compile. Generalized from erga's ontology DSL (erga 248-ontology-in-typescript) and stolos' RDF export.

import { en, namespace, vocabulary } from "./rdf-term.ts";
const ex = vocabulary(namespace("ex", "https://example.org/terms#"), { Widget: "rdfs:Class", size: "rdf:Property" });
ex.Widget.iri; // "https://example.org/terms#Widget"
en("a widget"); // { kind: "lang", value: "a widget", language: "en" }

Source: commons/rdf/src/rdf-term.ts

15 of 44 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

Iritypeno test imports it
type Iri = Brand<string, "rdf/Iri">;

An absolute IRI, checked by parseIri or made from a namespace.

IriErrortypeno test imports it
type IriError =
  | { readonly kind: "not-absolute"; readonly text: string }
  | { readonly kind: "forbidden-character"; readonly text: string; readonly at: number };

Why a text is not an IRI: it has no scheme, or holds a character an IRI may not hold in N-Triples.

parseIrifunction
function parseIri(text: string): Result<Iri, IriError>

The text as an IRI, or why it is not one.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

formatIriErrorfunction
function formatIriError(e: IriError): string

The sentence an IRI error is told with.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

RdfFaultclass
class RdfFault extends Error

A fault of the code: a constant namespace or term whose IRI does not parse. It is thrown where the constant is made.

MemberTypeWhat it is
namestring
messagestring
stack?string
cause?unknown

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Classestypeno test imports it
type Classes<C extends string> = { readonly [K in C]: true };

The classes a named node is an individual of, as the compiler sees them: one key for each class's name.

Namedtype
type Named<C extends string = never> = { readonly kind: "named"; readonly iri: Iri; readonly "@ifc/rdf-classes": Classes<C> };

A named node: an IRI, branded with the classes its term is an individual of (by their CURIEs, "pm:PackageManager"). A Named<"a" | "b"> is an individual of both, so it is accepted where either is asked for; a Named<"a"> is not accepted where "b" is, and a Named of no known class (Named, which is Named<never>) is not accepted where any one class is asked for: every named node is a Named, so it is the type of a predicate or of any node at all.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

NamedOftypeno test imports it
type NamedOf<C extends string> = C extends string ? Named<C> : never;

A named node of any one of the classes C (NamedOf<"a" | "b"> is a Named<"a"> or a Named<"b">).

namedfunction
function named<C extends string = never>(iri: Iri): Named<C>

The named node of an IRI, as an individual of the classes C; the brand is for the compiler alone.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Namespacetypeno test imports it
type Namespace<P extends string = string> = { readonly prefix: P; readonly iri: Iri };

A namespace: a prefix and the IRI its terms' local names are appended to.

namespacefunction
function namespace<const P extends string>(prefix: P, iri: string): Namespace<P>

A namespace; one whose prefix or IRI is not well formed is a fault of the code, thrown as an RdfFault.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

iriInfunctionno test imports it
function iriIn(ns: Namespace<string>, local: string): Iri

The IRI of a local name in a namespace; one that does not parse is a fault of the code, thrown as an RdfFault.

termfunction
function term<C extends string = never>(ns: Namespace<string>, local: string): Named<C>

The named node of a local name in a namespace, as an individual of the classes C, which the compiler takes from where it is used. It is how a term no module here states is named (an individual of another ontology); a term this code states comes from its schema.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

ontologyIrifunctionno test imports it
function ontologyIri(ns: Namespace<string>): Iri

The IRI of the ontology a namespace's terms belong to: the namespace's IRI without its last # or /.

VocabularyTermtypeno test imports it
type VocabularyTerm<C extends string, Curie extends string> = Named<C> & { readonly curie: Curie };

A term of a vocabulary: a named node of its class, with its CURIE.

Vocabularytypeno test imports it
type Vocabulary<P extends string, T extends Readonly<Record<string, string>>> = {
  readonly [K in keyof T & string]: VocabularyTerm<T[K], `${P}:${K}`>;
};

A typed vocabulary: each local name of T gives a term of the class T names for it.

vocabularyfunction
function vocabulary<const P extends string, const T extends Readonly<Record<string, string>>>(ns: Namespace<P>, terms: T): Vocabulary<P, T>

The vocabulary of a namespace: its terms by local name, each of the class given ({ label: "rdf:Property" }).

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

LanguageTagtypeno test imports it
type LanguageTag = Brand<string, "rdf/LanguageTag">;

A language tag, checked against BCP 47's shape (letters, then subtags of letters and digits, joined by -).

languageTagfunctionno test imports it
function languageTag(text: string): LanguageTag | undefined

The text as a language tag, in lower case, or nothing where it is not one.

LangStringtypeno test imports it
type LangString = { readonly kind: "lang"; readonly value: string; readonly language: LanguageTag };

A string in a language: "install"@en.

TypedLiteraltypeno test imports it
type TypedLiteral = { readonly kind: "typed"; readonly value: string; readonly datatype: Iri };

A literal with its datatype (a plain string is an xsd:string).

Literaltypeno test imports it
type Literal = LangString | TypedLiteral;

A literal: a string in a language, or a lexical form with its datatype.

Blanktypeno test imports it
type Blank = { readonly kind: "blank"; readonly label: string };

A blank node, by its label.

DefaultGraphtypeno test imports it
type DefaultGraph = { readonly kind: "default" };

A node of the default graph's name.

Termtypeno test imports it
type Term = Named | Blank | Literal;

Any RDF term.

Subjecttypeno test imports it
type Subject = Named | Blank;

What a triple may be about.

Tripletype
type Triple = { readonly subject: Subject; readonly predicate: Named; readonly object: Term };

A triple.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Quadtypeno test imports it
type Quad = Triple & { readonly graph: Named | DefaultGraph };

A triple in a named graph, or in the default graph.

TermErrortypeno test imports it
type TermError = { readonly kind: "language"; readonly tag: string } | { readonly kind: "blank-label"; readonly label: string };

Why a literal or a blank node could not be made.

formatTermErrorfunctionno test imports it
function formatTermError(e: TermError): string

The sentence a term error is told with.

langfunction
function lang(value: string, tag: string): Result<LangString, TermError>

A string in the language of a tag, or why the tag is not one.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

enfunction
function en(value: string): LangString

A string in English: en("install").

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

XSD_STRINGconstno test imports it
const XSD_STRING: Iri

The IRI of xsd:string.

XSD_INTEGERconstno test imports it
const XSD_INTEGER: Iri

The IRI of xsd:integer.

XSD_BOOLEANconstno test imports it
const XSD_BOOLEAN: Iri

The IRI of xsd:boolean.

RDF_TYPEconstno test imports it
const RDF_TYPE: Iri

The IRI of rdf:type.

typedfunction
function typed(value: string, datatype: Named<never>): TypedLiteral

A literal of a datatype: typed("2026-11-16", xsd.date).

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

plainfunction
function plain(value: string): TypedLiteral

A plain string, an xsd:string.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

blankfunction
function blank(label: string): Result<Blank, TermError>

A blank node, or why its label is not one.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

defaultGraphconst
const defaultGraph: DefaultGraph

The default graph.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Valuetypeno test imports it
type Value = string | number | boolean | Literal | Named;

One value of a field: a plain string, an integer, a boolean, a literal or a named node.

termOffunctionno test imports it
function termOf(v: Value): Term

The term of a value: a string is an xsd:string, a number an xsd:integer (in its canonical form), a boolean an xsd:boolean.

quotedfunctionno test imports it
function quoted(s: string): string

A string's characters escaped as N-Triples and Turtle write them, in quotes: \b \t \n \f \r \" \\ and \uXXXX for the other controls.

nTriplesTermfunctionno test imports it
function nTriplesTerm(t: Term): string

A term as N-Triples writes it: <iri>, _:label, or a quoted literal with its language or its datatype (none for xsd:string).

Titles, headings, exports, tables, columns and requirements. Esc closes.