n3 2.1.2 → 2.2.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
@@ -296,6 +296,19 @@ export default class N3Store {
296
296
  }
297
297
  }
298
298
 
299
+ // ### `_loopByKey0Deep` executes the callback on all keys of index 2
300
+ // for a certain entry in index 0, possibly repeating keys
301
+ _loopByKey0Deep(index0, key0, callback) {
302
+ let index1, index2, key1, key2;
303
+ if (index1 = index0[key0]) {
304
+ for (key1 in index1) {
305
+ index2 = index1[key1];
306
+ for (key2 in index2)
307
+ callback(key2);
308
+ }
309
+ }
310
+ }
311
+
299
312
  // ### `_countInIndex` counts matching quads in a three-layered index.
300
313
  // The index base is `index0` and the keys at each level are `key0`, `key1`, and `key2`.
301
314
  // Any of these keys can be undefined, which is interpreted as a wildcard.
@@ -719,8 +732,9 @@ export default class N3Store {
719
732
  // If subject and predicate are given, the SPO index is best.
720
733
  this._loopBy2Keys(content.subjects, subjectId, predicateId, callback);
721
734
  else
722
- // If only subject is given, the OSP index is best.
723
- this._loopByKey1(content.objects, subjectId, callback);
735
+ // If only subject is given, descending the SPO index
736
+ // visits only the subject's own quads.
737
+ this._loopByKey0Deep(content.subjects, subjectId, callback);
724
738
  }
725
739
  else if (predicateId)
726
740
  // If only predicate is given, the POS index is best.
package/src/N3Writer.js CHANGED
@@ -31,6 +31,7 @@ export default class N3Writer {
31
31
  constructor(outputStream, options) {
32
32
  // ### `_prefixRegex` matches a prefixed name or IRI that begins with one of the added prefixes
33
33
  this._prefixRegex = /$0^/;
34
+ this._hasPrefixes = false;
34
35
 
35
36
  // Shift arguments if the first argument is not a stream
36
37
  if (outputStream && typeof outputStream.write !== 'function')
@@ -151,7 +152,8 @@ export default class N3Writer {
151
152
  // If it is a list head, pretty-print it
152
153
  if (this._lists && (entity.value in this._lists))
153
154
  entity = this.list(this._lists[entity.value]);
154
- return 'id' in entity ? entity.id : `_:${entity.value}`;
155
+ return entity.termType === 'Variable' ? `?${entity.value}` :
156
+ 'id' in entity ? entity.id : `_:${entity.value}`;
155
157
  }
156
158
  let iri = entity.value;
157
159
  // Use relative IRIs if requested and possible
@@ -161,8 +163,8 @@ export default class N3Writer {
161
163
  // Escape special characters
162
164
  if (escape.test(iri))
163
165
  iri = iri.replace(escapeAll, characterReplacer);
164
- // Try to represent the IRI as prefixed name
165
- const prefixMatch = this._prefixRegex.exec(iri);
166
+ // Try to represent the IRI as prefixed name, unless no prefixes were added
167
+ const prefixMatch = this._hasPrefixes ? this._prefixRegex.exec(iri) : null;
166
168
  return !prefixMatch ? `<${iri}>` :
167
169
  (!prefixMatch[1] ? iri : this._prefixIRIs[prefixMatch[1]] + prefixMatch[2]);
168
170
  }
@@ -294,6 +296,7 @@ export default class N3Writer {
294
296
  }
295
297
  // Recreate the prefix matcher
296
298
  if (hasPrefixes) {
299
+ this._hasPrefixes = true;
297
300
  let IRIlist = '', prefixList = '';
298
301
  for (const prefixIRI in this._prefixIRIs) {
299
302
  IRIlist += IRIlist ? `|${prefixIRI}` : prefixIRI;
@@ -301,7 +304,7 @@ export default class N3Writer {
301
304
  }
302
305
  IRIlist = escapeRegex(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
303
306
  this._prefixRegex = new RegExp(`^(?:${prefixList})[^\/]*$|` +
304
- `^(${IRIlist})([_a-zA-Z0-9][\\-_a-zA-Z0-9]*)$`);
307
+ `^(${IRIlist})([_a-zA-Z0-9](?:\\.?[\\-_a-zA-Z0-9])*)$`);
305
308
  }
306
309
  // End a prefix block with a newline
307
310
  this._write(hasPrefixes ? '\n' : '', done);