annotate src/layout/json_ld_quads.ts @ 128:5a1a79f54779

big rewrite
author drewp@bigasterisk.com
date Fri, 05 May 2023 21:26:36 -0700
parents 2468f2227d22
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 106
diff changeset
1 // unused?
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 106
diff changeset
2
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
3 import * as jsonld from "jsonld";
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
4 import { JsonLd, JsonLdArray } from "jsonld/jsonld-spec";
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
5 import { Quad, NamedNode, DataFactory } from "n3";
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
6 const { literal, quad, namedNode } = DataFactory;
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
7
20
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
8 // const { rdf } = ns;
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 20
diff changeset
9 const rdf = { type: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" };
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
10
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
11 function parseObjNode(obj: any) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
12 if (obj["@id"]) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
13 return namedNode(obj["@id"]);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
14 } else {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
15 if (obj["@value"] === undefined) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
16 throw new Error("no @id or @value");
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
17 }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
18 return literal(obj["@value"], obj["@language"] || obj["@type"]);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
19 }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
20 }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
21
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
22 function parsePred(
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
23 onQuad: (q: Quad) => void,
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
24 graphNode: NamedNode,
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
25 subjNode: NamedNode,
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
26 predKey: string,
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
27 subjGroup: any
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
28 ) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
29 let predNode: NamedNode;
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
30 if (predKey === "@type") {
51
77aa2e9234ed bug with rdf:type stmts
drewp@bigasterisk.com
parents: 46
diff changeset
31 subjGroup["@type"].forEach((aType: string) => {
77aa2e9234ed bug with rdf:type stmts
drewp@bigasterisk.com
parents: 46
diff changeset
32 onQuad(quad(subjNode, namedNode(rdf.type), namedNode(aType), graphNode));
77aa2e9234ed bug with rdf:type stmts
drewp@bigasterisk.com
parents: 46
diff changeset
33 });
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
34 return;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
35 }
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
36 predNode = namedNode(predKey);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
37 subjGroup[predKey].forEach(function(obj: any) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
38 const objNode = parseObjNode(obj);
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
39 onQuad(quad(subjNode, predNode, objNode, graphNode));
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
40 });
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
41 }
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
42 function parseSubj(
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
43 onQuad: (q: Quad) => void,
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
44 graphNode: NamedNode,
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
45 subjGroup: { [predOrId: string]: any }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
46 ) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
47 const subjNode = namedNode(subjGroup["@id"]);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
48 for (let predKey in subjGroup) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
49 if (predKey === "@id") {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
50 continue;
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
51 }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
52 parsePred(onQuad, graphNode, subjNode, predKey, subjGroup);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
53 }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
54 }
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
55 function parseGraph(onQuad: (q: Quad) => void, g: JsonLd) {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
56 var graph = (g as { "@id": string })["@id"];
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
57 var graphNode = namedNode(graph);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
58 (g as { "@graph": JsonLdArray })["@graph"].forEach(subj => {
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
59 parseSubj(onQuad, graphNode, subj);
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
60 });
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
61 }
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
62
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
63 export async function eachJsonLdQuad(
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
64 jsonLdObj: object,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
65 onQuad: (q: Quad) => void
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
66 ) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
67 const expanded = await jsonld.expand(jsonLdObj);
46
709e305dbd4f bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents: 36
diff changeset
68 (expanded as JsonLdArray).forEach((g: JsonLd) => parseGraph(onQuad, g));
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
69 }