Mercurial > code > home > repos > streamed-graph
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 |
rev | line source |
---|---|
35 | 1 import * as jsonld from "jsonld"; |
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 | 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 // 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 | 9 function _emitQuad( |
10 onQuad: (q: Quad) => void, | |
11 subjNode: NamedNode, | |
12 pred: string, | |
13 subj: any, | |
14 graphNode: NamedNode) { | |
15 let predNode: NamedNode; | |
16 if (pred === "@type") { | |
17 predNode = namedNode(rdf.type); | |
18 } | |
19 else { | |
20 predNode = namedNode(pred); | |
21 } | |
22 subj[pred as string].forEach(function (obj: any) { | |
23 const objNode = (obj['@id'] ? namedNode(obj['@id']) : | |
24 literal(obj['@value'], | |
25 obj['@language'] || obj['@type'])); | |
26 onQuad(quad(subjNode, predNode, objNode, graphNode)); | |
27 }); | |
28 } | |
13 | 29 |
30 export async function eachJsonLdQuad(jsonLdObj: object, onQuad: (q: Quad) => void) { | |
35 | 31 const expanded = await jsonld.expand(jsonLdObj); |
13 | 32 |
35 | 33 (expanded as JsonLdArray).forEach(function (g: JsonLd) { |
34 var graph = (g as { '@id': string })['@id']; | |
35 var graphNode = namedNode(graph); | |
36 (g as { '@graph': JsonLdArray })['@graph'].forEach(function (subj: { [predOrId: string]: any; }) { | |
37 const subjNode = namedNode(subj['@id']); | |
38 for (let pred in subj) { | |
39 if (pred === '@id') { | |
40 continue; | |
41 } | |
42 _emitQuad(onQuad, subjNode, pred, subj, graphNode); | |
43 } | |
44 }); | |
45 }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
46 } |