Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @rdfdev/collections

js.rdf.dev/collections

Read the Docs npm (tag) npm bundle size

Utilities for reading and manipulating different kinds of RDF collections (rdf:Seq, rdf:List)

Example

import rdf from "@ontologies/core";
import { arrayToList, arrayToSeq, firstTermOfList, lastTermOfList, firstTermOfSeq, lastTermOfSeq, listToArray, seqToArray, Store } from "@rdfdev/collections";

const myArray = ["one", 2, rdf.namedNode("https://three.example/")];
const myStore: Store; // This can be the LinkedRenderStore from link-lib or your own.

// Converting an array to an rdf Sequence
const [ seqData, seqIRI ] =  arrayToSeq(myArray);
myStore.addQuads(seqData);
/**
 * The `seqData` would contain the following quads;
 * <seqIRI> a rdf:Seq .
 *      rdf:_0 "one" .
 *      rdf:_1 "2"^^http://www.w3.org/2001/XMLSchema#number .
 *      rdf:_2 <https://three.example/> ;
 */

// Reading
seqToArray(seqIRI); // [rdf.literal("one"), rdf.literal(2), rdf.namedNode("https://three.example/")]
firstTermOfSeq(myStore, listIRI); // rdf.literal("one")
lastTermOfSeq(myStore, listIRI); // rdf.namedNode("https://three.example/")

const [ listData, listIRI ] = arrayToList(myArray);
myStore.addQuads(listData);
/**
 * The `seqData` would contain the following quads;
 * <listIRI> rdf:first "one" .
 * <listIRI> rdf:rest _:0 .
 * _:0 rdf:first "2"^^http://www.w3.org/2001/XMLSchema#number .
 * _:0 rdf:rest _:1 .
 * _:1 rdf:first <https://three.example/> ;
 * _:1 rdf:rest rdf:nil ;
 */

// Reading
listToArray(listIRI); // [rdf.literal("one"), rdf.literal(2), rdf.namedNode("https://three.example/")]
firstTermOfList(myStore, listIRI); // rdf.literal("one")
lastTermOfList(myStore, listIRI); // rdf.namedNode("https://three.example/")

Getting started

Just install the package and its peer dependencies.

npm i @rdfdev/collections @ontologies/core @ontologies/ld @ontologies/rdf @ontologies/rdfs

yarn add @rdfdev/collections @ontologies/core @ontologies/ld @ontologies/rdf @ontologies/rdfs

Documentation

See the js.rdf.dev/collections documentation

See the complete js.rdf.dev documentation

Need help with linked data?

This package is brought to you by Ontola. We build production-grade linked data solutions and can help you from advice to building custom web services.

Index

Functions

arrayToList

  • arrayToList(arr: SomeTerm[], start?: Node): [Quad[], Node]
  • Convert an array of terms to a rdf:List.

    The quads are ordered, so arrayToList()[0]?.subject gives the list iri or undefined for an empty list.

    see

    {listToArray} for the inverse function.

    see

    {arrayToListQuads}

    Parameters

    • arr: SomeTerm[]

      The array to convert.

    • Optional start: Node

    Returns [Quad[], Node]

    An array with the first element the quads and the second the IRI of the list.

arrayToListQuads

  • arrayToListQuads(arr: SomeTerm[], start?: Node): Quad[]
  • Convert an array of terms to a rdf:list.

    The quads are ordered, so arrayToList()[0]?.subject gives the list iri or undefined for an empty list.

    see

    {listToArray} for the inverse function.

    see

    {arrayToList}

    Parameters

    • arr: SomeTerm[]

      The array to convert.

    • Optional start: Node

    Returns Quad[]

arrayToSeq

  • arrayToSeq(arr: SomeTerm[], start?: Node): [Quad[], Node]
  • Convert an array of terms to a rdf:Seq.

    see

    {seqToArray} for the inverse function.

    see

    {arrayToSeqQuads}

    Parameters

    • arr: SomeTerm[]

      The array to convert.

    • Optional start: Node

    Returns [Quad[], Node]

    An array with the first element the quads and the second the IRI of the seq.

arrayToSeqQuads

  • arrayToSeqQuads(arr: SomeTerm[], iri?: Node): Quad[]
  • Convert an array of terms to a rdf:list.

    The quads are ordered, so arrayToList()[0]?.subject gives the seq iri or undefined for an empty seq.

    see

    {seqToArray} for the inverse function.

    see

    {arrayToSeq}

    Parameters

    • arr: SomeTerm[]

      The array to convert.

    • Optional iri: Node

    Returns Quad[]

firstQuadOfList

  • firstQuadOfList(store: Store, listEntry: Node): Quad | undefined
  • Retrieve the first quad of the list at {listEntry}

    Parameters

    • store: Store
    • listEntry: Node

    Returns Quad | undefined

firstQuadOfSeq

  • firstQuadOfSeq(store: Store, seqIRI: Node): Quad | undefined

firstTermOfList

  • firstTermOfList(store: Store, listEntry: Node): SomeTerm | undefined
  • Retrieve the first term of the list at {listEntry}

    Parameters

    • store: Store
    • listEntry: Node

    Returns SomeTerm | undefined

firstTermOfSeq

  • firstTermOfSeq(store: Store, seqIRI: Node): SomeTerm | undefined
  • Retrieve the first term of the list at {listEntry}

    Parameters

    • store: Store
    • seqIRI: Node

    Returns SomeTerm | undefined

lastQuadOfList

  • lastQuadOfList(store: Store, listEntry: Node): Quad | undefined

lastQuadOfSeq

  • lastQuadOfSeq(store: Store, seqIRI: Node): Quad | undefined

lastTermOfList

  • lastTermOfList(store: Store, listEntry: Node): SomeTerm | undefined
  • Retrieve the last term of the list at {listEntry}

    Parameters

    • store: Store
    • listEntry: Node

    Returns SomeTerm | undefined

lastTermOfSeq

  • lastTermOfSeq(store: Store, seqIRI: Node): SomeTerm | undefined
  • Retrieve the last term of the list at {listIRI}

    Parameters

    • store: Store
    • seqIRI: Node

    Returns SomeTerm | undefined

listToArray

  • listToArray(store: Store, listEntry: Node): Term[]

orderedElementsOfList

  • orderedElementsOfList(store: Store, listEntry: Node): Quad[]
  • Convert a list from the point of {listEntry} to a Quad[].

    Will stop if missing links in the list aren't present. Can handle circular lists.

    Parameters

    • store: Store
    • listEntry: Node

    Returns Quad[]

orderedQuadsOfSeq

  • orderedQuadsOfSeq(store: Store, seqIRI: Node): Quad[]

seqMemberToNumber

  • seqMemberToNumber(member: NamedNode | undefined): number

seqPush

  • seqPush(store: Store, seqIRI: Node, value: Term | Term[], method?: NamedNode): Quad[]
  • Creates a delta to add one or more elements to the sequence.

    Parameters

    • store: Store

      The store that contains the sequence.

    • seqIRI: Node

      The IRI of the sequence

    • value: Term | Term[]

      The term or terms to push. Nested arrays aren't converted to nested sequences.

    • Default value method: NamedNode = ld.replace

      The delta method to use, uses replace by default.

    Returns Quad[]

seqShift

  • seqShift(store: Store, seqIRI: Node, method?: NamedNode): Quad[]
  • Creates a delta to remove the first element in the sequence.

    Parameters

    • store: Store

      The store that contains the sequence.

    • seqIRI: Node

      The IRI of the sequence

    • Default value method: NamedNode = ld.slice

      The delta method to use, uses slice by default.

    Returns Quad[]

seqToArray

  • seqToArray(store: Store, seqIRI: Node): Term[]

Generated using TypeDoc