n3 2.0.0-beta.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/N3Store.js CHANGED
@@ -163,7 +163,7 @@ export default class N3Store {
163
163
  this._graphs = Object.create(null);
164
164
 
165
165
  // Shift parameters if `quads` is not given
166
- if (!options && quads && !quads[0])
166
+ if (!options && quads && !quads[0] && !(typeof quads.match === 'function'))
167
167
  options = quads, quads = null;
168
168
  options = options || {};
169
169
  this._factory = options.factory || N3DataFactory;
@@ -175,7 +175,7 @@ export default class N3Store {
175
175
 
176
176
  // Add quads if passed
177
177
  if (quads)
178
- this.addQuads(quads);
178
+ this.addAll(quads);
179
179
  }
180
180
 
181
181
  // ## Public properties
@@ -601,7 +601,7 @@ export default class N3Store {
601
601
  // Setting any field to `undefined` or `null` indicates a wildcard.
602
602
  some(callback, subject, predicate, object, graph) {
603
603
  for (const quad of this.readQuads(subject, predicate, object, graph))
604
- if (callback(quad))
604
+ if (callback(quad, this))
605
605
  return true;
606
606
  return false;
607
607
  }
@@ -1134,27 +1134,28 @@ class DatasetCoreAndReadableStream extends Readable {
1134
1134
 
1135
1135
  const graphs = n3Store._getGraphs(graph);
1136
1136
  for (const graphKey in graphs) {
1137
- let subjects, predicates, objects;
1138
-
1139
- if (!subjectId && predicateId) {
1140
- if (predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId])) {
1141
- subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId]);
1142
- objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId]);
1137
+ let subjects, predicates, objects, content;
1138
+ if (content = graphs[graphKey]) {
1139
+ if (!subjectId && predicateId) {
1140
+ if (predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId])) {
1141
+ subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId]);
1142
+ objects = indexMatch(content.objects, [objectId, subjectId, predicateId]);
1143
+ }
1143
1144
  }
1144
- }
1145
- else if (objectId) {
1146
- if (objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId])) {
1147
- subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId]);
1148
- predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId]);
1145
+ else if (objectId) {
1146
+ if (objects = indexMatch(content.objects, [objectId, subjectId, predicateId])) {
1147
+ subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId]);
1148
+ predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId]);
1149
+ }
1150
+ }
1151
+ else if (subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId])) {
1152
+ predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId]);
1153
+ objects = indexMatch(content.objects, [objectId, subjectId, predicateId]);
1149
1154
  }
1150
- }
1151
- else if (subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId])) {
1152
- predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId]);
1153
- objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId]);
1154
- }
1155
1155
 
1156
- if (subjects)
1157
- newStore._graphs[graphKey] = { subjects, predicates, objects };
1156
+ if (subjects)
1157
+ newStore._graphs[graphKey] = { subjects, predicates, objects };
1158
+ }
1158
1159
  }
1159
1160
  newStore._size = null;
1160
1161
  }
package/src/N3Util.js CHANGED
@@ -22,6 +22,11 @@ export function isVariable(term) {
22
22
  return !!term && term.termType === 'Variable';
23
23
  }
24
24
 
25
+ // Tests whether the given term represents a quad
26
+ export function isQuad(term) {
27
+ return !!term && term.termType === 'Quad';
28
+ }
29
+
25
30
  // Tests whether the given term represents the default graph
26
31
  export function isDefaultGraph(term) {
27
32
  return !!term && term.termType === 'DefaultGraph';
package/src/N3Writer.js CHANGED
@@ -2,6 +2,8 @@
2
2
  import namespaces from './IRIs';
3
3
  import { default as N3DataFactory, Term } from './N3DataFactory';
4
4
  import { isDefaultGraph } from './N3Util';
5
+ import BaseIRI from './BaseIRI';
6
+ import { escapeRegex } from './Util';
5
7
 
6
8
  const DEFAULTGRAPH = N3DataFactory.defaultGraph();
7
9
 
@@ -58,9 +60,7 @@ export default class N3Writer {
58
60
  this._prefixIRIs = Object.create(null);
59
61
  options.prefixes && this.addPrefixes(options.prefixes);
60
62
  if (options.baseIRI) {
61
- this._baseMatcher = new RegExp(`^${escapeRegex(options.baseIRI)
62
- }${options.baseIRI.endsWith('/') ? '' : '[#?]'}`);
63
- this._baseLength = options.baseIRI.length;
63
+ this._baseIri = new BaseIRI(options.baseIRI);
64
64
  }
65
65
  }
66
66
  else {
@@ -153,8 +153,9 @@ export default class N3Writer {
153
153
  }
154
154
  let iri = entity.value;
155
155
  // Use relative IRIs if requested and possible
156
- if (this._baseMatcher && this._baseMatcher.test(iri))
157
- iri = iri.substr(this._baseLength);
156
+ if (this._baseIri) {
157
+ iri = this._baseIri.toRelative(iri);
158
+ }
158
159
  // Escape special characters
159
160
  if (escape.test(iri))
160
161
  iri = iri.replace(escapeAll, characterReplacer);
@@ -229,11 +230,11 @@ export default class N3Writer {
229
230
 
230
231
  // ### `_encodeQuad` encodes an RDF-star quad
231
232
  _encodeQuad({ subject, predicate, object, graph }) {
232
- return `<<${
233
+ return `<<(${
233
234
  this._encodeSubject(subject)} ${
234
235
  this._encodePredicate(predicate)} ${
235
236
  this._encodeObject(object)}${
236
- isDefaultGraph(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`}>>`;
237
+ isDefaultGraph(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`})>>`;
237
238
  }
238
239
 
239
240
  // ### `_blockedWrite` replaces `_write` after the writer has been closed
@@ -395,7 +396,3 @@ function characterReplacer(character) {
395
396
  }
396
397
  return result;
397
398
  }
398
-
399
- function escapeRegex(regex) {
400
- return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
401
- }
package/src/Util.js ADDED
@@ -0,0 +1,3 @@
1
+ export function escapeRegex(regex) {
2
+ return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
3
+ }
package/src/index.js CHANGED
@@ -7,6 +7,7 @@ import Reasoner, { getRulesFromDataset } from './N3Reasoner';
7
7
  import StreamParser from './N3StreamParser';
8
8
  import StreamWriter from './N3StreamWriter';
9
9
  import * as Util from './N3Util';
10
+ import BaseIRI from './BaseIRI';
10
11
 
11
12
  import {
12
13
  default as DataFactory,
@@ -36,6 +37,7 @@ export {
36
37
  StreamWriter,
37
38
  Util,
38
39
  Reasoner,
40
+ BaseIRI,
39
41
 
40
42
  DataFactory,
41
43
 
@@ -65,6 +67,7 @@ export default {
65
67
  StreamWriter,
66
68
  Util,
67
69
  Reasoner,
70
+ BaseIRI,
68
71
 
69
72
  DataFactory,
70
73