n3 1.20.2 → 1.20.4

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/N3Store.js CHANGED
@@ -15,6 +15,11 @@ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e;
15
15
  // **N3Store** objects store N3 quads by graph in memory.
16
16
 
17
17
  const ITERATOR = Symbol('iter');
18
+ function merge(target, source, depth = 4) {
19
+ if (depth === 0) return Object.assign(target, source);
20
+ for (const key in source) target[key] = merge(target[key] || Object.create(null), source[key], depth - 1);
21
+ return target;
22
+ }
18
23
 
19
24
  // ## Constructor
20
25
  class N3EntityIndex {
@@ -125,7 +130,7 @@ class N3Store {
125
130
  const index1 = index0[key0] || (index0[key0] = {});
126
131
  const index2 = index1[key1] || (index1[key1] = {});
127
132
  // Setting the key to _any_ value signals the presence of the quad
128
- const existed = (key2 in index2);
133
+ const existed = key2 in index2;
129
134
  if (!existed) index2[key2] = null;
130
135
  return !existed;
131
136
  }
@@ -746,7 +751,12 @@ class N3Store {
746
751
  * Blank Nodes will be normalized.
747
752
  */
748
753
  addAll(quads) {
749
- if (Array.isArray(quads)) this.addQuads(quads);else {
754
+ if (Array.isArray(quads)) this.addQuads(quads);else if (quads instanceof N3Store && quads._entityIndex === this._entityIndex) {
755
+ if (quads._size !== 0) {
756
+ this._graphs = merge(this._graphs, quads._graphs);
757
+ this._size = null; // Invalidate the cached size
758
+ }
759
+ } else {
750
760
  for (const quad of quads) this.add(quad);
751
761
  }
752
762
  return this;
@@ -759,6 +769,7 @@ class N3Store {
759
769
  * Blank Nodes will be normalized.
760
770
  */
761
771
  contains(other) {
772
+ if (other === this) return true;
762
773
  if (!(other instanceof N3Store) || this._entityIndex !== other._entityIndex) return other.every(quad => this.has(quad));
763
774
  const g1 = this._graphs,
764
775
  g2 = other._graphs;
@@ -797,6 +808,9 @@ class N3Store {
797
808
  * Returns a new dataset that contains all quads from the current dataset that are not included in the given dataset.
798
809
  */
799
810
  difference(other) {
811
+ if (other === this) return new N3Store({
812
+ entityIndex: this._entityIndex
813
+ });
800
814
  return this.filter(quad => !other.has(quad));
801
815
  }
802
816
 
@@ -815,7 +829,9 @@ class N3Store {
815
829
  * This method is aligned with Array.prototype.filter() in ECMAScript-262.
816
830
  */
817
831
  filter(iteratee) {
818
- const store = new N3Store();
832
+ const store = new N3Store({
833
+ entityIndex: this._entityIndex
834
+ });
819
835
  for (const quad of this) if (iteratee(quad, this)) store.add(quad);
820
836
  return store;
821
837
  }
@@ -824,6 +840,13 @@ class N3Store {
824
840
  * Returns a new dataset containing all quads from the current dataset that are also included in the given dataset.
825
841
  */
826
842
  intersection(other) {
843
+ if (other === this) {
844
+ const store = new N3Store({
845
+ entityIndex: this._entityIndex
846
+ });
847
+ store._graphs = merge(Object.create(null), this._graphs);
848
+ store._size = this._size;
849
+ }
827
850
  return this.filter(quad => other.has(quad));
828
851
  }
829
852
 
@@ -831,7 +854,9 @@ class N3Store {
831
854
  * Returns a new dataset containing all quads returned by applying `iteratee` to each quad in the current dataset.
832
855
  */
833
856
  map(iteratee) {
834
- const store = new N3Store();
857
+ const store = new N3Store({
858
+ entityIndex: this._entityIndex
859
+ });
835
860
  for (const quad of this) store.add(iteratee(quad, this));
836
861
  return store;
837
862
  }
@@ -891,8 +916,11 @@ class N3Store {
891
916
  * Returns a new `Dataset` that is a concatenation of this dataset and the quads given as an argument.
892
917
  */
893
918
  union(quads) {
894
- const store = new N3Store();
895
- store.addAll(this);
919
+ const store = new N3Store({
920
+ entityIndex: this._entityIndex
921
+ });
922
+ store._graphs = merge(Object.create(null), this._graphs);
923
+ store._size = this._size;
896
924
  store.addAll(quads);
897
925
  return store;
898
926
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "1.20.2",
3
+ "version": "1.20.4",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
@@ -36,10 +36,9 @@
36
36
  "@rdfjs/data-model": "^1",
37
37
  "arrayify-stream": "^1.0.0",
38
38
  "browserify": "^17.0.0",
39
- "colors": "^1.1.2",
40
39
  "deep-taxonomy-benchmark": "^1.2.0",
41
40
  "docco": "^0.9.1",
42
- "eslint": "^5.14.1",
41
+ "eslint": "^8.57.0",
43
42
  "eslint-plugin-import": "^2.29.1",
44
43
  "jest": "^29.7.0",
45
44
  "pre-commit": "^1.2.2",
@@ -229,7 +229,7 @@ export function termFromId(id, factory, nested) {
229
229
  termFromId(id[0], factory, true),
230
230
  termFromId(id[1], factory, true),
231
231
  termFromId(id[2], factory, true),
232
- id[3] && termFromId(id[3], factory, true)
232
+ id[3] && termFromId(id[3], factory, true),
233
233
  );
234
234
  }
235
235
 
package/src/N3Store.js CHANGED
@@ -7,6 +7,16 @@ import N3Writer from './N3Writer';
7
7
 
8
8
  const ITERATOR = Symbol('iter');
9
9
 
10
+ function merge(target, source, depth = 4) {
11
+ if (depth === 0)
12
+ return Object.assign(target, source);
13
+
14
+ for (const key in source)
15
+ target[key] = merge(target[key] || Object.create(null), source[key], depth - 1);
16
+
17
+ return target;
18
+ }
19
+
10
20
  // ## Constructor
11
21
  export class N3EntityIndex {
12
22
  constructor(options = {}) {
@@ -31,7 +41,7 @@ export class N3EntityIndex {
31
41
  this._termFromId(entities[terms[1]]),
32
42
  this._termFromId(entities[terms[2]]),
33
43
  this._termFromId(entities[terms[3]]),
34
- terms[4] && this._termFromId(entities[terms[4]])
44
+ terms[4] && this._termFromId(entities[terms[4]]),
35
45
  );
36
46
  return q;
37
47
  }
@@ -792,6 +802,12 @@ export default class N3Store {
792
802
  addAll(quads) {
793
803
  if (Array.isArray(quads))
794
804
  this.addQuads(quads);
805
+ else if (quads instanceof N3Store && quads._entityIndex === this._entityIndex) {
806
+ if (quads._size !== 0) {
807
+ this._graphs = merge(this._graphs, quads._graphs);
808
+ this._size = null; // Invalidate the cached size
809
+ }
810
+ }
795
811
  else {
796
812
  for (const quad of quads)
797
813
  this.add(quad);
@@ -799,7 +815,6 @@ export default class N3Store {
799
815
  return this;
800
816
  }
801
817
 
802
-
803
818
  /**
804
819
  * Returns `true` if the current dataset is a superset of the given dataset; in other words, returns `true` if
805
820
  * the given dataset is a subset of, i.e., is contained within, the current dataset.
@@ -807,6 +822,9 @@ export default class N3Store {
807
822
  * Blank Nodes will be normalized.
808
823
  */
809
824
  contains(other) {
825
+ if (other === this)
826
+ return true;
827
+
810
828
  if (!(other instanceof N3Store) || this._entityIndex !== other._entityIndex)
811
829
  return other.every(quad => this.has(quad));
812
830
 
@@ -848,6 +866,9 @@ export default class N3Store {
848
866
  * Returns a new dataset that contains all quads from the current dataset that are not included in the given dataset.
849
867
  */
850
868
  difference(other) {
869
+ if (other === this)
870
+ return new N3Store({ entityIndex: this._entityIndex });
871
+
851
872
  return this.filter(quad => !other.has(quad));
852
873
  }
853
874
 
@@ -866,7 +887,7 @@ export default class N3Store {
866
887
  * This method is aligned with Array.prototype.filter() in ECMAScript-262.
867
888
  */
868
889
  filter(iteratee) {
869
- const store = new N3Store();
890
+ const store = new N3Store({ entityIndex: this._entityIndex });
870
891
  for (const quad of this)
871
892
  if (iteratee(quad, this))
872
893
  store.add(quad);
@@ -877,6 +898,11 @@ export default class N3Store {
877
898
  * Returns a new dataset containing all quads from the current dataset that are also included in the given dataset.
878
899
  */
879
900
  intersection(other) {
901
+ if (other === this) {
902
+ const store = new N3Store({ entityIndex: this._entityIndex });
903
+ store._graphs = merge(Object.create(null), this._graphs);
904
+ store._size = this._size;
905
+ }
880
906
  return this.filter(quad => other.has(quad));
881
907
  }
882
908
 
@@ -884,7 +910,7 @@ export default class N3Store {
884
910
  * Returns a new dataset containing all quads returned by applying `iteratee` to each quad in the current dataset.
885
911
  */
886
912
  map(iteratee) {
887
- const store = new N3Store();
913
+ const store = new N3Store({ entityIndex: this._entityIndex });
888
914
  for (const quad of this)
889
915
  store.add(iteratee(quad, this));
890
916
  return store;
@@ -946,8 +972,10 @@ export default class N3Store {
946
972
  * Returns a new `Dataset` that is a concatenation of this dataset and the quads given as an argument.
947
973
  */
948
974
  union(quads) {
949
- const store = new N3Store();
950
- store.addAll(this);
975
+ const store = new N3Store({ entityIndex: this._entityIndex });
976
+ store._graphs = merge(Object.create(null), this._graphs);
977
+ store._size = this._size;
978
+
951
979
  store.addAll(quads);
952
980
  return store;
953
981
  }
@@ -22,7 +22,7 @@ export default class N3StreamParser extends Transform {
22
22
  // Handle quads by pushing them down the pipeline
23
23
  (error, quad) => { error && this.emit('error', error) || quad && this.push(quad); },
24
24
  // Emit prefixes through the `prefix` event
25
- (prefix, uri) => { this.emit('prefix', prefix, uri); }
25
+ (prefix, uri) => { this.emit('prefix', prefix, uri); },
26
26
  );
27
27
 
28
28
  // Implement Transform methods through parser callbacks