annotate src/json_ld_quads.ts @ 72:d5f9ebc5edb7

dead end
author drewp@bigasterisk.com
date Tue, 11 Feb 2020 21:41:48 -0800
parents 7fe46400f04b
children 3d8b98e9c01d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
1 import * as jsonld from "jsonld";
20
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
2 import { JsonLd, JsonLdArray } from 'jsonld/jsonld-spec';
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
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 // import {} from 'n3';
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
7 // const { rdf } = ns;
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 20
diff changeset
8 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
9
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
10 function _emitQuad(
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
11 onQuad: (q: Quad) => void,
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
12 subjNode: NamedNode,
15
7ca4ff2088c3 managed to use a newer ts or something, so this includes a bunch of type fixes too
drewp@bigasterisk.com
parents: 13
diff changeset
13 pred: string,
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
14 subj: any,
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
15 graphNode: NamedNode) {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
16 let predNode: NamedNode;
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
17 if (pred === "@type") {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
18 predNode = namedNode(rdf.type);
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
19 }
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
20 else {
15
7ca4ff2088c3 managed to use a newer ts or something, so this includes a bunch of type fixes too
drewp@bigasterisk.com
parents: 13
diff changeset
21 predNode = namedNode(pred);
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
22 }
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
23 subj[pred as string].forEach(function (obj: any) {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
24 const objNode = (obj['@id'] ? namedNode(obj['@id']) :
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
25 literal(obj['@value'],
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
26 obj['@language'] || obj['@type']));
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
27 onQuad(quad(subjNode, predNode, objNode, graphNode));
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
28 });
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
29 }
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
30
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
31 export async function eachJsonLdQuad(jsonLdObj: object, onQuad: (q: Quad) => void) {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
32
26
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
33 const expanded = await jsonld.expand(jsonLdObj);
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
34
26
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
35 (expanded as JsonLdArray).forEach(function (g: JsonLd) {
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
36 var graph = (g as { '@id': string })['@id'];
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
37 var graphNode = namedNode(graph);
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
38 (g as { '@graph': JsonLdArray })['@graph'].forEach(function (subj: { [predOrId: string]: any; }) {
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
39 const subjNode = namedNode(subj['@id']);
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
40 for (let pred in subj) {
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
41 if (pred === '@id') {
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
42 continue;
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
43 }
7fe46400f04b jsonld module now does async
drewp@bigasterisk.com
parents: 22
diff changeset
44 _emitQuad(onQuad, subjNode, pred, subj, graphNode);
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
45 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
46 });
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
47 });
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
48 }