Mercurial > code > home > repos > streamed-graph
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 |
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 | 4 const { literal, quad, namedNode } = DataFactory; |
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 | 10 function _emitQuad( |
11 onQuad: (q: Quad) => void, | |
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 | 14 subj: any, |
15 graphNode: NamedNode) { | |
16 let predNode: NamedNode; | |
17 if (pred === "@type") { | |
18 predNode = namedNode(rdf.type); | |
19 } | |
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 | 22 } |
23 subj[pred as string].forEach(function (obj: any) { | |
24 const objNode = (obj['@id'] ? namedNode(obj['@id']) : | |
25 literal(obj['@value'], | |
26 obj['@language'] || obj['@type'])); | |
27 onQuad(quad(subjNode, predNode, objNode, graphNode)); | |
28 }); | |
29 } | |
30 | |
31 export async function eachJsonLdQuad(jsonLdObj: object, onQuad: (q: Quad) => void) { | |
32 | |
33 return new Promise(function (resolve, reject) { | |
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 | 36 if (err) { |
37 reject(err); | |
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 | 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 | 44 const subjNode = namedNode(subj['@id']); |
45 for (let pred in subj) { | |
46 if (pred === '@id') { | |
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 | 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 ; |