annotate src/json_ld_quads.ts @ 46:709e305dbd4f

bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
author drewp@bigasterisk.com
date Sun, 05 Jan 2020 23:54:00 -0800
parents 8b4dc9e87b56
children 77aa2e9234ed
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") {
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
29 onQuad(
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 quad(
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
31 subjNode,
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 namedNode(rdf.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
33 namedNode(subjGroup["@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
34 graphNode
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 )
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 );
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 return;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
38 }
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
39 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
40 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
41 const objNode = parseObjNode(obj);
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
42 onQuad(quad(subjNode, predNode, objNode, graphNode));
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
43 });
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
44 }
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
45 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
46 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
47 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
48 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
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 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
51 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
52 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
53 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
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 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
56 }
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 }
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 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
59 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
60 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
61 (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
62 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
63 });
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
64 }
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
65
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
66 export async function eachJsonLdQuad(
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
67 jsonLdObj: object,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
68 onQuad: (q: Quad) => void
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
69 ) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 35
diff changeset
70 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
71 (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
72 }