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