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

    Module resource

    RFC 3987 resource identifiers.

    Provides types and functions for resource identifiers (IRIs), namespace factories, and reference resolution.

    Type Guards

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

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

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

    Identifier Factories

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

    // Absolute identifiers

    const absolute = asIRI("http://example.org/resource", "absolute");

    // Relative references

    const relative = asIRI("../resource", "relative");

    // Root-relative (internal) paths

    const internal = asIRI("/resource", "internal");

    // Unicode in IRIs

    const unicode = asIRI("http://example.org/资源", "absolute");

    Nesting Checks

    import { nests, asIRI } from "@metreeca/core/resource";

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

    Reference Operations

    import { base, resolve, relativize, internalize, asIRI } from "@metreeca/core/resource";

    const iri = asIRI("http://example.com/a/b/c", "absolute");

    // Resolve relative references against base

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

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

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

    // Convert absolute to relative path

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

    // Extract the base identifier usable for reference resolution

    base(iri); // "http://example.com/"
    base(asIRI("/a/b", "internal")); // undefined

    Namespace Objects

    import { createNamespace } 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

    // Open namespace for dynamic terms

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

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

    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.

    asIRI

    Creates a validated IRI from a string.

    nests

    Checks if a parent identifier nests a child identifier.

    base

    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.