n3 1.25.2 → 2.0.0-beta.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/lib/N3Parser.js CHANGED
@@ -34,10 +34,6 @@ class N3Parser {
34
34
  if (!(this._supportsNamedGraphs = !(isTurtle || isN3))) this._readPredicateOrNamedGraph = this._readPredicate;
35
35
  // Support triples in other graphs
36
36
  this._supportsQuads = !(isTurtle || isTriG || isNTriples || isN3);
37
- // Whether the log:isImpliedBy predicate is supported
38
- this._isImpliedBy = options.isImpliedBy;
39
- // Support nesting of triples
40
- this._supportsRDFStar = format === '' || /star|\*$/.test(format);
41
37
  // Disable relative IRIs in N-Triples or N-Quads mode
42
38
  if (isLineMode) this._resolveRelativeIRI = iri => {
43
39
  return null;
@@ -45,8 +41,7 @@ class N3Parser {
45
41
  this._blankNodePrefix = typeof options.blankNodePrefix !== 'string' ? '' : options.blankNodePrefix.replace(/^(?!_:)/, '_:');
46
42
  this._lexer = options.lexer || new _N3Lexer.default({
47
43
  lineMode: isLineMode,
48
- n3: isN3,
49
- isImpliedBy: this._isImpliedBy
44
+ n3: isN3
50
45
  });
51
46
  // Disable explicit quantifiers by default
52
47
  this._explicitQuantifiers = !!options.explicitQuantifiers;
@@ -144,6 +139,11 @@ class N3Parser {
144
139
  this._sparqlStyle = true;
145
140
  case '@base':
146
141
  return this._readBaseIRI;
142
+ // It could be a version declaration
143
+ case 'VERSION':
144
+ this._sparqlStyle = true;
145
+ case '@version':
146
+ return this._readVersion;
147
147
  // It could be a graph
148
148
  case '{':
149
149
  if (this._supportsNamedGraphs) {
@@ -203,6 +203,11 @@ class N3Parser {
203
203
  this._saveContext('blank', this._graph, this._subject = this._factory.blankNode(), null, null);
204
204
  return this._readBlankNodeHead;
205
205
  case '(':
206
+ const stack = this._contextStack,
207
+ parent = stack.length && stack[stack.length - 1];
208
+ if (parent.type === '<<') {
209
+ return this._error('Unexpected list in reified triple', token);
210
+ }
206
211
  // Start a new list
207
212
  this._saveContext('list', this._graph, this.RDF_NIL, null, null);
208
213
  this._subject = null;
@@ -234,8 +239,12 @@ class N3Parser {
234
239
  return this._completeSubjectLiteral;
235
240
  } else this._subject = this._factory.literal(token.value, this._factory.namedNode(token.prefix));
236
241
  break;
242
+ case '<<(':
243
+ if (!this._n3Mode) return this._error('Disallowed triple term as subject', token);
244
+ this._saveContext('<<(', this._graph, null, null, null);
245
+ this._graph = null;
246
+ return this._readSubject;
237
247
  case '<<':
238
- if (!this._supportsRDFStar) return this._error('Unexpected RDF-star syntax', token);
239
248
  this._saveContext('<<', this._graph, null, null, null);
240
249
  this._graph = null;
241
250
  return this._readSubject;
@@ -263,6 +272,7 @@ class N3Parser {
263
272
  case '.':
264
273
  case ']':
265
274
  case '}':
275
+ case '|}':
266
276
  // Expected predicate didn't come, must have been trailing semicolon
267
277
  if (this._predicate === null) return this._error(`Unexpected ${type}`, token);
268
278
  this._subject = null;
@@ -302,6 +312,11 @@ class N3Parser {
302
312
  this._saveContext('blank', this._graph, this._subject, this._predicate, this._subject = this._factory.blankNode());
303
313
  return this._readBlankNodeHead;
304
314
  case '(':
315
+ const stack = this._contextStack,
316
+ parent = stack.length && stack[stack.length - 1];
317
+ if (parent.type === '<<') {
318
+ return this._error('Unexpected list in reified triple', token);
319
+ }
305
320
  // Start a new list
306
321
  this._saveContext('list', this._graph, this._subject, this._predicate, this.RDF_NIL);
307
322
  this._subject = null;
@@ -311,8 +326,11 @@ class N3Parser {
311
326
  if (!this._n3Mode) return this._error('Unexpected graph', token);
312
327
  this._saveContext('formula', this._graph, this._subject, this._predicate, this._graph = this._factory.blankNode());
313
328
  return this._readSubject;
329
+ case '<<(':
330
+ this._saveContext('<<(', this._graph, this._subject, this._predicate, null);
331
+ this._graph = null;
332
+ return this._readSubject;
314
333
  case '<<':
315
- if (!this._supportsRDFStar) return this._error('Unexpected RDF-star syntax', token);
316
334
  this._saveContext('<<', this._graph, this._subject, this._predicate, null);
317
335
  this._graph = null;
318
336
  return this._readSubject;
@@ -344,6 +362,11 @@ class N3Parser {
344
362
  this._subject = null;
345
363
  return this._readBlankNodeTail(token);
346
364
  } else {
365
+ const stack = this._contextStack,
366
+ parentParent = stack.length > 1 && stack[stack.length - 2];
367
+ if (parentParent.type === '<<') {
368
+ return this._error('Unexpected compound blank node expression in reified triple', token);
369
+ }
347
370
  this._predicate = null;
348
371
  return this._readPredicate(token);
349
372
  }
@@ -445,6 +468,11 @@ class N3Parser {
445
468
  if (!this._n3Mode) return this._error('Unexpected graph', token);
446
469
  this._saveContext('formula', this._graph, this._subject, this._predicate, this._graph = this._factory.blankNode());
447
470
  return this._readSubject;
471
+ case '<<':
472
+ this._saveContext('<<', this._graph, null, null, null);
473
+ this._graph = null;
474
+ next = this._readSubject;
475
+ break;
448
476
  default:
449
477
  if ((item = this._readEntity(token)) === undefined) return;
450
478
  }
@@ -452,6 +480,9 @@ class N3Parser {
452
480
  // Create a new blank node if no item head was assigned yet
453
481
  if (list === null) this._subject = list = this._factory.blankNode();
454
482
 
483
+ // When reading a reified triple, store the list as subject in the stack, as this will be overridden when reading the triple.
484
+ if (token.type === '<<') stack[stack.length - 1].subject = this._subject;
485
+
455
486
  // Is this the first element of the list?
456
487
  if (previousList === null) {
457
488
  // This list is either the subject or the object of its parent
@@ -487,51 +518,82 @@ class N3Parser {
487
518
  }
488
519
 
489
520
  // ### `_completeLiteral` completes a literal with an optional datatype or language
490
- _completeLiteral(token) {
521
+ _completeLiteral(token, component) {
491
522
  // Create a simple string literal by default
492
523
  let literal = this._factory.literal(this._literalValue);
524
+ let readCb;
493
525
  switch (token.type) {
494
526
  // Create a datatyped literal
495
527
  case 'type':
496
528
  case 'typeIRI':
497
529
  const datatype = this._readEntity(token);
498
530
  if (datatype === undefined) return; // No datatype means an error occurred
531
+ if (datatype.value === _IRIs.default.rdf.langString || datatype.value === _IRIs.default.rdf.dirLangString) {
532
+ return this._error('Detected illegal (directional) languaged-tagged string with explicit datatype', token);
533
+ }
499
534
  literal = this._factory.literal(this._literalValue, datatype);
500
535
  token = null;
501
536
  break;
502
537
  // Create a language-tagged string
503
538
  case 'langcode':
539
+ if (token.value.length > 8) return this._error('Detected language tag of length larger than 8', token);
504
540
  literal = this._factory.literal(this._literalValue, token.value);
541
+ this._literalLanguage = token.value;
505
542
  token = null;
543
+ readCb = this._readDirCode.bind(this, component);
506
544
  break;
507
545
  }
508
546
  return {
509
547
  token,
510
- literal
548
+ literal,
549
+ readCb
511
550
  };
512
551
  }
552
+ _readDirCode(component, listItem, token) {
553
+ // Attempt to read a dircode
554
+ if (token.type === 'dircode') {
555
+ const term = this._factory.literal(this._literalValue, {
556
+ language: this._literalLanguage,
557
+ direction: token.value
558
+ });
559
+ if (component === 'subject') this._subject = term;else this._object = term;
560
+ this._literalLanguage = undefined;
561
+ token = null;
562
+ }
563
+ if (component === 'subject') return token === null ? this._readPredicateOrNamedGraph : this._readPredicateOrNamedGraph(token);
564
+ return this._completeObjectLiteralPost(token, listItem);
565
+ }
513
566
 
514
567
  // Completes a literal in subject position
515
568
  _completeSubjectLiteral(token) {
516
- this._subject = this._completeLiteral(token).literal;
569
+ const completed = this._completeLiteral(token, 'subject');
570
+ this._subject = completed.literal;
571
+
572
+ // Postpone completion if the literal is only partially completed (such as lang+dir).
573
+ if (completed.readCb) return completed.readCb.bind(this, false);
517
574
  return this._readPredicateOrNamedGraph;
518
575
  }
519
576
 
520
577
  // Completes a literal in object position
521
578
  _completeObjectLiteral(token, listItem) {
522
- const completed = this._completeLiteral(token);
579
+ const completed = this._completeLiteral(token, 'object');
523
580
  if (!completed) return;
524
581
  this._object = completed.literal;
525
582
 
583
+ // Postpone completion if the literal is only partially completed (such as lang+dir).
584
+ if (completed.readCb) return completed.readCb.bind(this, listItem);
585
+ return this._completeObjectLiteralPost(completed.token, listItem);
586
+ }
587
+ _completeObjectLiteralPost(token, listItem) {
526
588
  // If this literal was part of a list, write the item
527
589
  // (we could also check the context stack, but passing in a flag is faster)
528
590
  if (listItem) this._emit(this._subject, this.RDF_FIRST, this._object, this._graph);
529
591
  // If the token was consumed, continue with the rest of the input
530
- if (completed.token === null) return this._getContextEndReader();
592
+ if (token === null) return this._getContextEndReader();
531
593
  // Otherwise, consume the token now
532
594
  else {
533
595
  this._readCallback = this._getContextEndReader();
534
- return this._readCallback(completed.token);
596
+ return this._readCallback(token);
535
597
  }
536
598
  }
537
599
 
@@ -552,7 +614,8 @@ class N3Parser {
552
614
  // ### `_readPunctuation` reads punctuation between quads or quad parts
553
615
  _readPunctuation(token) {
554
616
  let next,
555
- graph = this._graph;
617
+ graph = this._graph,
618
+ startingAnnotation = false;
556
619
  const subject = this._subject,
557
620
  inversePredicate = this._inversePredicate;
558
621
  switch (token.type) {
@@ -564,6 +627,7 @@ class N3Parser {
564
627
  // A dot just ends the statement, without sharing anything with the next
565
628
  case '.':
566
629
  this._subject = null;
630
+ this._tripleTerm = null;
567
631
  next = this._contextStack.length ? this._readSubject : this._readInTopContext;
568
632
  if (inversePredicate) this._inversePredicate = false;
569
633
  break;
@@ -575,19 +639,23 @@ class N3Parser {
575
639
  case ',':
576
640
  next = this._readObject;
577
641
  break;
642
+ // ~ is allowed in the annotation syntax
643
+ case '~':
644
+ next = this._readReifierInAnnotation;
645
+ startingAnnotation = true;
646
+ break;
578
647
  // {| means that the current triple is annotated with predicate-object pairs.
579
648
  case '{|':
580
- if (!this._supportsRDFStar) return this._error('Unexpected RDF-star syntax', token);
581
- // Continue using the last triple as quoted triple subject for the predicate-object pairs.
582
- const predicate = this._predicate,
583
- object = this._object;
584
- this._subject = this._factory.quad(subject, predicate, object, this.DEFAULTGRAPH);
649
+ // Continue using the last triple as reified triple subject for the predicate-object pairs.
650
+ this._subject = this._readTripleTerm();
651
+ startingAnnotation = true;
585
652
  next = this._readPredicate;
586
653
  break;
587
- // |} means that the current quoted triple in annotation syntax is finalized.
654
+ // |} means that the current reified triple in annotation syntax is finalized.
588
655
  case '|}':
589
- if (this._subject.termType !== 'Quad') return this._error('Unexpected asserted triple closing', token);
656
+ if (!this._annotation) return this._error('Unexpected annotation syntax closing', token);
590
657
  this._subject = null;
658
+ this._annotation = false;
591
659
  next = this._readPunctuation;
592
660
  break;
593
661
  default:
@@ -599,11 +667,14 @@ class N3Parser {
599
667
  return this._error(`Expected punctuation to follow "${this._object.id}"`, token);
600
668
  }
601
669
  // A quad has been completed now, so return it
602
- if (subject !== null) {
670
+ if (subject !== null && (!startingAnnotation || startingAnnotation && !this._annotation)) {
603
671
  const predicate = this._predicate,
604
672
  object = this._object;
605
673
  if (!inversePredicate) this._emit(subject, predicate, object, graph);else this._emit(object, predicate, subject, graph);
606
674
  }
675
+ if (startingAnnotation) {
676
+ this._annotation = true;
677
+ }
607
678
  return next;
608
679
  }
609
680
 
@@ -657,6 +728,14 @@ class N3Parser {
657
728
  return this._readDeclarationPunctuation;
658
729
  }
659
730
 
731
+ // ### `_readVersion` reads version string declaration
732
+ _readVersion(token) {
733
+ if (token.type !== 'literal') return this._error('Expected literal to follow version declaration', token);
734
+ if (token.end - token.start !== token.value.length + 2) return this._error('Version declarations must use single quotes', token);
735
+ this._prefixCallback(token.value);
736
+ return this._readDeclarationPunctuation;
737
+ }
738
+
660
739
  // ### `_readNamedGraphLabel` reads the label of a named graph
661
740
  _readNamedGraphLabel(token) {
662
741
  switch (token.type) {
@@ -792,22 +871,13 @@ class N3Parser {
792
871
  return this._readPath;
793
872
  }
794
873
 
795
- // ### `_readRDFStarTailOrGraph` reads the graph of a nested RDF-star quad or the end of a nested RDF-star triple
796
- _readRDFStarTailOrGraph(token) {
797
- if (token.type !== '>>') {
798
- // An entity means this is a quad (only allowed if not already inside a graph)
799
- if (this._supportsQuads && this._graph === null && (this._graph = this._readEntity(token)) !== undefined) return this._readRDFStarTail;
800
- return this._error(`Expected >> to follow "${this._object.id}"`, token);
801
- }
802
- return this._readRDFStarTail(token);
803
- }
804
-
805
- // ### `_readRDFStarTail` reads the end of a nested RDF-star triple
806
- _readRDFStarTail(token) {
807
- if (token.type !== '>>') return this._error(`Expected >> but got ${token.type}`, token);
874
+ // ### `_readTripleTermTail` reads the end of a triple term
875
+ _readTripleTermTail(token) {
876
+ if (token.type !== ')>>') return this._error(`Expected )>> but got ${token.type}`, token);
808
877
  // Read the quad and restore the previous context
809
878
  const quad = this._factory.quad(this._subject, this._predicate, this._object, this._graph || this.DEFAULTGRAPH);
810
- this._restoreContext('<<', token);
879
+ this._restoreContext('<<(', token);
880
+
811
881
  // If the triple was the subject, continue by reading the predicate.
812
882
  if (this._subject === null) {
813
883
  this._subject = quad;
@@ -820,6 +890,77 @@ class N3Parser {
820
890
  }
821
891
  }
822
892
 
893
+ // ### `_readReifiedTripleTailOrReifier` reads a reifier or the end of a nested reified triple
894
+ _readReifiedTripleTailOrReifier(token) {
895
+ if (token.type === '~') {
896
+ return this._readReifier;
897
+ }
898
+ return this._readReifiedTripleTail(token);
899
+ }
900
+
901
+ // ### `_readReifiedTripleTail` reads the end of a nested reified triple
902
+ _readReifiedTripleTail(token) {
903
+ if (token.type !== '>>') return this._error(`Expected >> but got ${token.type}`, token);
904
+ // Read the triple term and restore the previous context
905
+ this._tripleTerm = null;
906
+ const reifier = this._readTripleTerm();
907
+ this._restoreContext('<<', token);
908
+
909
+ // // If we're in a list, continue processing that list
910
+ const stack = this._contextStack,
911
+ parent = stack.length && stack[stack.length - 1];
912
+ if (parent && parent.type === 'list') {
913
+ this._emit(this._subject, this.RDF_FIRST, reifier, this._graph);
914
+ return this._getContextEndReader();
915
+ }
916
+ // If the triple was the subject, continue by reading the predicate.
917
+ else if (this._subject === null) {
918
+ this._subject = reifier;
919
+ return this._readPredicateOrReifierTripleEnd;
920
+ }
921
+ // If the triple was the object, read context end.
922
+ else {
923
+ this._object = reifier;
924
+ return this._getContextEndReader();
925
+ }
926
+ }
927
+ _readPredicateOrReifierTripleEnd(token) {
928
+ if (token.type === '.') {
929
+ this._subject = null;
930
+ return this._readPunctuation(token);
931
+ }
932
+ return this._readPredicate(token);
933
+ }
934
+
935
+ // ### `_readReifier` reads the triple term identifier after a tilde when in a reifying triple.
936
+ _readReifier(token) {
937
+ this._reifier = this._readEntity(token);
938
+ return this._readReifiedTripleTail;
939
+ }
940
+
941
+ // ### `_readReifier` reads the optional triple term identifier after a tilde when in annotation syntax.
942
+ _readReifierInAnnotation(token) {
943
+ // If next token is a reifier, read it as such.
944
+ if (token.type === 'IRI' || token.type === 'typeIRI' || token.type === 'type' || token.type === 'prefixed' || token.type === 'blank' || token.type === 'var') {
945
+ this._reifier = this._readEntity(token);
946
+ return this._readPunctuation;
947
+ }
948
+ // Otherwise, emit and assert triple term.
949
+ this._readTripleTerm();
950
+ this._subject = null;
951
+ return this._readPunctuation(token);
952
+ }
953
+ _readTripleTerm() {
954
+ const stack = this._contextStack,
955
+ parent = stack.length && stack[stack.length - 1];
956
+ const parentGraph = parent ? parent.graph : undefined;
957
+ const reifier = this._reifier || this._factory.blankNode();
958
+ this._reifier = null;
959
+ this._tripleTerm = this._tripleTerm || this._factory.quad(this._subject, this._predicate, this._object);
960
+ this._emit(reifier, this.RDF_REIFIES, this._tripleTerm, parentGraph || this.DEFAULTGRAPH);
961
+ return reifier;
962
+ }
963
+
823
964
  // ### `_getContextEndReader` gets the next reader function at the end of a context
824
965
  _getContextEndReader() {
825
966
  const contextStack = this._contextStack;
@@ -831,8 +972,10 @@ class N3Parser {
831
972
  return this._readListItem;
832
973
  case 'formula':
833
974
  return this._readFormulaTail;
975
+ case '<<(':
976
+ return this._readTripleTermTail;
834
977
  case '<<':
835
- return this._readRDFStarTailOrGraph;
978
+ return this._readReifiedTripleTailOrReifier;
836
979
  }
837
980
  }
838
981
 
@@ -947,17 +1090,19 @@ class N3Parser {
947
1090
  // ## Public methods
948
1091
 
949
1092
  // ### `parse` parses the N3 input and emits each parsed quad through the onQuad callback.
950
- parse(input, quadCallback, prefixCallback) {
1093
+ parse(input, quadCallback, prefixCallback, versionCallback) {
951
1094
  // The second parameter accepts an object { onQuad: ..., onPrefix: ..., onComment: ...}
952
1095
  // As a second and third parameter it still accepts a separate quadCallback and prefixCallback for backward compatibility as well
953
- let onQuad, onPrefix, onComment;
954
- if (quadCallback && (quadCallback.onQuad || quadCallback.onPrefix || quadCallback.onComment)) {
1096
+ let onQuad, onPrefix, onComment, onVersion;
1097
+ if (quadCallback && (quadCallback.onQuad || quadCallback.onPrefix || quadCallback.onComment || quadCallback.onVersion)) {
955
1098
  onQuad = quadCallback.onQuad;
956
1099
  onPrefix = quadCallback.onPrefix;
957
1100
  onComment = quadCallback.onComment;
1101
+ onVersion = quadCallback.onVersion;
958
1102
  } else {
959
1103
  onQuad = quadCallback;
960
1104
  onPrefix = prefixCallback;
1105
+ onVersion = versionCallback;
961
1106
  }
962
1107
  // The read callback is the next function to be executed when a token arrives.
963
1108
  // We start reading in the top context.
@@ -966,6 +1111,7 @@ class N3Parser {
966
1111
  this._prefixes = Object.create(null);
967
1112
  this._prefixes._ = this._blankNodePrefix ? this._blankNodePrefix.substr(2) : `b${blankNodePrefix++}_`;
968
1113
  this._prefixCallback = onPrefix || noop;
1114
+ this._versionCallback = onVersion || noop;
969
1115
  this._inversePredicate = false;
970
1116
  this._quantified = Object.create(null);
971
1117
 
@@ -1017,13 +1163,13 @@ function initDataFactory(parser, factory) {
1017
1163
  parser.RDF_FIRST = factory.namedNode(_IRIs.default.rdf.first);
1018
1164
  parser.RDF_REST = factory.namedNode(_IRIs.default.rdf.rest);
1019
1165
  parser.RDF_NIL = factory.namedNode(_IRIs.default.rdf.nil);
1166
+ parser.RDF_REIFIES = factory.namedNode(_IRIs.default.rdf.reifies);
1020
1167
  parser.N3_FORALL = factory.namedNode(_IRIs.default.r.forAll);
1021
1168
  parser.N3_FORSOME = factory.namedNode(_IRIs.default.r.forSome);
1022
1169
  parser.ABBREVIATIONS = {
1023
1170
  'a': factory.namedNode(_IRIs.default.rdf.type),
1024
1171
  '=': factory.namedNode(_IRIs.default.owl.sameAs),
1025
- '>': factory.namedNode(_IRIs.default.log.implies),
1026
- '<': factory.namedNode(_IRIs.default.log.isImpliedBy)
1172
+ '>': factory.namedNode(_IRIs.default.log.implies)
1027
1173
  };
1028
1174
  parser.QUANTIFIERS_GRAPH = factory.namedNode('urn:n3:quantifiers');
1029
1175
  }
package/lib/N3Store.js CHANGED
@@ -147,7 +147,7 @@ class N3Store {
147
147
  this._graphs = Object.create(null);
148
148
 
149
149
  // Shift parameters if `quads` is not given
150
- if (!options && quads && !quads[0] && !(typeof quads.match === 'function')) options = quads, quads = null;
150
+ if (!options && quads && !quads[0]) options = quads, quads = null;
151
151
  options = options || {};
152
152
  this._factory = options.factory || _N3DataFactory.default;
153
153
  this._entityIndex = options.entityIndex || new N3EntityIndex({
@@ -159,7 +159,7 @@ class N3Store {
159
159
  this._termToNewNumericId = this._entityIndex._termToNewNumericId.bind(this._entityIndex);
160
160
 
161
161
  // Add quads if passed
162
- if (quads) this.addAll(quads);
162
+ if (quads) this.addQuads(quads);
163
163
  }
164
164
 
165
165
  // ## Public properties
@@ -579,7 +579,7 @@ class N3Store {
579
579
  // and returns `true` if it returns truthy for any of them.
580
580
  // Setting any field to `undefined` or `null` indicates a wildcard.
581
581
  some(callback, subject, predicate, object, graph) {
582
- for (const quad of this.readQuads(subject, predicate, object, graph)) if (callback(quad, this)) return true;
582
+ for (const quad of this.readQuads(subject, predicate, object, graph)) if (callback(quad)) return true;
583
583
  return false;
584
584
  }
585
585
 
@@ -1083,28 +1083,26 @@ class DatasetCoreAndReadableStream extends _readableStream.Readable {
1083
1083
  if (subject && !(subjectId = newStore._termToNumericId(subject)) || predicate && !(predicateId = newStore._termToNumericId(predicate)) || object && !(objectId = newStore._termToNumericId(object))) return newStore;
1084
1084
  const graphs = n3Store._getGraphs(graph);
1085
1085
  for (const graphKey in graphs) {
1086
- let subjects, predicates, objects, content;
1087
- if (content = graphs[graphKey]) {
1088
- if (!subjectId && predicateId) {
1089
- if (predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId])) {
1090
- subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId]);
1091
- objects = indexMatch(content.objects, [objectId, subjectId, predicateId]);
1092
- }
1093
- } else if (objectId) {
1094
- if (objects = indexMatch(content.objects, [objectId, subjectId, predicateId])) {
1095
- subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId]);
1096
- predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId]);
1097
- }
1098
- } else if (subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId])) {
1099
- predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId]);
1100
- objects = indexMatch(content.objects, [objectId, subjectId, predicateId]);
1086
+ let subjects, predicates, objects;
1087
+ if (!subjectId && predicateId) {
1088
+ if (predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId])) {
1089
+ subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId]);
1090
+ objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId]);
1091
+ }
1092
+ } else if (objectId) {
1093
+ if (objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId])) {
1094
+ subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId]);
1095
+ predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId]);
1101
1096
  }
1102
- if (subjects) newStore._graphs[graphKey] = {
1103
- subjects,
1104
- predicates,
1105
- objects
1106
- };
1097
+ } else if (subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId])) {
1098
+ predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId]);
1099
+ objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId]);
1107
1100
  }
1101
+ if (subjects) newStore._graphs[graphKey] = {
1102
+ subjects,
1103
+ predicates,
1104
+ objects
1105
+ };
1108
1106
  }
1109
1107
  newStore._size = null;
1110
1108
  }
package/lib/N3Writer.js CHANGED
@@ -7,8 +7,6 @@ exports.default = void 0;
7
7
  var _IRIs = _interopRequireDefault(require("./IRIs"));
8
8
  var _N3DataFactory = _interopRequireWildcard(require("./N3DataFactory"));
9
9
  var _N3Util = require("./N3Util");
10
- var _BaseIRI = _interopRequireDefault(require("./BaseIRI"));
11
- var _Util = require("./Util");
12
10
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
13
11
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
14
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -79,7 +77,8 @@ class N3Writer {
79
77
  this._prefixIRIs = Object.create(null);
80
78
  options.prefixes && this.addPrefixes(options.prefixes);
81
79
  if (options.baseIRI) {
82
- this._baseIri = new _BaseIRI.default(options.baseIRI);
80
+ this._baseMatcher = new RegExp(`^${escapeRegex(options.baseIRI)}${options.baseIRI.endsWith('/') ? '' : '[#?]'}`);
81
+ this._baseLength = options.baseIRI.length;
83
82
  }
84
83
  } else {
85
84
  this._lineMode = true;
@@ -157,9 +156,7 @@ class N3Writer {
157
156
  }
158
157
  let iri = entity.value;
159
158
  // Use relative IRIs if requested and possible
160
- if (this._baseIri) {
161
- iri = this._baseIri.toRelative(iri);
162
- }
159
+ if (this._baseMatcher && this._baseMatcher.test(iri)) iri = iri.substr(this._baseLength);
163
160
  // Escape special characters
164
161
  if (escape.test(iri)) iri = iri.replace(escapeAll, characterReplacer);
165
162
  // Try to represent the IRI as prefixed name
@@ -174,7 +171,8 @@ class N3Writer {
174
171
  if (escape.test(value)) value = value.replace(escapeAll, characterReplacer);
175
172
 
176
173
  // Write a language-tagged literal
177
- if (literal.language) return `"${value}"@${literal.language}`;
174
+ const direction = literal.direction ? `--${literal.direction}` : '';
175
+ if (literal.language) return `"${value}"@${literal.language}${direction}`;
178
176
 
179
177
  // Write dedicated literals per data type
180
178
  if (this._lineMode) {
@@ -286,7 +284,7 @@ class N3Writer {
286
284
  IRIlist += IRIlist ? `|${prefixIRI}` : prefixIRI;
287
285
  prefixList += (prefixList ? '|' : '') + this._prefixIRIs[prefixIRI];
288
286
  }
289
- IRIlist = (0, _Util.escapeRegex)(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
287
+ IRIlist = escapeRegex(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
290
288
  this._prefixRegex = new RegExp(`^(?:${prefixList})[^\/]*$|` + `^(${IRIlist})([_a-zA-Z0-9][\\-_a-zA-Z0-9]*)$`);
291
289
  }
292
290
  // End a prefix block with a newline
@@ -382,4 +380,7 @@ function characterReplacer(character) {
382
380
  }
383
381
  }
384
382
  return result;
383
+ }
384
+ function escapeRegex(regex) {
385
+ return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
385
386
  }
package/lib/index.js CHANGED
@@ -3,12 +3,6 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "BaseIRI", {
7
- enumerable: true,
8
- get: function () {
9
- return _BaseIRI.default;
10
- }
11
- });
12
6
  Object.defineProperty(exports, "BlankNode", {
13
7
  enumerable: true,
14
8
  get: function () {
@@ -147,7 +141,6 @@ var _N3StreamParser = _interopRequireDefault(require("./N3StreamParser"));
147
141
  var _N3StreamWriter = _interopRequireDefault(require("./N3StreamWriter"));
148
142
  var Util = _interopRequireWildcard(require("./N3Util"));
149
143
  exports.Util = Util;
150
- var _BaseIRI = _interopRequireDefault(require("./BaseIRI"));
151
144
  var _N3DataFactory = _interopRequireWildcard(require("./N3DataFactory"));
152
145
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
153
146
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
@@ -165,7 +158,6 @@ var _default = exports.default = {
165
158
  StreamWriter: _N3StreamWriter.default,
166
159
  Util,
167
160
  Reasoner: _N3Reasoner.default,
168
- BaseIRI: _BaseIRI.default,
169
161
  DataFactory: _N3DataFactory.default,
170
162
  Term: _N3DataFactory.Term,
171
163
  NamedNode: _N3DataFactory.NamedNode,