annotate src/json_ld_quads.ts @ 35:29d8ed02a275

build and tests, including jsonld
author drewp@bigasterisk.com
date Sat, 28 Dec 2019 02:01:23 -0800
parents 3d8b98e9c01d
children 8b4dc9e87b56
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";
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
2 import { JsonLd, JsonLdArray } from 'jsonld/jsonld-spec';
20
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 // 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
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
9 function _emitQuad(
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
10 onQuad: (q: Quad) => void,
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
11 subjNode: NamedNode,
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
12 pred: string,
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
13 subj: any,
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
14 graphNode: NamedNode) {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
15 let predNode: NamedNode;
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
16 if (pred === "@type") {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
17 predNode = namedNode(rdf.type);
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
18 }
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
19 else {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
20 predNode = namedNode(pred);
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
21 }
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
22 subj[pred as string].forEach(function (obj: any) {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
23 const objNode = (obj['@id'] ? namedNode(obj['@id']) :
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
24 literal(obj['@value'],
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
25 obj['@language'] || obj['@type']));
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
26 onQuad(quad(subjNode, predNode, objNode, graphNode));
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
27 });
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
28 }
13
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 export async function eachJsonLdQuad(jsonLdObj: object, onQuad: (q: Quad) => void) {
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
31 const expanded = await jsonld.expand(jsonLdObj);
13
b9f451791f3a refactor and update jsonldquads. WIP
drewp@localhost
parents: 8
diff changeset
32
35
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
33 (expanded as JsonLdArray).forEach(function (g: JsonLd) {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
34 var graph = (g as { '@id': string })['@id'];
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
35 var graphNode = namedNode(graph);
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
36 (g as { '@graph': JsonLdArray })['@graph'].forEach(function (subj: { [predOrId: string]: any; }) {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
37 const subjNode = namedNode(subj['@id']);
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
38 for (let pred in subj) {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
39 if (pred === '@id') {
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
40 continue;
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
41 }
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
42 _emitQuad(onQuad, subjNode, pred, subj, graphNode);
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
43 }
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
44 });
29d8ed02a275 build and tests, including jsonld
drewp@bigasterisk.com
parents: 34
diff changeset
45 });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
46 }