n3 1.21.3 → 1.22.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/browser/n3.min.js +1 -1
- package/lib/N3DataFactory.js +31 -1
- package/lib/N3Store.js +42 -0
- package/package.json +1 -1
- package/src/N3DataFactory.js +28 -0
- package/src/N3Store.js +44 -0
package/lib/N3DataFactory.js
CHANGED
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.Variable = exports.Triple = exports.Term = exports.Quad = exports.NamedNode = exports.Literal = exports.DefaultGraph = exports.BlankNode = void 0;
|
|
7
7
|
exports.escapeQuotes = escapeQuotes;
|
|
8
|
+
exports.fromQuad = fromQuad;
|
|
9
|
+
exports.fromTerm = fromTerm;
|
|
8
10
|
exports.termFromId = termFromId;
|
|
9
11
|
exports.termToId = termToId;
|
|
10
12
|
exports.unescapeQuotes = unescapeQuotes;
|
|
@@ -31,7 +33,9 @@ const DataFactory = {
|
|
|
31
33
|
literal,
|
|
32
34
|
defaultGraph,
|
|
33
35
|
quad,
|
|
34
|
-
triple: quad
|
|
36
|
+
triple: quad,
|
|
37
|
+
fromTerm,
|
|
38
|
+
fromQuad
|
|
35
39
|
};
|
|
36
40
|
var _default = exports.default = DataFactory; // ## Term constructor
|
|
37
41
|
class Term {
|
|
@@ -366,4 +370,30 @@ function defaultGraph() {
|
|
|
366
370
|
// ### Creates a quad
|
|
367
371
|
function quad(subject, predicate, object, graph) {
|
|
368
372
|
return new Quad(subject, predicate, object, graph);
|
|
373
|
+
}
|
|
374
|
+
function fromTerm(term) {
|
|
375
|
+
if (term instanceof Term) return term;
|
|
376
|
+
|
|
377
|
+
// Term instantiated with another library
|
|
378
|
+
switch (term.termType) {
|
|
379
|
+
case 'NamedNode':
|
|
380
|
+
return namedNode(term.value);
|
|
381
|
+
case 'BlankNode':
|
|
382
|
+
return blankNode(term.value);
|
|
383
|
+
case 'Variable':
|
|
384
|
+
return variable(term.value);
|
|
385
|
+
case 'DefaultGraph':
|
|
386
|
+
return DEFAULTGRAPH;
|
|
387
|
+
case 'Literal':
|
|
388
|
+
return literal(term.value, term.language || term.datatype);
|
|
389
|
+
case 'Quad':
|
|
390
|
+
return fromQuad(term);
|
|
391
|
+
default:
|
|
392
|
+
throw new Error(`Unexpected termType: ${term.termType}`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function fromQuad(inQuad) {
|
|
396
|
+
if (inQuad instanceof Quad) return inQuad;
|
|
397
|
+
if (inQuad.termType !== 'Quad') throw new Error(`Unexpected termType: ${inQuad.termType}`);
|
|
398
|
+
return quad(fromTerm(inQuad.subject), fromTerm(inQuad.predicate), fromTerm(inQuad.object), fromTerm(inQuad.graph));
|
|
369
399
|
}
|
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 = {}) {
|
|
@@ -842,6 +873,17 @@ class N3Store {
|
|
|
842
873
|
if (other === this) return new N3Store({
|
|
843
874
|
entityIndex: this._entityIndex
|
|
844
875
|
});
|
|
876
|
+
if (other instanceof N3Store && other._entityIndex === this._entityIndex) {
|
|
877
|
+
const store = new N3Store({
|
|
878
|
+
entityIndex: this._entityIndex
|
|
879
|
+
});
|
|
880
|
+
const graphs = difference(this._graphs, other._graphs);
|
|
881
|
+
if (graphs) {
|
|
882
|
+
store._graphs = graphs;
|
|
883
|
+
store._size = null;
|
|
884
|
+
}
|
|
885
|
+
return store;
|
|
886
|
+
}
|
|
845
887
|
return this.filter(quad => !other.has(quad));
|
|
846
888
|
}
|
|
847
889
|
|
package/package.json
CHANGED
package/src/N3DataFactory.js
CHANGED
|
@@ -20,6 +20,8 @@ const DataFactory = {
|
|
|
20
20
|
defaultGraph,
|
|
21
21
|
quad,
|
|
22
22
|
triple: quad,
|
|
23
|
+
fromTerm,
|
|
24
|
+
fromQuad,
|
|
23
25
|
};
|
|
24
26
|
export default DataFactory;
|
|
25
27
|
|
|
@@ -386,3 +388,29 @@ function defaultGraph() {
|
|
|
386
388
|
function quad(subject, predicate, object, graph) {
|
|
387
389
|
return new Quad(subject, predicate, object, graph);
|
|
388
390
|
}
|
|
391
|
+
|
|
392
|
+
export function fromTerm(term) {
|
|
393
|
+
if (term instanceof Term)
|
|
394
|
+
return term;
|
|
395
|
+
|
|
396
|
+
// Term instantiated with another library
|
|
397
|
+
switch (term.termType) {
|
|
398
|
+
case 'NamedNode': return namedNode(term.value);
|
|
399
|
+
case 'BlankNode': return blankNode(term.value);
|
|
400
|
+
case 'Variable': return variable(term.value);
|
|
401
|
+
case 'DefaultGraph': return DEFAULTGRAPH;
|
|
402
|
+
case 'Literal': return literal(term.value, term.language || term.datatype);
|
|
403
|
+
case 'Quad': return fromQuad(term);
|
|
404
|
+
default: throw new Error(`Unexpected termType: ${term.termType}`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export function fromQuad(inQuad) {
|
|
409
|
+
if (inQuad instanceof Quad)
|
|
410
|
+
return inQuad;
|
|
411
|
+
|
|
412
|
+
if (inQuad.termType !== 'Quad')
|
|
413
|
+
throw new Error(`Unexpected termType: ${inQuad.termType}`);
|
|
414
|
+
|
|
415
|
+
return quad(fromTerm(inQuad.subject), fromTerm(inQuad.predicate), fromTerm(inQuad.object), fromTerm(inQuad.graph));
|
|
416
|
+
}
|
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 = {}) {
|
|
@@ -896,6 +930,16 @@ export default class N3Store {
|
|
|
896
930
|
if (other === this)
|
|
897
931
|
return new N3Store({ entityIndex: this._entityIndex });
|
|
898
932
|
|
|
933
|
+
if ((other instanceof N3Store) && other._entityIndex === this._entityIndex) {
|
|
934
|
+
const store = new N3Store({ entityIndex: this._entityIndex });
|
|
935
|
+
const graphs = difference(this._graphs, other._graphs);
|
|
936
|
+
if (graphs) {
|
|
937
|
+
store._graphs = graphs;
|
|
938
|
+
store._size = null;
|
|
939
|
+
}
|
|
940
|
+
return store;
|
|
941
|
+
}
|
|
942
|
+
|
|
899
943
|
return this.filter(quad => !other.has(quad));
|
|
900
944
|
}
|
|
901
945
|
|