Skip to content

Commit a49385f

Browse files
committed
chore: refactor for review #319 (comment)
1 parent a751dc2 commit a49385f

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ const writer2 = new N3.Writer({ format: 'application/trig' });
201201
```
202202
To exclude the graph name from the serialization, pass a `graphs` argument upon creation:
203203
```JavaScript
204-
const writer3 = new N3.Writer({ graphs: 'keep' }); // Keeps the graph names (default)
205-
const writer4 = new N3.Writer({ graphs: 'ignore' }); // Ignores the graph name
204+
const writer3 = new N3.Writer({ graphs: 'keep' }); // Keeps the graph names (default)
205+
const writer4 = new N3.Writer({ graphs: 'drop' }); // Drops the graph name
206206
const writer5 = new N3.Writer({ graphs: 'error' }); // Throws an error when writing a graph name
207207
```
208208

src/N3Writer.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export default class N3Writer {
3535
options = outputStream, outputStream = null;
3636
options = options || {};
3737
this._lists = options.lists;
38-
this._graphs = options.graphs || 'keep';
38+
this._keepGraphs = !options.graphs || options.graphs === 'keep';
39+
this._errorOnGraphs = options.graphs === 'error';
40+
3941

4042
// If no output stream given, send the output as string through the end callback
4143
if (!outputStream) {
@@ -85,11 +87,11 @@ export default class N3Writer {
8587
// ### `_writeQuad` writes the quad to the output stream
8688
_writeQuad(subject, predicate, object, graph, done) {
8789
try {
88-
if (this._graphs === 'error' && !DEFAULTGRAPH.equals(graph)) {
89-
done(new Error('Encountered graph name, this is forbidden.'));
90+
if (this._errorOnGraphs && !DEFAULTGRAPH.equals(graph)) {
91+
done(new Error('The chosen serialization settings do not support triples in a non-default graph.'));
9092
}
9193
// Write the graph's label if it has changed
92-
if (this._graphs === 'keep' && !graph.equals(this._graph)) {
94+
if (this._keepGraphs && !graph.equals(this._graph)) {
9395
// Close the previous graph and start the new one
9496
this._write((this._subject === null ? '' : (this._inDefaultGraph ? '.\n' : '\n}\n')) +
9597
(DEFAULTGRAPH.equals(graph) ? '' : `${this._encodeIriOrBlank(graph)} {\n`));
@@ -126,15 +128,15 @@ export default class N3Writer {
126128

127129
// ### `quadToString` serializes a quad as a string
128130
quadToString(subject, predicate, object, graph, done) {
129-
if (this._graphs === 'error' && !DEFAULTGRAPH.equals(graph)) {
130-
const err = new Error('Encountered graph name, this is forbidden.');
131-
if (done) return done(err);
132-
throw err;
131+
if (this._errorOnGraphs && !DEFAULTGRAPH.equals(graph)) {
132+
const error = new Error('The chosen serialization settings do not support triples in a non-default graph.');
133+
if (done) return done(error);
134+
throw error;
133135
}
134136
return `${this._encodeSubject(subject)} ${
135137
this._encodeIriOrBlank(predicate)} ${
136138
this._encodeObject(object)
137-
}${this._graphs === 'keep' && graph && graph.value ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
139+
}${this._keepGraphs && graph && graph.value ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
138140
}
139141

140142
// ### `quadsToString` serializes an array of quads as a string

test/N3Writer-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Writer', () => {
3838
}
3939
catch (error) {
4040
error.should.be.an.instanceof(Error);
41-
error.should.have.property('message', 'Encountered graph name, this is forbidden.');
41+
error.should.have.property('message', 'The chosen serialization settings do not support triples in a non-default graph.');
4242
done();
4343
}
4444
});
@@ -688,7 +688,7 @@ describe('Writer', () => {
688688
new NamedNode('c:c'),
689689
new NamedNode('g:g')), error => {
690690
error.should.be.an.instanceof(Error);
691-
error.should.have.property('message', 'Encountered graph name, this is forbidden.');
691+
error.should.have.property('message', 'The chosen serialization settings do not support triples in a non-default graph.');
692692
done();
693693
});
694694
});
@@ -713,7 +713,7 @@ describe('Writer', () => {
713713
new NamedNode('c:c'),
714714
new NamedNode('g:g')), error => {
715715
error.should.be.an.instanceof(Error);
716-
error.should.have.property('message', 'Encountered graph name, this is forbidden.');
716+
error.should.have.property('message', 'The chosen serialization settings do not support triples in a non-default graph.');
717717
done();
718718
});
719719
});

0 commit comments

Comments
 (0)