n3 1.11.1 → 1.12.2

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.
@@ -35,6 +35,12 @@ export class Term {
35
35
  return this.id;
36
36
  }
37
37
 
38
+ // ### Implement hashCode for Immutable.js, since we implement `equals`
39
+ // https://immutable-js.com/docs/v4.0.0/ValueObject/#hashCode()
40
+ get hashCode() {
41
+ return 0;
42
+ }
43
+
38
44
  // ### Returns whether this object represents the same term as the other
39
45
  equals(other) {
40
46
  // If both terms were created by this library,
package/src/N3Parser.js CHANGED
@@ -272,6 +272,13 @@ export default class N3Parser {
272
272
  // Additional semicolons can be safely ignored
273
273
  return this._predicate !== null ? this._readPredicate :
274
274
  this._error('Expected predicate but got ;', token);
275
+ case '[':
276
+ if (this._n3Mode) {
277
+ // Start a new quad with a new blank node as subject
278
+ this._saveContext('blank', this._graph, this._subject,
279
+ this._subject = this._blankNode(), null);
280
+ return this._readBlankNodeHead;
281
+ }
275
282
  case 'blank':
276
283
  if (!this._n3Mode)
277
284
  return this._error('Disallowed blank node as predicate', token);
@@ -368,13 +375,16 @@ export default class N3Parser {
368
375
  // Restore the parent context containing this blank node
369
376
  const empty = this._predicate === null;
370
377
  this._restoreContext();
378
+ // If the blank node was the object, restore previous context and read punctuation
379
+ if (this._object !== null)
380
+ return this._getContextEndReader();
381
+ // If the blank node was the predicate, continue reading the object
382
+ else if (this._predicate !== null)
383
+ return this._readObject;
371
384
  // If the blank node was the subject, continue reading the predicate
372
- if (this._object === null)
385
+ else
373
386
  // If the blank node was empty, it could be a named graph label
374
387
  return empty ? this._readPredicateOrNamedGraph : this._readPredicateAfterBlank;
375
- // If the blank node was the object, restore previous context and read punctuation
376
- else
377
- return this._getContextEndReader();
378
388
  }
379
389
 
380
390
  // ### `_readPredicateAfterBlank` reads a predicate after an anonymous blank node
package/src/N3Writer.js CHANGED
@@ -55,9 +55,13 @@ export default class N3Writer {
55
55
  if (!(/triple|quad/i).test(options.format)) {
56
56
  this._lineMode = false;
57
57
  this._graph = DEFAULTGRAPH;
58
- this._baseIRI = options.baseIRI;
59
58
  this._prefixIRIs = Object.create(null);
60
59
  options.prefixes && this.addPrefixes(options.prefixes);
60
+ if (options.baseIRI) {
61
+ this._baseMatcher = new RegExp(`^${escapeRegex(options.baseIRI)
62
+ }${options.baseIRI.endsWith('/') ? '' : '[#?]'}`);
63
+ this._baseLength = options.baseIRI.length;
64
+ }
61
65
  }
62
66
  else {
63
67
  this._lineMode = true;
@@ -148,8 +152,8 @@ export default class N3Writer {
148
152
  }
149
153
  let iri = entity.value;
150
154
  // Use relative IRIs if requested and possible
151
- if (this._baseIRI && iri.startsWith(this._baseIRI))
152
- iri = iri.substr(this._baseIRI.length);
155
+ if (this._baseMatcher && this._baseMatcher.test(iri))
156
+ iri = iri.substr(this._baseLength);
153
157
  // Escape special characters
154
158
  if (escape.test(iri))
155
159
  iri = iri.replace(escapeAll, characterReplacer);
@@ -290,7 +294,7 @@ export default class N3Writer {
290
294
  IRIlist += IRIlist ? `|${prefixIRI}` : prefixIRI;
291
295
  prefixList += (prefixList ? '|' : '') + this._prefixIRIs[prefixIRI];
292
296
  }
293
- IRIlist = IRIlist.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
297
+ IRIlist = escapeRegex(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
294
298
  this._prefixRegex = new RegExp(`^(?:${prefixList})[^\/]*$|` +
295
299
  `^(${IRIlist})([a-zA-Z][\\-_a-zA-Z0-9]*)$`);
296
300
  }
@@ -389,3 +393,7 @@ function characterReplacer(character) {
389
393
  }
390
394
  return result;
391
395
  }
396
+
397
+ function escapeRegex(regex) {
398
+ return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
399
+ }