n3 1.17.1 → 1.17.3

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