annotate src/layout/json_ld_quads.ts @ 106:2468f2227d22

make src/layout/ and src/render/ separation
author drewp@bigasterisk.com
date Sun, 13 Mar 2022 22:00:30 -0700
parents src/json_ld_quads.ts@77aa2e9234ed
children 5a1a79f54779
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
1 import * as jsonld from "jsonld";
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
2 import { JsonLd, JsonLdArray } from "jsonld/jsonld-spec";
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
3 import { Quad, NamedNode, DataFactory } from "n3";
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
4 const { literal, quad, namedNode } = DataFactory;
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
5
20
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
6 // const { rdf } = ns;
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 20
diff changeset
7 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
8
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
9 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
10 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
11 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
12 } 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
13 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
14 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
15 }
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 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
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 }
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 function parsePred(
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
21 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
22 graphNode: NamedNode,
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
23 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
24 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
25 subjGroup: any
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
26 ) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
27 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
28 if (predKey === "@type") {
51
77aa2e9234ed bug with rdf:type stmts
drewp@bigasterisk.com
parents: 46
diff changeset
29 subjGroup["@type"].forEach((aType: string) => {
77aa2e9234ed bug with rdf:type stmts
drewp@bigasterisk.com
parents: 46
diff changeset
30 onQuad(quad(subjNode, namedNode(rdf.type), namedNode(aType), graphNode));
77aa2e9234ed bug with rdf:type stmts
drewp@bigasterisk.com
parents: 46
diff changeset
31 });
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
32 return;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
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 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
35 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
36 const objNode = parseObjNode(obj);
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
37 onQuad(quad(subjNode, predNode, objNode, graphNode));
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
38 });
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
39 }
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
40 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
41 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
42 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
43 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
44 ) {
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 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
46 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
47 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
48 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
49 }
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 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
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 }
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 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
54 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
55 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
56 (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
57 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
58 });
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 }
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
60
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
61 export async function eachJsonLdQuad(
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
62 jsonLdObj: object,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
63 onQuad: (q: Quad) => void
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
64 ) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
65 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
66 (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
67 }