All files util.ts

100% Statements 17/17
100% Branches 5/5
100% Functions 6/6
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 692x   2x                                           2x     29x               2x     2x     2x     5x         2x     7x     2x     7x 7x 7x       3x   4x    
import { namedNode } from "@rdfjs/data-model";
import { Dataset } from "@rdfjs/types";
import {
  ArrayProxy,
  getProxyFromObject,
  GraphType,
  ObjectType,
  PredicateType,
  SubjectProxy,
  SubjectType,
  _getUnderlyingDataset,
  _proxyContext,
} from "jsonld-dataset-proxy";
import { Quad } from "n3";
import { SubscribableDataset, TransactionalDataset } from "o-dataset-pack";
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type LdoBase = Record<string, any>;
 
/**
 * Converts a node/string into just a node
 * @param input A Node or string
 * @returns A node
 */
export function normalizeNodeName<
  NodeType extends SubjectType | PredicateType | ObjectType | GraphType
>(input: NodeType | string): NodeType {
  return (typeof input === "string" ? namedNode(input) : input) as NodeType;
}
 
/**
 * Converts an array of nodes/strings into an array of nodes
 * @param inputs An array of nodes/strings
 * @returns An array of nodes
 */
export function normalizeNodeNames<
  NodeType extends SubjectType | PredicateType | ObjectType | GraphType
>(inputs: (NodeType | string)[]): NodeType[] {
  return inputs.map((input) => normalizeNodeName<NodeType>(input));
}
 
export function canDatasetStartTransaction(
  dataset: Dataset
): dataset is SubscribableDataset<Quad> {
  return (
    typeof (dataset as SubscribableDataset).startTransaction === "function"
  );
}
 
export function isTransactionalDataset(
  dataset: Dataset
): dataset is TransactionalDataset<Quad> {
  return typeof (dataset as TransactionalDataset).commit === "function";
}
 
export function getTransactionalDatasetFromLdo(
  ldo: LdoBase
): [TransactionalDataset<Quad>, SubjectProxy | ArrayProxy] {
  const proxy = getProxyFromObject(ldo);
  const dataset = proxy[_getUnderlyingDataset];
  if (
    !isTransactionalDataset(dataset) ||
    !proxy[_proxyContext].state.parentDataset
  ) {
    throw new Error("Object is not currently in a transaction");
  }
  return [dataset, proxy];
}