n3 1.22.0 → 1.22.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.
package/lib/N3Store.js CHANGED
@@ -47,6 +47,37 @@ function intersect(s1, s2, depth = 4) {
47
47
  return target;
48
48
  }
49
49
 
50
+ /**
51
+ * Determines the difference of the `_graphs` index s1 and s2.
52
+ * s1 and s2 *must* belong to Stores that share an `_entityIndex`.
53
+ *
54
+ * False is returned when there is no difference; this should
55
+ * *not* be set as the value for an index.
56
+ */
57
+ function difference(s1, s2, depth = 4) {
58
+ let target = false;
59
+ for (const key in s1) {
60
+ // When the key is not in the index, then none of the triples defined by s1[key] are
61
+ // in s2 and so we want to copy them over to the resultant store.
62
+ if (!(key in s2)) {
63
+ target = target || Object.create(null);
64
+ target[key] = depth === 0 ? null : merge({}, s1[key], depth - 1);
65
+ } else if (depth !== 0) {
66
+ const diff = difference(s1[key], s2[key], depth - 1);
67
+ if (diff !== false) {
68
+ target = target || Object.create(null);
69
+ target[key] = diff;
70
+ }
71
+ // Depth 3 is the 'subjects', 'predicates' and 'objects' keys.
72
+ // If the 'subjects' index is empty, so will the 'predicates' and 'objects' index.
73
+ else if (depth === 3) {
74
+ return false;
75
+ }
76
+ }
77
+ }
78
+ return target;
79
+ }
80
+
50
81
  // ## Constructor
51
82
  class N3EntityIndex {
52
83
  constructor(options = {}) {
@@ -782,6 +813,7 @@ class N3Store {
782
813
  * Blank Nodes will be normalized.
783
814
  */
784
815
  addAll(quads) {
816
+ if (quads instanceof DatasetCoreAndReadableStream) quads = quads.filtered;
785
817
  if (Array.isArray(quads)) this.addQuads(quads);else if (quads instanceof N3Store && quads._entityIndex === this._entityIndex) {
786
818
  if (quads._size !== 0) {
787
819
  this._graphs = merge(this._graphs, quads._graphs);
@@ -800,6 +832,7 @@ class N3Store {
800
832
  * Blank Nodes will be normalized.
801
833
  */
802
834
  contains(other) {
835
+ if (other instanceof DatasetCoreAndReadableStream) other = other.filtered;
803
836
  if (other === this) return true;
804
837
  if (!(other instanceof N3Store) || this._entityIndex !== other._entityIndex) return other.every(quad => this.has(quad));
805
838
  const g1 = this._graphs,
@@ -839,9 +872,21 @@ class N3Store {
839
872
  * Returns a new dataset that contains all quads from the current dataset that are not included in the given dataset.
840
873
  */
841
874
  difference(other) {
875
+ if (other && other instanceof DatasetCoreAndReadableStream) other = other.filtered;
842
876
  if (other === this) return new N3Store({
843
877
  entityIndex: this._entityIndex
844
878
  });
879
+ if (other instanceof N3Store && other._entityIndex === this._entityIndex) {
880
+ const store = new N3Store({
881
+ entityIndex: this._entityIndex
882
+ });
883
+ const graphs = difference(this._graphs, other._graphs);
884
+ if (graphs) {
885
+ store._graphs = graphs;
886
+ store._size = null;
887
+ }
888
+ return store;
889
+ }
845
890
  return this.filter(quad => !other.has(quad));
846
891
  }
847
892
 
@@ -851,6 +896,7 @@ class N3Store {
851
896
  * Blank Nodes will be normalized.
852
897
  */
853
898
  equals(other) {
899
+ if (other instanceof DatasetCoreAndReadableStream) other = other.filtered;
854
900
  return other === this || this.size === other.size && this.contains(other);
855
901
  }
856
902
 
@@ -871,6 +917,7 @@ class N3Store {
871
917
  * Returns a new dataset containing all quads from the current dataset that are also included in the given dataset.
872
918
  */
873
919
  intersection(other) {
920
+ if (other instanceof DatasetCoreAndReadableStream) other = other.filtered;
874
921
  if (other === this) {
875
922
  const store = new N3Store({
876
923
  entityIndex: this._entityIndex
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "1.22.0",
3
+ "version": "1.22.2",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
@@ -40,6 +40,7 @@
40
40
  "docco": "^0.9.1",
41
41
  "eslint": "^8.57.0",
42
42
  "eslint-plugin-import": "^2.29.1",
43
+ "eslint-plugin-jest": "^28.8.3",
43
44
  "jest": "^29.7.0",
44
45
  "pre-commit": "^1.2.2",
45
46
  "rdf-isomorphic": "^1.3.1",
package/src/N3Store.js CHANGED
@@ -45,6 +45,40 @@ function intersect(s1, s2, depth = 4) {
45
45
  return target;
46
46
  }
47
47
 
48
+ /**
49
+ * Determines the difference of the `_graphs` index s1 and s2.
50
+ * s1 and s2 *must* belong to Stores that share an `_entityIndex`.
51
+ *
52
+ * False is returned when there is no difference; this should
53
+ * *not* be set as the value for an index.
54
+ */
55
+ function difference(s1, s2, depth = 4) {
56
+ let target = false;
57
+
58
+ for (const key in s1) {
59
+ // When the key is not in the index, then none of the triples defined by s1[key] are
60
+ // in s2 and so we want to copy them over to the resultant store.
61
+ if (!(key in s2)) {
62
+ target = target || Object.create(null);
63
+ target[key] = depth === 0 ? null : merge({}, s1[key], depth - 1);
64
+ }
65
+ else if (depth !== 0) {
66
+ const diff = difference(s1[key], s2[key], depth - 1);
67
+ if (diff !== false) {
68
+ target = target || Object.create(null);
69
+ target[key] = diff;
70
+ }
71
+ // Depth 3 is the 'subjects', 'predicates' and 'objects' keys.
72
+ // If the 'subjects' index is empty, so will the 'predicates' and 'objects' index.
73
+ else if (depth === 3) {
74
+ return false;
75
+ }
76
+ }
77
+ }
78
+
79
+ return target;
80
+ }
81
+
48
82
  // ## Constructor
49
83
  export class N3EntityIndex {
50
84
  constructor(options = {}) {
@@ -827,6 +861,9 @@ export default class N3Store {
827
861
  * Blank Nodes will be normalized.
828
862
  */
829
863
  addAll(quads) {
864
+ if (quads instanceof DatasetCoreAndReadableStream)
865
+ quads = quads.filtered;
866
+
830
867
  if (Array.isArray(quads))
831
868
  this.addQuads(quads);
832
869
  else if (quads instanceof N3Store && quads._entityIndex === this._entityIndex) {
@@ -849,6 +886,9 @@ export default class N3Store {
849
886
  * Blank Nodes will be normalized.
850
887
  */
851
888
  contains(other) {
889
+ if (other instanceof DatasetCoreAndReadableStream)
890
+ other = other.filtered;
891
+
852
892
  if (other === this)
853
893
  return true;
854
894
 
@@ -893,9 +933,22 @@ export default class N3Store {
893
933
  * Returns a new dataset that contains all quads from the current dataset that are not included in the given dataset.
894
934
  */
895
935
  difference(other) {
936
+ if (other && other instanceof DatasetCoreAndReadableStream)
937
+ other = other.filtered;
938
+
896
939
  if (other === this)
897
940
  return new N3Store({ entityIndex: this._entityIndex });
898
941
 
942
+ if ((other instanceof N3Store) && other._entityIndex === this._entityIndex) {
943
+ const store = new N3Store({ entityIndex: this._entityIndex });
944
+ const graphs = difference(this._graphs, other._graphs);
945
+ if (graphs) {
946
+ store._graphs = graphs;
947
+ store._size = null;
948
+ }
949
+ return store;
950
+ }
951
+
899
952
  return this.filter(quad => !other.has(quad));
900
953
  }
901
954
 
@@ -905,6 +958,9 @@ export default class N3Store {
905
958
  * Blank Nodes will be normalized.
906
959
  */
907
960
  equals(other) {
961
+ if (other instanceof DatasetCoreAndReadableStream)
962
+ other = other.filtered;
963
+
908
964
  return other === this || (this.size === other.size && this.contains(other));
909
965
  }
910
966
 
@@ -925,6 +981,9 @@ export default class N3Store {
925
981
  * Returns a new dataset containing all quads from the current dataset that are also included in the given dataset.
926
982
  */
927
983
  intersection(other) {
984
+ if (other instanceof DatasetCoreAndReadableStream)
985
+ other = other.filtered;
986
+
928
987
  if (other === this) {
929
988
  const store = new N3Store({ entityIndex: this._entityIndex });
930
989
  store._graphs = merge(Object.create(null), this._graphs);