Mercurial > code > home > repos > streamed-graph
annotate src/json_ld_quads.ts @ 17:94629c39681c
trying to do ts+jest. WIP
author | drewp@bigasterisk.com |
---|---|
date | Thu, 12 Dec 2019 22:52:57 -0800 |
parents | b9f451791f3a |
children | 7ca4ff2088c3 |
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"; |
13 | 2 import { Quad, NamedNode, Literal, N3Store } from 'n3'; |
3 | |
4 | |
5 | |
6 import { DataFactory } from 'n3'; | |
7 const { literal, quad, namedNode } = DataFactory; | |
8 | |
8
6fefd287aff9
closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents:
3
diff
changeset
|
9 import ns from 'n3/src/IRIs'; |
13 | 10 const { rdf } = ns; |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
11 |
13 | 12 function _emitQuad( |
13 onQuad: (q: Quad) => void, | |
14 subjNode: NamedNode, | |
15 pred: string | { '@id': string }, | |
16 subj: any, | |
17 graphNode: NamedNode) { | |
18 let predNode: NamedNode; | |
19 if (pred === "@type") { | |
20 predNode = namedNode(rdf.type); | |
21 } | |
22 else { | |
23 predNode = namedNode(pred['@id']); | |
24 } | |
25 subj[pred as string].forEach(function (obj: any) { | |
26 const objNode = (obj['@id'] ? namedNode(obj['@id']) : | |
27 literal(obj['@value'], | |
28 obj['@language'] || obj['@type'])); | |
29 onQuad(quad(subjNode, predNode, objNode, graphNode)); | |
30 }); | |
31 } | |
32 | |
33 export async function eachJsonLdQuad(jsonLdObj: object, onQuad: (q: Quad) => void) { | |
34 | |
35 return new Promise(function (resolve, reject) { | |
36 | |
37 jsonld.expand(jsonLdObj, function onExpand(err, expanded) { | |
38 if (err) { | |
39 reject(err); | |
40 } | |
41 (expanded as [object]).forEach(function (g) { | |
42 var graph = g['@id']; | |
43 var graphNode = namedNode(graph); | |
44 g['@graph'].forEach(function (subj) { | |
45 const subjNode = namedNode(subj['@id']); | |
46 for (let pred in subj) { | |
47 if (pred === '@id') { | |
48 continue; | |
49 } 2 | |
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 | 52 }); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
53 }); |
13 | 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 ; |