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

    Module resource

    RFC 3987 resource identifiers.

    Provides types and functions for validating resource identifiers (IRIs), checking nesting relationships, resolving and rewriting references, creating and inspecting namespace objects, and minting IRIs for application-local resources through the predefined app namespace.

    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, 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

    Namespace Objects

    import { app, 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 application-local resources

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

    Variables

    app

    Application namespace.

    Type Aliases

    IRI

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

    Variant

    Identifier variant per RFC 3986 §§ 4.2-4.3.

    Namespace

    Object type for accessing IRIs within a common namespace.

    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.

    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.