@rdfjs/parser-n3
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -0,27 +1,25 @@ | ||
const N3 = require('n3') | ||
const rdf = require('@rdfjs/data-model') | ||
const Readable = require('readable-stream') | ||
const N3 = require('n3') | ||
const { finished } = require('readable-stream') | ||
const ReadableToReadable = require('readable-to-readable') | ||
class ParserStream extends Readable { | ||
constructor (input, {baseIRI = '', factory = rdf} = {}) { | ||
super({ | ||
objectMode: true, | ||
read: () => {} | ||
}) | ||
class ParserStream extends ReadableToReadable { | ||
constructor (input, { baseIRI = '', factory = rdf } = {}) { | ||
const parser = new N3.StreamParser({ baseIRI, factory }) | ||
const parser = new N3.Parser({baseIRI, factory}) | ||
super(parser, { objectMode: true }) | ||
parser.parse(input, (err, quad, prefixes) => { | ||
if (err) { | ||
return this.emit('error', err) | ||
} | ||
parser.on('prefix', (prefix, term) => { | ||
this.emit('prefix', prefix, term) | ||
}) | ||
if (prefixes) { | ||
Object.keys(prefixes).forEach((prefix) => { | ||
this.emit('prefix', prefix, factory.namedNode(prefixes[prefix])) | ||
}) | ||
} | ||
parser.on('error', err => { | ||
this.destroy(err) | ||
}) | ||
this.push(quad || null) | ||
finished(this, () => { | ||
parser.end() | ||
}) | ||
input.pipe(parser) | ||
} | ||
@@ -28,0 +26,0 @@ } |
{ | ||
"name": "@rdfjs/parser-n3", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "N3 parser that implements the RDFJS Sink interface", | ||
@@ -30,13 +30,12 @@ "main": "index.js", | ||
"@rdfjs/sink": "^1.0.2", | ||
"n3": "^1.0.0-beta.1", | ||
"readable-stream": "^2.3.6" | ||
"n3": "^1.3.5", | ||
"readable-stream": "^3.6.0", | ||
"readable-to-readable": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^5.2.0", | ||
"standard": "^11.0.1", | ||
"string-to-stream": "^1.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
"get-stream": "^5.1.0", | ||
"mocha": "^7.1.1", | ||
"standard": "^14.3.3", | ||
"string-to-stream": "^3.0.1" | ||
} | ||
} |
# @rdfjs/parser-n3 | ||
[![Build Status](https://travis-ci.org/rdfjs/parser-n3.svg?branch=master)](https://travis-ci.org/rdfjs/parser-n3) | ||
[![Build Status](https://travis-ci.org/rdfjs-base/parser-n3.svg?branch=master)](https://travis-ci.org/rdfjs-base/parser-n3) | ||
@@ -5,0 +5,0 @@ [![npm version](https://img.shields.io/npm/v/@rdfjs/parser-n3.svg)](https://www.npmjs.com/package/@rdfjs/parser-n3) |
136
test/test.js
@@ -1,4 +0,4 @@ | ||
/* global describe, it */ | ||
const assert = require('assert') | ||
const { strictEqual } = require('assert') | ||
const getStream = require('get-stream') | ||
const { describe, it } = require('mocha') | ||
const sinkTest = require('@rdfjs/sink/test') | ||
@@ -8,100 +8,94 @@ const stringToStream = require('string-to-stream') | ||
function waitFor (stream) { | ||
return new Promise((resolve, reject) => { | ||
stream.on('end', resolve) | ||
stream.on('error', reject) | ||
}) | ||
} | ||
describe('@rdfjs/parser-n3', () => { | ||
sinkTest(N3Parser, {readable: true}) | ||
sinkTest(N3Parser, { readable: true }) | ||
it('.import should parse the given string triple stream', () => { | ||
const nt = '<http://example.org/subject> <http://example.org/predicate> "object" .' | ||
const parser = new N3Parser() | ||
describe('.import', () => { | ||
it('should parse the given string triple stream', async () => { | ||
const nt = '<http://example.org/subject> <http://example.org/predicate> "object" .' | ||
const parser = new N3Parser() | ||
const stream = parser.import(stringToStream(nt)) | ||
const stream = parser.import(stringToStream(nt)) | ||
return Promise.resolve().then(() => { | ||
const quad = stream.read() | ||
const results = await getStream.array(stream) | ||
const quad = results[0] | ||
assert.equal(quad.subject.termType, 'NamedNode') | ||
assert.equal(quad.subject.value, 'http://example.org/subject') | ||
strictEqual(quad.subject.termType, 'NamedNode') | ||
strictEqual(quad.subject.value, 'http://example.org/subject') | ||
assert.equal(quad.predicate.termType, 'NamedNode') | ||
assert.equal(quad.predicate.value, 'http://example.org/predicate') | ||
strictEqual(quad.predicate.termType, 'NamedNode') | ||
strictEqual(quad.predicate.value, 'http://example.org/predicate') | ||
assert.equal(quad.object.termType, 'Literal') | ||
assert.equal(quad.object.value, 'object') | ||
strictEqual(quad.object.termType, 'Literal') | ||
strictEqual(quad.object.value, 'object') | ||
assert.equal(quad.graph.termType, 'DefaultGraph') | ||
return waitFor(stream) | ||
strictEqual(quad.graph.termType, 'DefaultGraph') | ||
}) | ||
}) | ||
it('.import should parse the given string quad stream', () => { | ||
const nt = '<http://example.org/subject> <http://example.org/predicate> "object" <http://example.org/graph> .' | ||
const parser = new N3Parser() | ||
it('should parse the given string quad stream', async () => { | ||
const nt = '<http://example.org/subject> <http://example.org/predicate> "object" <http://example.org/graph> .' | ||
const parser = new N3Parser() | ||
const stream = parser.import(stringToStream(nt)) | ||
const stream = parser.import(stringToStream(nt)) | ||
return Promise.resolve().then(() => { | ||
const quad = stream.read() | ||
const results = await getStream.array(stream) | ||
const quad = results[0] | ||
assert.equal(quad.subject.termType, 'NamedNode') | ||
assert.equal(quad.subject.value, 'http://example.org/subject') | ||
strictEqual(quad.subject.termType, 'NamedNode') | ||
strictEqual(quad.subject.value, 'http://example.org/subject') | ||
assert.equal(quad.predicate.termType, 'NamedNode') | ||
assert.equal(quad.predicate.value, 'http://example.org/predicate') | ||
strictEqual(quad.predicate.termType, 'NamedNode') | ||
strictEqual(quad.predicate.value, 'http://example.org/predicate') | ||
assert.equal(quad.object.termType, 'Literal') | ||
assert.equal(quad.object.value, 'object') | ||
strictEqual(quad.object.termType, 'Literal') | ||
strictEqual(quad.object.value, 'object') | ||
assert.equal(quad.graph.termType, 'NamedNode') | ||
assert.equal(quad.graph.value, 'http://example.org/graph') | ||
return waitFor(stream) | ||
strictEqual(quad.graph.termType, 'NamedNode') | ||
strictEqual(quad.graph.value, 'http://example.org/graph') | ||
}) | ||
}) | ||
it('.import should emit prefix events for each prefix', () => { | ||
const prefix1 = 'http://example.org/prefix1#' | ||
const prefix2 = 'http://example.org/prefix2#' | ||
it('should emit prefix events for each prefix', async () => { | ||
const prefix1 = 'http://example.org/prefix1#' | ||
const prefix2 = 'http://example.org/prefix2#' | ||
const nt = `PREFIX p1: <${prefix1}> | ||
PREFIX p2: <${prefix2}> | ||
<http://example.org/subject> <http://example.org/predicate> "object" .` | ||
const parser = new N3Parser() | ||
const prefixes = {} | ||
const nt = `PREFIX p1: <${prefix1}> | ||
PREFIX p2: <${prefix2}> | ||
<http://example.org/subject> <http://example.org/predicate> "object" .` | ||
const parser = new N3Parser() | ||
const prefixes = {} | ||
const stream = parser.import(stringToStream(nt)) | ||
const stream = parser.import(stringToStream(nt)) | ||
stream.on('prefix', (prefix, namespace) => { | ||
prefixes[prefix] = namespace | ||
}) | ||
stream.on('prefix', (prefix, namespace) => { | ||
prefixes[prefix] = namespace | ||
}) | ||
stream.resume() | ||
await getStream.array(stream) | ||
return waitFor(stream).then(() => { | ||
assert(prefixes.p1) | ||
assert.equal(prefixes.p1.termType, 'NamedNode') | ||
assert.equal(prefixes.p1.value, prefix1) | ||
strictEqual(typeof prefixes.p1, 'object') | ||
strictEqual(prefixes.p1.termType, 'NamedNode') | ||
strictEqual(prefixes.p1.value, prefix1) | ||
assert(prefixes.p2) | ||
assert.equal(prefixes.p2.termType, 'NamedNode') | ||
assert.equal(prefixes.p2.value, prefix2) | ||
strictEqual(typeof prefixes.p2, 'object') | ||
strictEqual(prefixes.p2.termType, 'NamedNode') | ||
strictEqual(prefixes.p2.value, prefix2) | ||
}) | ||
}) | ||
it('.import should handle parser errors', () => { | ||
const nt = '1' | ||
const parser = new N3Parser() | ||
it('should handle parser errors', async () => { | ||
const nt = '1' | ||
const parser = new N3Parser() | ||
const stream = parser.import(stringToStream(nt)) | ||
const stream = parser.import(stringToStream(nt)) | ||
return new Promise((resolve, reject) => { | ||
return waitFor(stream).catch(resolve).then(reject) | ||
let error = null | ||
try { | ||
await getStream.array(stream) | ||
} catch (err) { | ||
error = err | ||
} | ||
strictEqual(typeof error, 'object') | ||
strictEqual(error.message.includes('literal'), true) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7324
5
4
102
+ Addedreadable-to-readable@^0.1.0
+ Addedreadable-stream@3.6.2(transitive)
+ Addedreadable-to-readable@0.1.3(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
Updatedn3@^1.3.5
Updatedreadable-stream@^3.6.0