Skip to content

Commit e971663

Browse files
authoredJul 8, 2022
Emit prefix and context events when parsing, Closes #22
1 parent 5d4adff commit e971663

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed
 

‎lib/RdfParser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export class RdfParser<Q extends RDF.BaseQuad = RDF.Quad> {
8282
.then((output) => {
8383
const quads = output.handle.data;
8484
quads.on('error', (e) => readable.emit('error', e));
85+
quads.on('prefix', (prefix, iri) => readable.emit('prefix', prefix, iri));
86+
quads.on('context', (ctx) => readable.emit('context', ctx));
8587
quads.pipe(readable);
8688
})
8789
.catch((e) => readable.emit('error', e));

‎test/RdfParser-test.ts

+73-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "jest-rdf";
22
const stringToStream = require('streamify-string');
33
const arrayifyStream = require('arrayify-stream');
44
const quad = require('rdf-quad');
5-
import {RdfParser} from "../lib/RdfParser";
5+
import { RdfParser } from "../lib/RdfParser";
66

77
import parser from "..";
88

@@ -51,7 +51,7 @@ describe('parser', () => {
5151
const stream = stringToStream(`
5252
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
5353
`);
54-
return expect(() => parser.parse(stream, <any> {}))
54+
return expect(() => parser.parse(stream, <any>{}))
5555
.toThrow(new Error('Missing \'contentType\' or \'path\' option while parsing.'));
5656
});
5757

@@ -110,6 +110,24 @@ describe('parser', () => {
110110
.rejects.toThrow(new Error('Expected entity but got eof on line 3.'));
111111
});
112112

113+
it('should parse application/ld+json and emit context', async (): Promise<void> => {
114+
const stream = stringToStream(`
115+
{
116+
"@context": "http://schema.org/",
117+
"@type": "Person",
118+
"@id": "",
119+
"name": "Jane Doe",
120+
"url": ""
121+
}
122+
`);
123+
const contexts: string[] = [];
124+
const result = await arrayifyStream(parser.parse(stream, { contentType: 'application/ld+json' })
125+
.on('context', (context => contexts.push(context))));
126+
127+
expect(result).toBeRdfIsomorphic([]);
128+
expect(contexts).toContain('http://schema.org/');
129+
});
130+
113131
it('should parse application/ld+json without baseIRI', () => {
114132
const stream = stringToStream(`
115133
{
@@ -124,6 +142,7 @@ describe('parser', () => {
124142
.toBeRdfIsomorphic([]);
125143
});
126144

145+
127146
it('should parse application/ld+json with baseIRI', () => {
128147
const stream = stringToStream(`
129148
{
@@ -190,4 +209,56 @@ describe('parser', () => {
190209
quad('http://ex.org/', 'http://schema.org/name', '"Title"'),
191210
]);
192211
});
212+
213+
it('should parse and emit prefixes in text/turtle', async (): Promise<void> => {
214+
const turtle = `
215+
@base <http://example.org/> .
216+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
217+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
218+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
219+
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
220+
221+
<#green-goblin>
222+
rel:enemyOf <#spiderman> ;
223+
a foaf:Person ; # in the context of the Marvel universe
224+
foaf:name "Green Goblin" .
225+
226+
<#spiderman>
227+
rel:enemyOf <#green-goblin> ;
228+
a foaf:Person ;
229+
foaf:name "Spiderman", "Человек-паук"@ru .`
230+
const stream = stringToStream(turtle);
231+
const prefixes: Record<string, string> = {};
232+
const result = await arrayifyStream(parser.parse(stream, { path: 'myfile.ttl' })
233+
.on('prefix', (prefix, iri) => prefixes[prefix] = iri.value));
234+
expect(result).toBeRdfIsomorphic([
235+
quad('http://example.org/#green-goblin', 'http://www.perceive.net/schemas/relationship/enemyOf', 'http://example.org/#spiderman'),
236+
quad('http://example.org/#green-goblin', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://xmlns.com/foaf/0.1/Person'),
237+
quad('http://example.org/#green-goblin', 'http://xmlns.com/foaf/0.1/name', '\"Green Goblin\"'),
238+
quad('http://example.org/#spiderman', 'http://www.perceive.net/schemas/relationship/enemyOf', 'http://example.org/#green-goblin'),
239+
quad('http://example.org/#spiderman', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://xmlns.com/foaf/0.1/Person'),
240+
quad('http://example.org/#spiderman', 'http://xmlns.com/foaf/0.1/name', '\"Spiderman\"'),
241+
quad('http://example.org/#spiderman', 'http://xmlns.com/foaf/0.1/name', '\"Человек-паук\"@ru'),
242+
]);
243+
244+
expect(prefixes).toMatchObject({
245+
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
246+
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
247+
foaf: 'http://xmlns.com/foaf/0.1/',
248+
rel: 'http://www.perceive.net/schemas/relationship/'
249+
});
250+
251+
252+
});
253+
254+
it('should parse text/turtle with baseIRI', () => {
255+
const stream = stringToStream(`
256+
<s> <p> <o1>, <o2>.
257+
`);
258+
return expect(arrayifyStream(parser.parse(stream, { contentType: 'text/turtle', baseIRI: 'http://ex.org/' })))
259+
.resolves.toBeRdfIsomorphic([
260+
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
261+
quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
262+
]);
263+
});
193264
});

0 commit comments

Comments
 (0)
Please sign in to comment.