annotate src/json_ld_quads.ts @ 22:e90d9021c6a0

add back s-g code; this breaks the build a little. WIP
author drewp@bigasterisk.com
date Fri, 13 Dec 2019 01:39:29 -0800
parents 9ec3cbc8791a
children 7fe46400f04b
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
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
33 return new Promise(function (resolve, reject) {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
34
20
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
35 jsonld.expand(jsonLdObj, function onExpand(err, expanded: JsonLd) {
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
36 if (err) {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
37 reject(err);
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
38 }
20
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
39 (expanded as JsonLdArray).forEach(function (g: JsonLd) {
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
40 var graph = (g as { '@id': string })['@id'];
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
41 var graphNode = namedNode(graph);
20
9ec3cbc8791a build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents: 15
diff changeset
42 (g as { '@graph': JsonLdArray })['@graph'].forEach(function (subj: { [predOrId: string]: any; }) {
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
43 console.log('process subj', subj)
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
44 const subjNode = namedNode(subj['@id']);
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
45 for (let pred in subj) {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
46 if (pred === '@id') {
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
47 continue;
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
48 }
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
49 console.log('emit with', pred);
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
50 _emitQuad(onQuad, subjNode, pred, subj, graphNode);
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
51 }
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
52 });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
53 });
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
54 resolve();
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
55 });
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
56 });
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
57 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
58 ;