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 | 2x 2x 2x 2x 2x 19x 2x 4x 2x 1x 1x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 2x 2x | import { NamedNode, BlankNode, Dataset, Quad } from "@rdfjs/types";
import {
createSubscribableDataset,
serializedToSubscribableDataset,
WrapperSubscribableDataset,
} from "o-dataset-pack";
import { createLinkedDataObject } from "./createLinkedDataObject";
import { LinkedDataObject } from "./LinkedDataObject";
import { ShapeType } from "./ShapeType";
import jsonldDatasetProxy from "jsonld-dataset-proxy";
import df from "@rdfjs/data-model";
import { ParserOptions } from "n3";
import { JsonLdDocument } from "jsonld";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class LdoFactory<Type extends Record<string, any>> {
private shapeType: ShapeType<Type>;
constructor(shapeType: ShapeType<Type>) {
this.shapeType = shapeType;
}
public async parse(
id: string | NamedNode | BlankNode,
data: string | JsonLdDocument | Dataset,
options?: ParserOptions
): Promise<LinkedDataObject<Type>> {
let dataset: WrapperSubscribableDataset<Quad>;
if (typeof data === "string") {
// Input data is serialized
dataset = await serializedToSubscribableDataset(data, options);
} else if (Itypeof (data as Dataset).add === "function") {
// Input data is a dataset
dataset = createSubscribableDataset(data as Dataset);
} else {
dataset = await serializedToSubscribableDataset(JSON.stringify(data), {
format: "application/json-ld",
});
}
const entryNode = typeof id === "string" ? df.namedNode(id) : id;
return createLinkedDataObject(
dataset.startTransaction(),
entryNode,
this.shapeType
);
}
public new(id?: string | NamedNode | BlankNode): LinkedDataObject<Type> {
const entryNode =
typeof id === "string" ? df.namedNode(id) : id || df.blankNode();
const dataset = createSubscribableDataset();
return createLinkedDataObject(
dataset.startTransaction(),
entryNode,
this.shapeType
);
}
public fromJson(inputData: Type): LinkedDataObject<Type> {
const dataset = createSubscribableDataset();
const entryNode = inputData["@id"]
? df.namedNode(inputData["@id"])
: df.blankNode();
const proxy = jsonldDatasetProxy<Type>(
dataset,
this.shapeType.context,
entryNode
);
Object.entries(inputData).forEach(([key, value]) => {
proxy[<keyof Type>key] = value;
});
return createLinkedDataObject(
dataset.startTransaction(),
entryNode,
this.shapeType
);
}
}
|