All files LdoBuilder.ts

100% Statements 20/20
100% Branches 12/12
100% Functions 8/8
100% Lines 18/18

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                  2x           2x               34x 34x             3x 1x                   2x 4x   1x                       2x 21x                       2x         2x                           2x         2x                           2x 2x   2x  
import {
  GraphType,
  JsonldDatasetProxyBuilder,
  QuadMatch,
  SubjectType,
  _proxyContext,
  LanguageOrdering,
} from "jsonld-dataset-proxy";
import { ShapeType } from "./ShapeType";
import { LdoBase, normalizeNodeName, normalizeNodeNames } from "./util";
 
/**
 * A wrapper around Jsonld Dataset Proxy Builder with a slightly more friendly
 * user experience that doesn't require the use of rdfjs datatypes.
 */
export class LdoBuilder<Type extends LdoBase> {
  private jsonldDatasetProxyBuilder: JsonldDatasetProxyBuilder;
  private shapeType: ShapeType<Type>;
 
  constructor(
    jsonldDatasetProxyBuilder: JsonldDatasetProxyBuilder,
    shapeType: ShapeType<Type>
  ) {
    this.jsonldDatasetProxyBuilder = jsonldDatasetProxyBuilder;
    this.shapeType = shapeType;
  }
 
  /**
   * Designates that all Linked Data Objects created should write to the
   * specified graphs
   */
  write(...graphs: (GraphType | string)[]): LdoBuilder<Type> {
    return new LdoBuilder(
      this.jsonldDatasetProxyBuilder.write(...normalizeNodeNames(graphs)),
      this.shapeType
    );
  }
 
  /**
   * Sets the order of language preferences for Language Strings. Acceptable
   * values as EITF language tags, "@none" and "@other"
   */
  setLanguagePreferences(
    ...languageOrdering: LanguageOrdering
  ): LdoBuilder<Type> {
    return new LdoBuilder(
      this.jsonldDatasetProxyBuilder.setLanguagePreferences(
        ...languageOrdering
      ),
      this.shapeType
    );
  }
 
  /**
   * Creates a Linked Data Object that matches the given subject
   * @param subject The node to match
   */
  fromSubject(subject: SubjectType | string): Type {
    return this.jsonldDatasetProxyBuilder.fromSubject<Type>(
      normalizeNodeName(subject)
    );
  }
 
  /**
   * Matches Subjects to provided predicates, objects, and graphs. Returns a
   * JSON LD Dataset that can be read an modified.
   * @param predicate The predicate to match
   * @param object The object to match
   * @param graph The graph to match
   */
  matchSubject(
    predicate: QuadMatch[1] | string,
    object?: QuadMatch[2] | string,
    graph?: QuadMatch[3] | string
  ): Type[] {
    return this.jsonldDatasetProxyBuilder.matchSubject<Type>(
      predicate != undefined ? normalizeNodeName(predicate) : undefined,
      object != undefined ? normalizeNodeName(object) : undefined,
      graph != undefined ? normalizeNodeName(graph) : undefined
    );
  }
 
  /**
   * Matches Objects to provided subjects, predicates, and graphs. Returns a
   * collection of Linked Data Objects that can be read an modified.
   * @param subject The subject to match
   * @param predicate The predicate to match
   * @param graph The graph to match
   */
  matchObject(
    subject?: QuadMatch[0] | string,
    predicate?: QuadMatch[1] | string,
    graph?: QuadMatch[3] | string
  ): Type[] {
    return this.jsonldDatasetProxyBuilder.matchObject<Type>(
      subject != undefined ? normalizeNodeName(subject) : undefined,
      predicate != undefined ? normalizeNodeName(predicate) : undefined,
      graph != undefined ? normalizeNodeName(graph) : undefined
    );
  }
 
  /**
   * Takes a given object and places it in the dataset while returning a Linked
   * Data Object representing the object.
   *
   * @param inputData Initial Data
   * @param graph Optional graph to save this data to
   */
  fromJson(inputData: Type): Type {
    return this.jsonldDatasetProxyBuilder.fromJson<Type>(inputData);
  }
}