Mercurial > code > home > repos > streamed-graph
annotate src/json_ld_quads.ts @ 15:7ca4ff2088c3
managed to use a newer ts or something, so this includes a bunch of type fixes too
author | drewp@bigasterisk.com |
---|---|
date | Sun, 08 Dec 2019 23:32:12 -0800 |
parents | b9f451791f3a |
children | 9ec3cbc8791a |
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
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
|
15 pred: string, |
13 | 16 subj: any, |
17 graphNode: NamedNode) { | |
18 let predNode: NamedNode; | |
19 if (pred === "@type") { | |
20 predNode = namedNode(rdf.type); | |
21 } | |
22 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
|
23 predNode = namedNode(pred); |
13 | 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); | |
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
|
44 g['@graph'].forEach(function (subj: { [predOrId: string]: any; }) { |
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
|
45 console.log('process subj', subj) |
13 | 46 const subjNode = namedNode(subj['@id']); |
47 for (let pred in subj) { | |
48 if (pred === '@id') { | |
49 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
|
50 } |
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
|
51 console.log('emit with', pred); |
13 | 52 _emitQuad(onQuad, subjNode, pred, subj, graphNode); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
53 } |
13 | 54 }); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
55 }); |
13 | 56 resolve(); |
3
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 }); |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
59 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
60 ; |