@grafctory/core - v0.15.0
    Preparing search index...

    Module resource

    RFC 3987 resource identifiers.

    Provides types and functions for validating resource identifiers (IRIs), telling links a browser can open apart from other identifiers, checking nesting relationships and navigating identifier hierarchies, extracting hosts and display labels from identifiers, resolving and rewriting references, creating and inspecting namespace objects, and minting IRIs for local resources through the predefined base namespace. Prefix lexicons compact IRIs to the prefixed names used in SPARQL and Turtle text, and expand those names back to full IRIs. Names written against a lexicon round-trip to the identifiers they abbreviate, and every name a lexicon produces pastes verbatim into query text.

    Type Guards

    import { isIRI } from "@grafctory/core/resource";

    const value = "http://example.org/resource";

    if (isIRI(value)) {
    // value is typed as IRI
    }

    // Variant-specific checks

    isIRI("https://example.org/resource", "browsable"); // true
    isIRI("mailto:user@example.org", "browsable"); // false
    isIRI("http://example.org/resource", "absolute"); // true
    isIRI("/resource", "internal"); // true
    isIRI("../resource", "relative"); // true
    isIRI("http://example.org/资源", "absolute"); // true (Unicode allowed)

    Nesting Checks

    import { isNestedIRI } from "@grafctory/core/resource";

    isNestedIRI("http://example.com/a/", "http://example.com/a/b"); // true
    isNestedIRI("http://example.com/a/", "http://example.com/a/"); // true (self-nesting)
    isNestedIRI("http://example.com/a/", "http://example.com/x/y"); // false

    Reference Operations

    import {
    getIRIBase, getIRIHost, getIRILabel, getIRIParent, internalize, relativize, resolve
    } from "@grafctory/core/resource";

    const iri = "http://example.com/a/b/c";

    // Resolve relative references against base

    resolve(iri, "../d"); // "http://example.com/a/d"
    resolve(iri, "/d"); // "http://example.com/d"

    // Convert absolute to root-relative (internal) path

    internalize(iri, "http://example.com/x/y"); // "/x/y"

    // Convert absolute to relative path

    relativize(iri, "http://example.com/a/d"); // "../d"

    // Extract the base identifier usable for reference resolution

    getIRIBase(iri); // "http://example.com/"
    getIRIBase("/a/b"); // undefined

    // Extract the identifier of the enclosing collection

    getIRIParent(iri); // "http://example.com/a/b/"
    getIRIParent("/a/b/"); // "/a/"
    getIRIParent("a"); // "./"

    // Extract the host for compact display of web addresses

    getIRIHost(iri); // "example.com"
    getIRIHost("mailto:user@example.com"); // undefined

    // Derive a display label from the last segment

    getIRILabel("http://example.com/fineArts"); // "Fine Arts"

    Namespace Objects

    import { base, createNamespace, getNamespaceBase, getNamespaceIRI } from "@grafctory/core/resource";

    // Closed namespace with predefined terms

    const rdfs = createNamespace("http://www.w3.org/2000/01/rdf-schema#", [
    "label",
    "comment"
    ]);

    rdfs[""]; // IRI: "http://www.w3.org/2000/01/rdf-schema#"
    rdfs.label; // IRI: "http://www.w3.org/2000/01/rdf-schema#label"
    rdfs["comment"]; // IRI: "http://www.w3.org/2000/01/rdf-schema#comment"
    rdfs["seeAlso"]; // throws RangeError: unknown term

    // Namespace accessors

    getNamespaceIRI(rdfs); // IRI: "http://www.w3.org/2000/01/rdf-schema#"
    getNamespaceBase(rdfs); // IRI: "http://www.w3.org/"

    // Open namespace for dynamic terms

    const ex = createNamespace("http://example.org/");

    ex[""]; // IRI: "http://example.org/"
    ex["anything"]; // IRI: "http://example.org/anything"

    // Predefined open namespace for local resources

    base[""]; // IRI: "base:/#"
    base.label; // IRI: "base:/#label"

    Compact URIs

    import { compact, createLexicon, createNamespace, expand } from "@grafctory/core/resource";

    const lexicon = createLexicon({
    rdfs: "http://www.w3.org/2000/01/rdf-schema#",
    ex: createNamespace("https://example.org/")
    });

    // Compact IRIs to prefixed names

    compact(lexicon, "http://www.w3.org/2000/01/rdf-schema#label"); // "rdfs:label"
    compact(lexicon, "https://example.com/Book"); // undefined (undeclared namespace)

    // Expand prefixed names to IRIs

    expand(lexicon, "ex:Book"); // "https://example.org/Book"
    expand(lexicon, "owl:Class"); // "owl:Class" (undeclared prefix)
    base

    Base namespace for local resources.

    IRI

    Internationalized Resource Identifier (IRI) as defined by RFC 3987.

    Variant

    Identifier variant per RFC 3986 §§ 4.2-4.3.

    CURIE

    Compact URI abbreviating an IRI with a namespace prefix.

    Namespace

    Object type for accessing IRIs within a common namespace.

    Lexicon

    Namespace prefixes for writing IRIs as compact URIs.

    isIRI

    Checks if a value is a valid IRI.

    isNestedIRI

    Checks if a parent identifier nests a child identifier.

    getIRIBase

    Extracts the base identifier from a hierarchical identifier.

    getIRIParent

    Extracts the identifier of the collection enclosing a reference.

    getIRIHost

    Extracts the host from an identifier.

    getIRILabel

    Derives a readable label from an identifier.

    resolve

    Resolves a reference against a base identifier.

    internalize

    Extracts a root-relative reference.

    relativize

    Creates a relative reference from base to reference.

    createNamespace

    Creates an immutable Namespace object for generating and accessing IRIs from a common base.

    getNamespaceIRI

    Retrieves the IRI of a namespace.

    getNamespaceBase

    Retrieves the base identifier of a namespace.

    createLexicon

    Creates an immutable Lexicon.

    isCURIE

    Checks if a value is a valid compact URI.

    compact

    Converts an IRI to a compact URI, if a declared namespace abbreviates it.

    expand

    Expands a compact URI to an IRI.