Mercurial > code > home > repos > streamed-graph
annotate src/json_ld_quads.ts @ 72:d5f9ebc5edb7
dead end
author | drewp@bigasterisk.com |
---|---|
date | Tue, 11 Feb 2020 21:41:48 -0800 |
parents | 7fe46400f04b |
children | 3d8b98e9c01d |
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 | |
26 | 33 const expanded = await jsonld.expand(jsonLdObj); |
13 | 34 |
26 | 35 (expanded as JsonLdArray).forEach(function (g: JsonLd) { |
36 var graph = (g as { '@id': string })['@id']; | |
37 var graphNode = namedNode(graph); | |
38 (g as { '@graph': JsonLdArray })['@graph'].forEach(function (subj: { [predOrId: string]: any; }) { | |
39 const subjNode = namedNode(subj['@id']); | |
40 for (let pred in subj) { | |
41 if (pred === '@id') { | |
42 continue; | |
43 } | |
44 _emitQuad(onQuad, subjNode, pred, subj, graphNode); | |
13 | 45 } |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
46 }); |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
47 }); |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
48 } |