n3 2.6.2 → 2.6.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
@@ -331,28 +331,25 @@ class N3Store {
331
331
 
332
332
  // ### `_countInIndex` counts matching quads in a three-layered index.
333
333
  // The index base is `index0` and the keys at each level are `key0`, `key1`, and `key2`.
334
- // Any of these keys can be undefined, which is interpreted as a wildcard.
334
+ // A key and any keys after it can be null or undefined, which is interpreted as a wildcard.
335
335
  _countInIndex(index0, key0, key1, key2) {
336
336
  let count = 0,
337
- tmp,
338
337
  index1,
339
338
  index2;
340
339
 
341
- // If a key is specified, count only that part of index 0
342
- if (key0) (tmp = index0, index0 = {})[key0] = tmp[key0];
343
- for (const value0 in index0) {
344
- if (index1 = index0[value0]) {
345
- // If a key is specified, count only that part of index 1
346
- if (key1) (tmp = index1, index1 = {})[key1] = tmp[key1];
347
- for (const value1 in index1) {
348
- if (index2 = index1[value1]) {
349
- // If a key is specified, count the quad if it exists
350
- if (key2) key2 in index2 && count++;
351
- // Otherwise, count all quads
352
- else count += index2[SIZE];
353
- }
354
- }
340
+ // Bound outer keys can be looked up directly.
341
+ if (key0) {
342
+ if (!(index1 = index0[key0])) return 0;
343
+ if (key1) {
344
+ if (!(index2 = index1[key1])) return 0;
345
+ return key2 ? key2 in index2 ? 1 : 0 : index2[SIZE];
355
346
  }
347
+ for (const value1 in index1) count += index1[value1][SIZE];
348
+ return count;
349
+ }
350
+ for (const value0 in index0) {
351
+ index1 = index0[value0];
352
+ for (const value1 in index1) count += index1[value1][SIZE];
356
353
  }
357
354
  return count;
358
355
  }
package/lib/N3Writer.js CHANGED
@@ -106,16 +106,16 @@ class N3Writer {
106
106
  // Write the graph's label if it has changed
107
107
  // (the id-based fast path of `equals` would conflate
108
108
  // the empty named node `<>` with the default graph)
109
- if (!graph.equals(this._graph) || graph.termType !== this._graph.termType) {
109
+ if (graph !== this._graph && (!graph.equals(this._graph) || graph.termType !== this._graph.termType)) {
110
110
  // Close the previous graph and start the new one
111
111
  this._write((this._subject === null ? '' : this._inDefaultGraph ? '.\n' : '\n}\n') + (DEFAULTGRAPH.equals(graph) ? '' : `${this._encodeIriOrBlank(graph)} {\n`));
112
112
  this._graph = graph;
113
113
  this._subject = null;
114
114
  }
115
115
  // Don't repeat the subject if it's the same
116
- if (subject.equals(this._subject)) {
116
+ if (subject === this._subject || subject.equals(this._subject)) {
117
117
  // Don't repeat the predicate if it's the same
118
- if (predicate.equals(this._predicate)) this._write(`, ${this._encodeObject(object)}`, done);
118
+ if (predicate === this._predicate || predicate.equals(this._predicate)) this._write(`, ${this._encodeObject(object)}`, done);
119
119
  // Same subject, different predicate
120
120
  else this._write(`;\n ${this._encodePredicate(this._predicate = predicate)} ${this._encodeObject(object)}`, done);
121
121
  }
@@ -177,8 +177,12 @@ class N3Writer {
177
177
  if (escape.test(value)) value = value.replace(escapeAll, characterReplacer);
178
178
 
179
179
  // Write a language-tagged literal
180
- const direction = literal.direction ? `--${literal.direction}` : '';
181
- if (literal.language) return `"${value}"@${literal.language}${direction}`;
180
+ const language = literal.language;
181
+ if (language) {
182
+ const literalDirection = literal.direction;
183
+ const direction = literalDirection ? `--${literalDirection}` : '';
184
+ return `"${value}"@${language}${direction}`;
185
+ }
182
186
 
183
187
  // Write dedicated literals per data type
184
188
  if (this._lineMode) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
package/src/N3Store.js CHANGED
@@ -351,25 +351,29 @@ export default class N3Store {
351
351
 
352
352
  // ### `_countInIndex` counts matching quads in a three-layered index.
353
353
  // The index base is `index0` and the keys at each level are `key0`, `key1`, and `key2`.
354
- // Any of these keys can be undefined, which is interpreted as a wildcard.
354
+ // A key and any keys after it can be null or undefined, which is interpreted as a wildcard.
355
355
  _countInIndex(index0, key0, key1, key2) {
356
- let count = 0, tmp, index1, index2;
356
+ let count = 0, index1, index2;
357
+
358
+ // Bound outer keys can be looked up directly.
359
+ if (key0) {
360
+ if (!(index1 = index0[key0]))
361
+ return 0;
362
+ if (key1) {
363
+ if (!(index2 = index1[key1]))
364
+ return 0;
365
+ return key2 ? (key2 in index2 ? 1 : 0) : index2[SIZE];
366
+ }
367
+
368
+ for (const value1 in index1)
369
+ count += index1[value1][SIZE];
370
+ return count;
371
+ }
357
372
 
358
- // If a key is specified, count only that part of index 0
359
- if (key0) (tmp = index0, index0 = {})[key0] = tmp[key0];
360
373
  for (const value0 in index0) {
361
- if (index1 = index0[value0]) {
362
- // If a key is specified, count only that part of index 1
363
- if (key1) (tmp = index1, index1 = {})[key1] = tmp[key1];
364
- for (const value1 in index1) {
365
- if (index2 = index1[value1]) {
366
- // If a key is specified, count the quad if it exists
367
- if (key2) (key2 in index2) && count++;
368
- // Otherwise, count all quads
369
- else count += index2[SIZE];
370
- }
371
- }
372
- }
374
+ index1 = index0[value0];
375
+ for (const value1 in index1)
376
+ count += index1[value1][SIZE];
373
377
  }
374
378
  return count;
375
379
  }
package/src/N3Writer.js CHANGED
@@ -90,7 +90,8 @@ export default class N3Writer {
90
90
  // Write the graph's label if it has changed
91
91
  // (the id-based fast path of `equals` would conflate
92
92
  // the empty named node `<>` with the default graph)
93
- if (!graph.equals(this._graph) || graph.termType !== this._graph.termType) {
93
+ if (graph !== this._graph &&
94
+ (!graph.equals(this._graph) || graph.termType !== this._graph.termType)) {
94
95
  // Close the previous graph and start the new one
95
96
  this._write((this._subject === null ? '' : (this._inDefaultGraph ? '.\n' : '\n}\n')) +
96
97
  (DEFAULTGRAPH.equals(graph) ? '' : `${this._encodeIriOrBlank(graph)} {\n`));
@@ -98,9 +99,9 @@ export default class N3Writer {
98
99
  this._subject = null;
99
100
  }
100
101
  // Don't repeat the subject if it's the same
101
- if (subject.equals(this._subject)) {
102
+ if (subject === this._subject || subject.equals(this._subject)) {
102
103
  // Don't repeat the predicate if it's the same
103
- if (predicate.equals(this._predicate))
104
+ if (predicate === this._predicate || predicate.equals(this._predicate))
104
105
  this._write(`, ${this._encodeObject(object)}`, done);
105
106
  // Same subject, different predicate
106
107
  else
@@ -179,9 +180,12 @@ export default class N3Writer {
179
180
  value = value.replace(escapeAll, characterReplacer);
180
181
 
181
182
  // Write a language-tagged literal
182
- const direction = literal.direction ? `--${literal.direction}` : '';
183
- if (literal.language)
184
- return `"${value}"@${literal.language}${direction}`;
183
+ const language = literal.language;
184
+ if (language) {
185
+ const literalDirection = literal.direction;
186
+ const direction = literalDirection ? `--${literalDirection}` : '';
187
+ return `"${value}"@${language}${direction}`;
188
+ }
185
189
 
186
190
  // Write dedicated literals per data type
187
191
  if (this._lineMode) {