Class ROCrate

Class for building, navigating, testing and rendering ROCrates

import validation and rendering from Calcyte

Constructors

  • Create a new ROCrate object using a default template or from a valid jsonld object.

    Parameters

    • json: any = {}

      a valid jsonld object

    • Optionalconfig: {
          array?: boolean;
          defaultType?: string;
          duplicate?: boolean;
          link?: boolean;
          merge?: boolean;
          replace?: boolean;
      }
      • Optionalarray?: boolean

        Always return property of an Entity as an array (eg when using getEntity() method)

      • OptionaldefaultType?: string

        The default value for @type to be used when adding a new entity and the property is not specified. Default to 'Thing'

      • Optionalduplicate?: boolean

        Allow duplicate values in a property that has multiple values

      • Optionallink?: boolean

        Resolve linked node as nested object

      • Optionalmerge?: boolean

        When replacing or updating an entity, merge the values and the properties instead of full replace

      • Optionalreplace?: boolean

        When importing from json, a subsequent duplicate entity always replaces the existing one

    Returns ROCrate

Properties

__context: (string | {})[]

Internal representation of the context

__contextCache: {} = {}

A cache of resolved context content

__contextIriIndex: Map<any, any> = ...

Index for context IRI that is irregular

__handler: Handler

Lookup table to index nodes by their properties

__handlerReverse: { get(target: any, prop: any): any }
__nodeById: Map<string, Node> = ...

Lookup table to get a reference to existing and non-existing nodes. This is needed to avoid searching for the whole graph for every "@reverse" lookup as an entity referenced by other entities may not exist yet in the graph.

config: {}
defaults: {
    back_back_links: Set<any>;
    back_links: {};
    context: (string | { "@vocab": string })[];
    datasetTemplate: { "@id": string; "@type": string };
    metadataFileDescriptorTemplate: {
        "@id": string;
        "@type": string;
        about: { "@id": string };
        identifier: string;
    };
    pageSize: number;
    render_script: string;
    ro_crate_name: string;
    ROCrate_Specification_Identifier: string;
    roCrateMetadataID: string;
    roCrateMetadataIDs: string[];
    roCratePreviewFileName: string;
} = defaults
utils: typeof Utils = Utils

Import Utils class directly

defaults: {
    back_back_links: Set<any>;
    back_links: {};
    context: (string | { "@vocab": string })[];
    datasetTemplate: { "@id": string; "@type": string };
    metadataFileDescriptorTemplate: {
        "@id": string;
        "@type": string;
        about: { "@id": string };
        identifier: string;
    };
    pageSize: number;
    render_script: string;
    ro_crate_name: string;
    ROCrate_Specification_Identifier: string;
    roCrateMetadataID: string;
    roCrateMetadataIDs: string[];
    roCratePreviewFileName: string;
} = defaults

Accessors

  • get "@context"(): string | {} | (string | {})[]

    Returns string | {} | (string | {})[]

  • get context(): string | {} | (string | {})[]

    The context part of the crate. An alias for '@context'. This returns the original context information.

    Returns string | {} | (string | {})[]

  • get graph(): any[]

    An array of all nodes in the graph. An alias for '@graph'

    Returns any[]

  • get metadata(): Entity

    The Metadata File Descriptor, which describes the RO-Crate Metadata File and links it to the Root Data Entity.

    Returns Entity

  • get rootId(): string

    The root identifier of the RO Crate

    Returns string

  • set rootId(newId: string): void

    Parameters

    • newId: string

    Returns void

Methods

  • Parameters

    • ref: any
    • prop: any
    • oldValues_: any
    • values: any
    • __namedParameters: { duplicate?: any; merge?: any; recurse?: boolean; replace?: any; seen?: any }

    Returns any

  • Create a new node or return an existing node with the given data

    Parameters

    • id: string

      Identifier of the node (@id)

    • Optionalref: NodeRef

      An immutable and unique reference to node that contains id and reverse information only

    Returns Node

    a newly created or existing node that matches the id

  • Return a proxy that wraps a node as an entity object supporting linked objects capability.

    Parameters

    • n: Node

    Returns any

  • Parameters

    • entity: Node
    • prop: string
    • values: any
    • opt: {
          duplicate?: boolean;
          merge?: boolean;
          recurse?: boolean;
          replace?: boolean;
          seen?: WeakSet<any>;
      }

    Returns boolean

  • Parameters

    • idOrEntity: any

    Returns Node

  • Init a new node or update existing one

    Parameters

    • node: Node
    • data: any

      Update the node with the given data

    • opt: {
          add?: boolean;
          merge?: boolean;
          recurse?: boolean;
          replace?: boolean;
          seen?: WeakSet<any>;
      }
      • Optionaladd?: boolean

        If true, create an entity even if the data is empty

      • Optionalmerge?: boolean

        If false and if node already exists, remove all existing properties not in the specified data

      • Optionalrecurse?: boolean

        Process nested objects recursively

      • Optionalreplace?: boolean

        If false and if node already exists, do nothing to the node

      • Optionalseen?: WeakSet<any>

        A set to keep track of cyclic reference in the input

    Returns boolean

    Return true if node is changed

  • Returns void

    Not required anymore. Calling this method will do nothing.

  • Append the specified string or object directly as an entry into the RO-Crate JSON-LD Context array. It does not check for duplicates or overlapping content if the context is an object.

    Parameters

    • context: any

      A URL or an Object that contains the context mapping

    Returns void

  • Add an entity to the crate.

    Parameters

    • data: any

      A valid RO-Crate entity described in plain object.

    • opt: { recurse?: boolean; replace?: boolean } = {}
      • Optionalrecurse?: boolean

        If true, nested entities will be added as well.

      • Optionalreplace?: boolean

        If true, replace existing entity with the same id.

    Returns boolean

    true if the entity is successfully added.

  • Add a new identifier as a PropertyValue to the Root Data Entity. identifier and name are required parameters

    Parameters

    • options: { description?: string; identifier: string; name: string }

    Returns string

    The added identifier or undefined

  • This silently fails if the item has no

    Parameters

    • item: any

    Returns boolean

    or already exists - this is probably sub-optimal

    Use addEntity

  • Add a Profile URI to the crate

    Parameters

    • uri: string

      A valid Profile URI

    Returns void

  • Parameters

    • prov: any

    Returns void

  • Add the term and its definition to the first context definition (a map) found from the @context entries. If no existing context definition found, a new one will be created.

    Parameters

    • term: string

      The term name like 'name' or 'schema:name'

    • definition: any

      The term IRI

    • Optionalforce: boolean

      If the term is in the format of prefix:suffix and force is true, add the term to the context as it is

    Returns void

  • Add one or more value to a property of an entity. If the specified property does not exists, a new one will be set. If the property already exists, the new value will be added to the property array.

    Parameters

    • idOrEntity: any

      The id or the entity to add the property to

    • prop: string

      The name of the property

    • values: any

      The value of the property

    • opt: { duplicate?: boolean } = {}
      • Optionalduplicate?: boolean

        If true, allow a property to have duplicate values in the array

    Returns boolean

  • Transform the property names of all entities by replacing it with the specified prefixes

    Parameters

    • prefixes: { [key: string]: string }

      The set of prefixes to use for the compaction

    Returns void

  • Parameters

    • subgraphs: any

    Returns any[]

    Use Utils.union, eg: union([sg1, sg2])

  • Delete an entity from the graph

    Parameters

    • id_or_entity: string | Entity

      Entity Identifier or the entity object itself

    • opt: { references?: boolean } = {}
      • Optionalreferences?: boolean

        Set true to delete all references to the deleted entity

    Returns boolean

    True if any existing entity was deleted

  • Parameters

    • id: string

    Returns any

    The raw data of deleted entity

    Use deleteEntity

  • Delete a property of an Entity

    Parameters

    • idOrEntity: any

      The id of the entity to add the property to

    • prop: string

      The name of the property

    Returns boolean

    • True, if the property has been deleted
  • Delete one or more values from a property.

    Parameters

    • idOrEntity: string | Entity
    • prop: string
    • values: any

    Returns void

  • Returns a new iterator object that contains the entities in the graph.

    Parameters

    • p: { filter?: any; flat?: boolean } = {}
      • Optionalfilter?: any

        Filter the result based on the values of the properties defined in this object.

      • Optionalflat?: boolean

        If true, return the copy of entity as a plain object.

    Returns {
        "[iterator]"(): { [Symbol.iterator](): ...; next(): { done: boolean; value: any; }; };
        next(): { done: boolean; value: any };
    }

  • Experimental method to turn a graph into a flat dictionary eg for turning it into a table

    Parameters

    • item: any
    • depth: any
    • flatItem: any
    • propPath: any
    • seen: any

    Returns any

  • Get configuration value

    Parameters

    • key: "link" | "array" | "duplicate" | "replace" | "merge"

      Name of the config parameter

    Returns any

  • Parameters

    • term: any

    Returns any

  • Get the context term definition. This method will also search for term defined locally in the graph. Make sure resolveContext() has been called prior calling this method.

    Parameters

    • term: string

    Returns any

  • Get an entity from the graph. If config.link is true, any reference (object with just "@id" property) is resolved as a nested object.

    Parameters

    • id: string

      An entity identifier

    Returns Entity

    A wrapper for entity that resolves properties as linked objects

  • Get an array of all nodes in the graph. Each node in the array is an Entity instance. If config.link is true, any link to other node will be made into nested object.

    Parameters

    • flat: boolean = false

      If true, return the copy of entity as a plain object.

    Returns any[]

  • Get named identifier

    Parameters

    • name: string

    Returns string

    the identifier

  • Returns { "@context": (string | {})[]; "@graph": any[] }

    Use toJSON

  • Parameters

    • root: any
    • depth: number = 1

    Returns any

    Use getTree with the following argument: { root, depth, allowCycle: true }

  • Get the property of an entity

    Parameters

    • idOrEntity: any
    • prop: string

    Returns any

    the value of the property

  • Get the context term name from it's definition id. Make sure resolveContext() has been called prior calling this method.

    Parameters

    • definition: any

    Returns any

  • Return a JSON.stringify-ready tree structure starting from the specified item with all values (apart from @id) as arrays and string-values expressed like: {"@value": "string-value"}

    Parameters

    • opt: { allowCycle?: boolean; depth?: number; root?: any; valueObject?: boolean } = {}
      • OptionalallowCycle?: boolean
      • Optionaldepth?: number

        The number of nesting the tree will have. Must be 0 or positive integer.

      • Optionalroot?: any
      • OptionalvalueObject?: boolean

    Returns any

    the root entity

  • Check if entity exists in the graph

    Parameters

    • id: string

      An entity identifier

    Returns boolean

  • Check if an entity has a type

    Parameters

    • item: any
    • type: string

    Returns boolean

  • Add a remote URL to the context and resolve the entries.

    Parameters

    • contextUrl: string | string[]

    Returns Promise<void>

  • Returns void

    Not required anymore. Calling this method will do nothing.

  • Get the index of the entity in the graph array. This is an O(n) operation.

    Parameters

    • entityId: string

    Returns number

  • Create a simple tree-like object - but don't make circular structures

    Returns any

    Use getTree with the valueObject argument set to false`

  • Add a value to an item's property array

    Parameters

    • item: any
    • prop: string
    • val: any
    • allowDuplicates: boolean = false

    Returns void

    Use addValues

  • Parameters

    • items: any

      A JSON-LD item or array of [item]

    • pathArray: any[]

      An array of objects that represents a 'path' through the graph. Object must have a "property" to follow, eg: resolve(item, {"property": "miltaryService"}); and optionally a condition "includes", eg: "includes": {"@type", "Action"}} and optionally, a function "matchFn" which takes an item as argument and returns a boolean, eg: "matchFn": (item) => item['@id'].match(/anzsrc-for/)

    • Optionalsubgraph: any[]

      If present and true, all intervening items during the traversal will be stored. If an array is passed, the intervening items will be stored in the array.

    Returns any[]

    null, or an array of items

  • resolveAll does a resolve but collects and deduplicates intermediate items. Its first returned value is the final items (ie what resolve(..)) would have returned.

    Parameters

    • items: any
    • pathArray: any

    Returns any[][]

  • Generate a local flat lookup table for context terms

    Parameters

    • refreshCache: boolean = false

    Returns Promise<{ getDefinition(term: any): any; getTerm(definition: any): any }>

  • Expand a term into the IRI, which is the same as the @id of the term definition. Make sure resolveContext() has been called prior calling this method.

    Parameters

    • term: string

      a short word defined in the context

    Returns string

  • Returns any[]

    Use getGraph and pass true as the argument

  • Set a property of an entity with the given value. If a property with the same name exists, its existing value will be replaced with the specified value. If values contain nested non-empty entities, they will be processed recursively.

    Parameters

    • idOrEntity: any

      The id of the entity to add the property to

    • prop: string

      The name of the property

    • values: any

      A value or an array of values

    • opt: { duplicate?: boolean } = {}
      • Optionalduplicate?: boolean

        If true, allow a property to have duplicate values

    Returns boolean

  • Returns boolean

    Specify {array: true, link: true} in the options when creating the ROCrate instance

  • Convert the rocrate into plain JSON object. The value returned by this method is used when JSON.stringify() is used on the ROCrate object.

    Returns { "@context": (string | {})[]; "@graph": any[] }

    plain JSON object

  • Generate a new unique id that does not match any existing id in the graph.

    Parameters

    • base: string

      The base string of the id.

    Returns string

    The base suffixed with the incremental number.

  • Update an entity by replacing the object with the same id. This operations will remove all properties of the existing entity and add the new ones contained in data, unless merge argument is true.

    Parameters

    • data: any
    • opt: { merge?: boolean; recurse?: boolean } = {}
      • Optionalmerge?: boolean

        If true, new properties will be merged. Defaults to config.merge.

      • Optionalrecurse?: boolean

        If true, nested entities will be updated as well.

    Returns boolean

    false if there is no existing entity with the same id or data is empty.

  • Change the identifier of an entity node

    Parameters

    • idOrEntity: any
    • newId: string

    Returns boolean

  • Create a new ROCrate instance with default configuration set as the following:

    • array: true
    • link: true
    • replace: false
    • merge: true
    • duplicate: false and resolve all the included context

    Parameters

    • json: any = {}

      a valid jsonld object

    Returns Promise<ROCrate>