@metreeca/core - v0.13.0
    Preparing search index...

    Module resource

    RFC 3987 resource identifiers.

    Provides types and functions for validating resource identifiers (IRIs), checking nesting relationships and navigating identifier hierarchies, 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.

    Type Guards

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

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

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

    // Variant-specific checks

    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 "@metreeca/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, getIRIParent, internalize, relativize, resolve } from "@metreeca/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"); // "./"

    Namespace Objects

    import { base, createNamespace, getNamespaceBase, getNamespaceIRI } from "@metreeca/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 "@metreeca/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)

    Variables

    base

    Base namespace for local resources.

    Type Aliases

    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.

    Functions

    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.

    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.