n3 1.17.0 → 1.17.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/IRIs.js CHANGED
@@ -5,9 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
8
- XSD = 'http://www.w3.org/2001/XMLSchema#',
9
- SWAP = 'http://www.w3.org/2000/10/swap/';
10
- var _default = {
8
+ XSD = 'http://www.w3.org/2001/XMLSchema#',
9
+ SWAP = 'http://www.w3.org/2000/10/swap/';
10
+ var _default = exports.default = {
11
11
  xsd: {
12
12
  decimal: `${XSD}decimal`,
13
13
  boolean: `${XSD}boolean`,
@@ -32,5 +32,4 @@ var _default = {
32
32
  log: {
33
33
  implies: `${SWAP}log#implies`
34
34
  }
35
- };
36
- exports.default = _default;
35
+ };
@@ -8,25 +8,23 @@ exports.escapeQuotes = escapeQuotes;
8
8
  exports.termFromId = termFromId;
9
9
  exports.termToId = termToId;
10
10
  exports.unescapeQuotes = unescapeQuotes;
11
-
12
11
  var _IRIs = _interopRequireDefault(require("./IRIs"));
13
-
14
12
  var _N3Util = require("./N3Util");
15
-
16
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
14
  // N3.js implementations of the RDF/JS core data types
19
15
  // See https://github.com/rdfjs/representation-task-force/blob/master/interface-spec.md
16
+
20
17
  const {
21
18
  rdf,
22
19
  xsd
23
- } = _IRIs.default; // eslint-disable-next-line prefer-const
20
+ } = _IRIs.default;
24
21
 
22
+ // eslint-disable-next-line prefer-const
25
23
  let DEFAULTGRAPH;
26
24
  let _blankNodeCounter = 0;
27
25
  const escapedLiteral = /^"(.*".*)(?="[^"]*$)/;
28
- const quadId = /^<<("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ?("(?:""|[^"])*"[^ ]*|[^ ]+)?>>$/; // ## DataFactory singleton
29
26
 
27
+ // ## DataFactory singleton
30
28
  const DataFactory = {
31
29
  namedNode,
32
30
  blankNode,
@@ -36,104 +34,97 @@ const DataFactory = {
36
34
  quad,
37
35
  triple: quad
38
36
  };
39
- var _default = DataFactory; // ## Term constructor
40
-
41
- exports.default = _default;
42
-
37
+ var _default = exports.default = DataFactory; // ## Term constructor
43
38
  class Term {
44
39
  constructor(id) {
45
40
  this.id = id;
46
- } // ### The value of this term
47
-
41
+ }
48
42
 
43
+ // ### The value of this term
49
44
  get value() {
50
45
  return this.id;
51
- } // ### Returns whether this object represents the same term as the other
52
-
46
+ }
53
47
 
48
+ // ### Returns whether this object represents the same term as the other
54
49
  equals(other) {
55
50
  // If both terms were created by this library,
56
51
  // equality can be computed through ids
57
- if (other instanceof Term) return this.id === other.id; // Otherwise, compare term type and value
58
-
52
+ if (other instanceof Term) return this.id === other.id;
53
+ // Otherwise, compare term type and value
59
54
  return !!other && this.termType === other.termType && this.value === other.value;
60
- } // ### Implement hashCode for Immutable.js, since we implement `equals`
61
- // https://immutable-js.com/docs/v4.0.0/ValueObject/#hashCode()
62
-
55
+ }
63
56
 
57
+ // ### Implement hashCode for Immutable.js, since we implement `equals`
58
+ // https://immutable-js.com/docs/v4.0.0/ValueObject/#hashCode()
64
59
  hashCode() {
65
60
  return 0;
66
- } // ### Returns a plain object representation of this term
67
-
61
+ }
68
62
 
63
+ // ### Returns a plain object representation of this term
69
64
  toJSON() {
70
65
  return {
71
66
  termType: this.termType,
72
67
  value: this.value
73
68
  };
74
69
  }
70
+ }
75
71
 
76
- } // ## NamedNode constructor
77
-
78
-
72
+ // ## NamedNode constructor
79
73
  exports.Term = Term;
80
-
81
74
  class NamedNode extends Term {
82
75
  // ### The term type of this term
83
76
  get termType() {
84
77
  return 'NamedNode';
85
78
  }
79
+ }
86
80
 
87
- } // ## Literal constructor
88
-
89
-
81
+ // ## Literal constructor
90
82
  exports.NamedNode = NamedNode;
91
-
92
83
  class Literal extends Term {
93
84
  // ### The term type of this term
94
85
  get termType() {
95
86
  return 'Literal';
96
- } // ### The text value of this literal
97
-
87
+ }
98
88
 
89
+ // ### The text value of this literal
99
90
  get value() {
100
91
  return this.id.substring(1, this.id.lastIndexOf('"'));
101
- } // ### The language of this literal
102
-
92
+ }
103
93
 
94
+ // ### The language of this literal
104
95
  get language() {
105
96
  // Find the last quotation mark (e.g., '"abc"@en-us')
106
97
  const id = this.id;
107
- let atPos = id.lastIndexOf('"') + 1; // If "@" it follows, return the remaining substring; empty otherwise
108
-
98
+ let atPos = id.lastIndexOf('"') + 1;
99
+ // If "@" it follows, return the remaining substring; empty otherwise
109
100
  return atPos < id.length && id[atPos++] === '@' ? id.substr(atPos).toLowerCase() : '';
110
- } // ### The datatype IRI of this literal
111
-
101
+ }
112
102
 
103
+ // ### The datatype IRI of this literal
113
104
  get datatype() {
114
105
  return new NamedNode(this.datatypeString);
115
- } // ### The datatype string of this literal
116
-
106
+ }
117
107
 
108
+ // ### The datatype string of this literal
118
109
  get datatypeString() {
119
110
  // Find the last quotation mark (e.g., '"abc"^^http://ex.org/types#t')
120
111
  const id = this.id,
121
- dtPos = id.lastIndexOf('"') + 1;
122
- const char = dtPos < id.length ? id[dtPos] : ''; // If "^" it follows, return the remaining substring
123
-
124
- return char === '^' ? id.substr(dtPos + 2) : // If "@" follows, return rdf:langString; xsd:string otherwise
112
+ dtPos = id.lastIndexOf('"') + 1;
113
+ const char = dtPos < id.length ? id[dtPos] : '';
114
+ // If "^" it follows, return the remaining substring
115
+ return char === '^' ? id.substr(dtPos + 2) :
116
+ // If "@" follows, return rdf:langString; xsd:string otherwise
125
117
  char !== '@' ? xsd.string : rdf.langString;
126
- } // ### Returns whether this object represents the same term as the other
127
-
118
+ }
128
119
 
120
+ // ### Returns whether this object represents the same term as the other
129
121
  equals(other) {
130
122
  // If both literals were created by this library,
131
123
  // equality can be computed through ids
132
- if (other instanceof Literal) return this.id === other.id; // Otherwise, compare term type, value, language, and datatype
133
-
124
+ if (other instanceof Literal) return this.id === other.id;
125
+ // Otherwise, compare term type, value, language, and datatype
134
126
  return !!other && !!other.datatype && this.termType === other.termType && this.value === other.value && this.language === other.language && this.datatype.value === other.datatype.value;
135
127
  }
136
-
137
128
  toJSON() {
138
129
  return {
139
130
  termType: this.termType,
@@ -145,139 +136,138 @@ class Literal extends Term {
145
136
  }
146
137
  };
147
138
  }
139
+ }
148
140
 
149
- } // ## BlankNode constructor
150
-
151
-
141
+ // ## BlankNode constructor
152
142
  exports.Literal = Literal;
153
-
154
143
  class BlankNode extends Term {
155
144
  constructor(name) {
156
145
  super(`_:${name}`);
157
- } // ### The term type of this term
158
-
146
+ }
159
147
 
148
+ // ### The term type of this term
160
149
  get termType() {
161
150
  return 'BlankNode';
162
- } // ### The name of this blank node
163
-
151
+ }
164
152
 
153
+ // ### The name of this blank node
165
154
  get value() {
166
155
  return this.id.substr(2);
167
156
  }
168
-
169
157
  }
170
-
171
158
  exports.BlankNode = BlankNode;
172
-
173
159
  class Variable extends Term {
174
160
  constructor(name) {
175
161
  super(`?${name}`);
176
- } // ### The term type of this term
177
-
162
+ }
178
163
 
164
+ // ### The term type of this term
179
165
  get termType() {
180
166
  return 'Variable';
181
- } // ### The name of this variable
182
-
167
+ }
183
168
 
169
+ // ### The name of this variable
184
170
  get value() {
185
171
  return this.id.substr(1);
186
172
  }
173
+ }
187
174
 
188
- } // ## DefaultGraph constructor
189
-
190
-
175
+ // ## DefaultGraph constructor
191
176
  exports.Variable = Variable;
192
-
193
177
  class DefaultGraph extends Term {
194
178
  constructor() {
195
179
  super('');
196
180
  return DEFAULTGRAPH || this;
197
- } // ### The term type of this term
198
-
181
+ }
199
182
 
183
+ // ### The term type of this term
200
184
  get termType() {
201
185
  return 'DefaultGraph';
202
- } // ### Returns whether this object represents the same term as the other
203
-
186
+ }
204
187
 
188
+ // ### Returns whether this object represents the same term as the other
205
189
  equals(other) {
206
190
  // If both terms were created by this library,
207
191
  // equality can be computed through strict equality;
208
192
  // otherwise, compare term types.
209
193
  return this === other || !!other && this.termType === other.termType;
210
194
  }
195
+ }
211
196
 
212
- } // ## DefaultGraph singleton
213
-
214
-
197
+ // ## DefaultGraph singleton
215
198
  exports.DefaultGraph = DefaultGraph;
216
- DEFAULTGRAPH = new DefaultGraph(); // ### Constructs a term from the given internal string ID
199
+ DEFAULTGRAPH = new DefaultGraph();
217
200
 
218
- function termFromId(id, factory) {
219
- factory = factory || DataFactory; // Falsy value or empty string indicate the default graph
201
+ // ### Constructs a term from the given internal string ID
202
+ // The third 'nested' parameter of this function is to aid
203
+ // with recursion over nested terms. It should not be used
204
+ // by consumers of this library.
205
+ // See https://github.com/rdfjs/N3.js/pull/311#discussion_r1061042725
206
+ function termFromId(id, factory, nested) {
207
+ factory = factory || DataFactory;
220
208
 
221
- if (!id) return factory.defaultGraph(); // Identify the term type based on the first character
209
+ // Falsy value or empty string indicate the default graph
210
+ if (!id) return factory.defaultGraph();
222
211
 
212
+ // Identify the term type based on the first character
223
213
  switch (id[0]) {
224
214
  case '?':
225
215
  return factory.variable(id.substr(1));
226
-
227
216
  case '_':
228
217
  return factory.blankNode(id.substr(2));
229
-
230
218
  case '"':
231
219
  // Shortcut for internal literals
232
- if (factory === DataFactory) return new Literal(id); // Literal without datatype or language
233
-
234
- if (id[id.length - 1] === '"') return factory.literal(id.substr(1, id.length - 2)); // Literal with datatype or language
235
-
220
+ if (factory === DataFactory) return new Literal(id);
221
+ // Literal without datatype or language
222
+ if (id[id.length - 1] === '"') return factory.literal(id.substr(1, id.length - 2));
223
+ // Literal with datatype or language
236
224
  const endPos = id.lastIndexOf('"', id.length - 1);
237
225
  return factory.literal(id.substr(1, endPos - 1), id[endPos + 1] === '@' ? id.substr(endPos + 2) : factory.namedNode(id.substr(endPos + 3)));
238
-
239
- case '<':
240
- const components = quadId.exec(id);
241
- return factory.quad(termFromId(unescapeQuotes(components[1]), factory), termFromId(unescapeQuotes(components[2]), factory), termFromId(unescapeQuotes(components[3]), factory), components[4] && termFromId(unescapeQuotes(components[4]), factory));
242
-
226
+ case '[':
227
+ id = JSON.parse(id);
228
+ break;
243
229
  default:
244
- return factory.namedNode(id);
230
+ if (!nested || !Array.isArray(id)) {
231
+ return factory.namedNode(id);
232
+ }
245
233
  }
246
- } // ### Constructs an internal string ID from the given term or ID string
247
-
234
+ return factory.quad(termFromId(id[0], factory, true), termFromId(id[1], factory, true), termFromId(id[2], factory, true), id[3] && termFromId(id[3], factory, true));
235
+ }
248
236
 
249
- function termToId(term) {
237
+ // ### Constructs an internal string ID from the given term or ID string
238
+ // The third 'nested' parameter of this function is to aid
239
+ // with recursion over nested terms. It should not be used
240
+ // by consumers of this library.
241
+ // See https://github.com/rdfjs/N3.js/pull/311#discussion_r1061042725
242
+ function termToId(term, nested) {
250
243
  if (typeof term === 'string') return term;
251
244
  if (term instanceof Term && term.termType !== 'Quad') return term.id;
252
- if (!term) return DEFAULTGRAPH.id; // Term instantiated with another library
245
+ if (!term) return DEFAULTGRAPH.id;
253
246
 
247
+ // Term instantiated with another library
254
248
  switch (term.termType) {
255
249
  case 'NamedNode':
256
250
  return term.value;
257
-
258
251
  case 'BlankNode':
259
252
  return `_:${term.value}`;
260
-
261
253
  case 'Variable':
262
254
  return `?${term.value}`;
263
-
264
255
  case 'DefaultGraph':
265
256
  return '';
266
-
267
257
  case 'Literal':
268
258
  return `"${term.value}"${term.language ? `@${term.language}` : term.datatype && term.datatype.value !== xsd.string ? `^^${term.datatype.value}` : ''}`;
269
-
270
259
  case 'Quad':
271
- // To identify RDF* quad components, we escape quotes by doubling them.
272
- // This avoids the overhead of backslash parsing of Turtle-like syntaxes.
273
- return `<<${escapeQuotes(termToId(term.subject))} ${escapeQuotes(termToId(term.predicate))} ${escapeQuotes(termToId(term.object))}${(0, _N3Util.isDefaultGraph)(term.graph) ? '' : ` ${termToId(term.graph)}`}>>`;
274
-
260
+ const res = [termToId(term.subject, true), termToId(term.predicate, true), termToId(term.object, true)];
261
+ if (!(0, _N3Util.isDefaultGraph)(term.graph)) {
262
+ res.push(termToId(term.graph, true));
263
+ }
264
+ return nested ? res : JSON.stringify(res);
275
265
  default:
276
266
  throw new Error(`Unexpected termType: ${term.termType}`);
277
267
  }
278
- } // ## Quad constructor
279
-
268
+ }
280
269
 
270
+ // ## Quad constructor
281
271
  class Quad extends Term {
282
272
  constructor(subject, predicate, object, graph) {
283
273
  super('');
@@ -285,30 +275,26 @@ class Quad extends Term {
285
275
  this._predicate = predicate;
286
276
  this._object = object;
287
277
  this._graph = graph || DEFAULTGRAPH;
288
- } // ### The term type of this term
289
-
278
+ }
290
279
 
280
+ // ### The term type of this term
291
281
  get termType() {
292
282
  return 'Quad';
293
283
  }
294
-
295
284
  get subject() {
296
285
  return this._subject;
297
286
  }
298
-
299
287
  get predicate() {
300
288
  return this._predicate;
301
289
  }
302
-
303
290
  get object() {
304
291
  return this._object;
305
292
  }
306
-
307
293
  get graph() {
308
294
  return this._graph;
309
- } // ### Returns a plain object representation of this quad
310
-
295
+ }
311
296
 
297
+ // ### Returns a plain object representation of this quad
312
298
  toJSON() {
313
299
  return {
314
300
  termType: this.termType,
@@ -317,70 +303,68 @@ class Quad extends Term {
317
303
  object: this._object.toJSON(),
318
304
  graph: this._graph.toJSON()
319
305
  };
320
- } // ### Returns whether this object represents the same quad as the other
321
-
306
+ }
322
307
 
308
+ // ### Returns whether this object represents the same quad as the other
323
309
  equals(other) {
324
310
  return !!other && this._subject.equals(other.subject) && this._predicate.equals(other.predicate) && this._object.equals(other.object) && this._graph.equals(other.graph);
325
311
  }
326
-
327
312
  }
328
-
329
313
  exports.Triple = exports.Quad = Quad;
330
-
331
314
  // ### Escapes the quotes within the given literal
332
315
  function escapeQuotes(id) {
333
316
  return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/"/g, '""')}`);
334
- } // ### Unescapes the quotes within the given literal
335
-
317
+ }
336
318
 
319
+ // ### Unescapes the quotes within the given literal
337
320
  function unescapeQuotes(id) {
338
321
  return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/""/g, '"')}`);
339
- } // ### Creates an IRI
340
-
322
+ }
341
323
 
324
+ // ### Creates an IRI
342
325
  function namedNode(iri) {
343
326
  return new NamedNode(iri);
344
- } // ### Creates a blank node
345
-
327
+ }
346
328
 
329
+ // ### Creates a blank node
347
330
  function blankNode(name) {
348
331
  return new BlankNode(name || `n3-${_blankNodeCounter++}`);
349
- } // ### Creates a literal
350
-
332
+ }
351
333
 
334
+ // ### Creates a literal
352
335
  function literal(value, languageOrDataType) {
353
336
  // Create a language-tagged string
354
- if (typeof languageOrDataType === 'string') return new Literal(`"${value}"@${languageOrDataType.toLowerCase()}`); // Automatically determine datatype for booleans and numbers
337
+ if (typeof languageOrDataType === 'string') return new Literal(`"${value}"@${languageOrDataType.toLowerCase()}`);
355
338
 
339
+ // Automatically determine datatype for booleans and numbers
356
340
  let datatype = languageOrDataType ? languageOrDataType.value : '';
357
-
358
341
  if (datatype === '') {
359
342
  // Convert a boolean
360
- if (typeof value === 'boolean') datatype = xsd.boolean; // Convert an integer or double
343
+ if (typeof value === 'boolean') datatype = xsd.boolean;
344
+ // Convert an integer or double
361
345
  else if (typeof value === 'number') {
362
346
  if (Number.isFinite(value)) datatype = Number.isInteger(value) ? xsd.integer : xsd.double;else {
363
347
  datatype = xsd.double;
364
348
  if (!Number.isNaN(value)) value = value > 0 ? 'INF' : '-INF';
365
349
  }
366
350
  }
367
- } // Create a datatyped literal
368
-
351
+ }
369
352
 
353
+ // Create a datatyped literal
370
354
  return datatype === '' || datatype === xsd.string ? new Literal(`"${value}"`) : new Literal(`"${value}"^^${datatype}`);
371
- } // ### Creates a variable
372
-
355
+ }
373
356
 
357
+ // ### Creates a variable
374
358
  function variable(name) {
375
359
  return new Variable(name);
376
- } // ### Returns the default graph
377
-
360
+ }
378
361
 
362
+ // ### Returns the default graph
379
363
  function defaultGraph() {
380
364
  return DEFAULTGRAPH;
381
- } // ### Creates a quad
382
-
365
+ }
383
366
 
367
+ // ### Creates a quad
384
368
  function quad(subject, predicate, object, graph) {
385
369
  return new Quad(subject, predicate, object, graph);
386
370
  }