n3 1.14.0 → 1.16.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/src/N3Store.js CHANGED
@@ -84,15 +84,13 @@ export default class N3Store {
84
84
  // `name0`, `name1`, and `name2` are the names of the keys at each level,
85
85
  // used when reconstructing the resulting quad
86
86
  // (for instance: _subject_, _predicate_, and _object_).
87
- // Finally, `graph` will be the graph of the created quads.
88
- // If `callback` is given, each result is passed through it
89
- // and iteration halts when it returns truthy for any quad.
90
- // If instead `array` is given, each result is added to the array.
91
- _findInIndex(index0, key0, key1, key2, name0, name1, name2, graph, callback, array) {
87
+ // Finally, `graphId` will be the graph of the created quads.
88
+ *_findInIndex(index0, key0, key1, key2, name0, name1, name2, graphId) {
92
89
  let tmp, index1, index2;
93
90
  // Depending on the number of variables, keys or reverse index are faster
94
91
  const varCount = !key0 + !key1 + !key2,
95
92
  entityKeys = varCount > 1 ? Object.keys(this._ids) : this._entities;
93
+ const graph = termFromId(graphId, this._factory);
96
94
 
97
95
  // If a key is specified, use only that part of index 0.
98
96
  if (key0) (tmp = index0, index0 = {})[key0] = tmp[key0];
@@ -114,18 +112,12 @@ export default class N3Store {
114
112
  parts[name0] = termFromId(entity0, this._factory);
115
113
  parts[name1] = termFromId(entity1, this._factory);
116
114
  parts[name2] = termFromId(entityKeys[values[l]], this._factory);
117
- const quad = this._factory.quad(
118
- parts.subject, parts.predicate, parts.object, termFromId(graph, this._factory));
119
- if (array)
120
- array.push(quad);
121
- else if (callback(quad))
122
- return true;
115
+ yield this._factory.quad(parts.subject, parts.predicate, parts.object, graph);
123
116
  }
124
117
  }
125
118
  }
126
119
  }
127
120
  }
128
- return array;
129
121
  }
130
122
 
131
123
  // ### `_loop` executes the callback on all keys of index 0
@@ -274,11 +266,11 @@ export default class N3Store {
274
266
  return this;
275
267
  }
276
268
 
277
- // ### `has` determines whether a dataset includes a certain quad.
278
- // Returns true or false as appropriate.
279
- has(quad) {
280
- const quads = this.getQuads(quad.subject, quad.predicate, quad.object, quad.graph);
281
- return quads.length !== 0;
269
+ // ### `has` determines whether a dataset includes a certain quad or quad pattern.
270
+ has(subjectOrQuad, predicate, object, graph) {
271
+ if (subjectOrQuad && subjectOrQuad.subject)
272
+ ({ subject: subjectOrQuad, predicate, object, graph } = subjectOrQuad);
273
+ return !this.readQuads(subjectOrQuad, predicate, object, graph).next().done;
282
274
  }
283
275
 
284
276
  // ### `import` adds a stream of quads to the store
@@ -341,7 +333,7 @@ export default class N3Store {
341
333
  const stream = new Readable({ objectMode: true });
342
334
 
343
335
  stream._read = () => {
344
- for (const quad of this.getQuads(subject, predicate, object, graph))
336
+ for (const quad of this.readQuads(subject, predicate, object, graph))
345
337
  stream.push(quad);
346
338
  stream.push(null);
347
339
  };
@@ -357,20 +349,26 @@ export default class N3Store {
357
349
  // ### `getQuads` returns an array of quads matching a pattern.
358
350
  // Setting any field to `undefined` or `null` indicates a wildcard.
359
351
  getQuads(subject, predicate, object, graph) {
352
+ return [...this.readQuads(subject, predicate, object, graph)];
353
+ }
354
+
355
+ // ### `readQuads` returns an generator of quads matching a pattern.
356
+ // Setting any field to `undefined` or `null` indicates a wildcard.
357
+ *readQuads(subject, predicate, object, graph) {
360
358
  // Convert terms to internal string representation
361
359
  subject = subject && termToId(subject);
362
360
  predicate = predicate && termToId(predicate);
363
361
  object = object && termToId(object);
364
362
  graph = graph && termToId(graph);
365
363
 
366
- const quads = [], graphs = this._getGraphs(graph), ids = this._ids;
364
+ const graphs = this._getGraphs(graph), ids = this._ids;
367
365
  let content, subjectId, predicateId, objectId;
368
366
 
369
367
  // Translate IRIs to internal index keys.
370
368
  if (isString(subject) && !(subjectId = ids[subject]) ||
371
369
  isString(predicate) && !(predicateId = ids[predicate]) ||
372
370
  isString(object) && !(objectId = ids[object]))
373
- return quads;
371
+ return;
374
372
 
375
373
  for (const graphId in graphs) {
376
374
  // Only if the specified graph contains triples, there can be results
@@ -379,28 +377,27 @@ export default class N3Store {
379
377
  if (subjectId) {
380
378
  if (objectId)
381
379
  // If subject and object are given, the object index will be the fastest
382
- this._findInIndex(content.objects, objectId, subjectId, predicateId,
383
- 'object', 'subject', 'predicate', graphId, null, quads);
380
+ yield* this._findInIndex(content.objects, objectId, subjectId, predicateId,
381
+ 'object', 'subject', 'predicate', graphId, null, true);
384
382
  else
385
383
  // If only subject and possibly predicate are given, the subject index will be the fastest
386
- this._findInIndex(content.subjects, subjectId, predicateId, null,
387
- 'subject', 'predicate', 'object', graphId, null, quads);
384
+ yield* this._findInIndex(content.subjects, subjectId, predicateId, null,
385
+ 'subject', 'predicate', 'object', graphId, null, true);
388
386
  }
389
387
  else if (predicateId)
390
388
  // If only predicate and possibly object are given, the predicate index will be the fastest
391
- this._findInIndex(content.predicates, predicateId, objectId, null,
392
- 'predicate', 'object', 'subject', graphId, null, quads);
389
+ yield* this._findInIndex(content.predicates, predicateId, objectId, null,
390
+ 'predicate', 'object', 'subject', graphId, null, true);
393
391
  else if (objectId)
394
392
  // If only object is given, the object index will be the fastest
395
- this._findInIndex(content.objects, objectId, null, null,
396
- 'object', 'subject', 'predicate', graphId, null, quads);
393
+ yield* this._findInIndex(content.objects, objectId, null, null,
394
+ 'object', 'subject', 'predicate', graphId, null, true);
397
395
  else
398
396
  // If nothing is given, iterate subjects and predicates first
399
- this._findInIndex(content.subjects, null, null, null,
400
- 'subject', 'predicate', 'object', graphId, null, quads);
397
+ yield* this._findInIndex(content.subjects, null, null, null,
398
+ 'subject', 'predicate', 'object', graphId, null, true);
401
399
  }
402
400
  }
403
- return quads;
404
401
  }
405
402
 
406
403
  // ### `match` returns a new dataset that is comprised of all quads in the current instance matching the given arguments.
@@ -481,60 +478,9 @@ export default class N3Store {
481
478
  // and returns `true` if it returns truthy for any of them.
482
479
  // Setting any field to `undefined` or `null` indicates a wildcard.
483
480
  some(callback, subject, predicate, object, graph) {
484
- // Convert terms to internal string representation
485
- subject = subject && termToId(subject);
486
- predicate = predicate && termToId(predicate);
487
- object = object && termToId(object);
488
- graph = graph && termToId(graph);
489
-
490
- const graphs = this._getGraphs(graph), ids = this._ids;
491
- let content, subjectId, predicateId, objectId;
492
-
493
- // Translate IRIs to internal index keys.
494
- if (isString(subject) && !(subjectId = ids[subject]) ||
495
- isString(predicate) && !(predicateId = ids[predicate]) ||
496
- isString(object) && !(objectId = ids[object]))
497
- return false;
498
-
499
- for (const graphId in graphs) {
500
- // Only if the specified graph contains triples, there can be results
501
- if (content = graphs[graphId]) {
502
- // Choose the optimal index, based on what fields are present
503
- if (subjectId) {
504
- if (objectId) {
505
- // If subject and object are given, the object index will be the fastest
506
- if (this._findInIndex(content.objects, objectId, subjectId, predicateId,
507
- 'object', 'subject', 'predicate', graphId, callback, null))
508
- return true;
509
- }
510
- else
511
- // If only subject and possibly predicate are given, the subject index will be the fastest
512
- if (this._findInIndex(content.subjects, subjectId, predicateId, null,
513
- 'subject', 'predicate', 'object', graphId, callback, null))
514
- return true;
515
- }
516
- else if (predicateId) {
517
- // If only predicate and possibly object are given, the predicate index will be the fastest
518
- if (this._findInIndex(content.predicates, predicateId, objectId, null,
519
- 'predicate', 'object', 'subject', graphId, callback, null)) {
520
- return true;
521
- }
522
- }
523
- else if (objectId) {
524
- // If only object is given, the object index will be the fastest
525
- if (this._findInIndex(content.objects, objectId, null, null,
526
- 'object', 'subject', 'predicate', graphId, callback, null)) {
527
- return true;
528
- }
529
- }
530
- else
531
- // If nothing is given, iterate subjects and predicates first
532
- if (this._findInIndex(content.subjects, null, null, null,
533
- 'subject', 'predicate', 'object', graphId, callback, null)) {
534
- return true;
535
- }
536
- }
537
- }
481
+ for (const quad of this.readQuads(subject, predicate, object, graph))
482
+ if (callback(quad))
483
+ return true;
538
484
  return false;
539
485
  }
540
486
 
@@ -820,7 +766,7 @@ export default class N3Store {
820
766
  // Can be used where iterables are expected: for...of loops, array spread operator,
821
767
  // `yield*`, and destructuring assignment (order is not guaranteed).
822
768
  *[Symbol.iterator]() {
823
- yield* this.getQuads();
769
+ yield* this.readQuads();
824
770
  }
825
771
  }
826
772
 
@@ -851,7 +797,7 @@ class DatasetCoreAndReadableStream extends Readable {
851
797
  }
852
798
 
853
799
  _read() {
854
- for (const quad of this.filtered.getQuads())
800
+ for (const quad of this)
855
801
  this.push(quad);
856
802
  this.push(null);
857
803
  }
@@ -873,6 +819,6 @@ class DatasetCoreAndReadableStream extends Readable {
873
819
  }
874
820
 
875
821
  *[Symbol.iterator]() {
876
- yield* this.filtered.getQuads();
822
+ yield* this._filtered || this.n3Store.readQuads(this.subject, this.predicate, this.object, this.graph);
877
823
  }
878
824
  }
package/src/N3Util.js CHANGED
@@ -34,7 +34,7 @@ export function inDefaultGraph(quad) {
34
34
 
35
35
  // Creates a function that prepends the given IRI to a local name
36
36
  export function prefix(iri, factory) {
37
- return prefixes({ '': iri }, factory)('');
37
+ return prefixes({ '': iri.value || iri }, factory)('');
38
38
  }
39
39
 
40
40
  // Creates a function that allows registering and expanding prefixes