rdflib 2.2.21 → 2.2.22-0de51c50

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.
Files changed (115) hide show
  1. package/dist/rdflib.min.js +1 -1
  2. package/dist/rdflib.min.js.LICENSE.txt +13 -1
  3. package/dist/rdflib.min.js.map +1 -1
  4. package/esm/blank-node.js +3 -27
  5. package/esm/collection.js +7 -29
  6. package/esm/default-graph.js +1 -15
  7. package/esm/empty.js +0 -13
  8. package/esm/factories/canonical-data-factory.js +5 -40
  9. package/esm/factories/extended-term-factory.js +0 -11
  10. package/esm/factories/factory-types.js +2 -1
  11. package/esm/factories/rdflib-data-factory.js +0 -8
  12. package/esm/fetcher.js +257 -481
  13. package/esm/formula.js +43 -142
  14. package/esm/index.js +19 -36
  15. package/esm/jsonldparser.js +20 -29
  16. package/esm/jsonparser.js +0 -7
  17. package/esm/lists.js +18 -33
  18. package/esm/literal.js +10 -42
  19. package/esm/n3parser.js +38 -351
  20. package/esm/named-node.js +8 -27
  21. package/esm/namespace.js +0 -1
  22. package/esm/node-internal.js +11 -24
  23. package/esm/node.js +1 -6
  24. package/esm/parse.js +10 -17
  25. package/esm/patch-parser.js +9 -29
  26. package/esm/query-to-sparql.js +0 -18
  27. package/esm/query.js +50 -133
  28. package/esm/rdfaparser.js +72 -210
  29. package/esm/rdfxmlparser.js +31 -128
  30. package/esm/serialize.js +8 -33
  31. package/esm/serializer.js +122 -286
  32. package/esm/sparql-to-query.js +44 -134
  33. package/esm/statement.js +8 -19
  34. package/esm/store.js +109 -280
  35. package/esm/types.js +3 -2
  36. package/esm/update-manager.js +110 -246
  37. package/esm/updates-via.js +0 -27
  38. package/esm/uri.js +6 -50
  39. package/esm/utils/default-graph-uri.js +1 -0
  40. package/esm/utils/termValue.js +0 -1
  41. package/esm/utils/terms.js +15 -16
  42. package/esm/utils-js.js +16 -57
  43. package/esm/utils.js +4 -15
  44. package/esm/variable.js +0 -20
  45. package/lib/blank-node.js +3 -28
  46. package/lib/class-order.js +0 -1
  47. package/lib/collection.js +7 -34
  48. package/lib/default-graph.js +0 -20
  49. package/lib/empty.js +0 -18
  50. package/lib/factories/canonical-data-factory.js +4 -51
  51. package/lib/factories/extended-term-factory.js +0 -18
  52. package/lib/factories/factory-types.d.ts +6 -6
  53. package/lib/factories/factory-types.js +0 -3
  54. package/lib/factories/rdflib-data-factory.js +0 -12
  55. package/lib/fetcher.d.ts +6 -6
  56. package/lib/fetcher.js +258 -470
  57. package/lib/formula.d.ts +1 -1
  58. package/lib/formula.js +43 -151
  59. package/lib/index.d.ts +1 -2
  60. package/lib/index.js +14 -82
  61. package/lib/jsonldparser.js +19 -33
  62. package/lib/jsonparser.js +0 -9
  63. package/lib/lists.js +14 -39
  64. package/lib/literal.js +10 -49
  65. package/lib/log.d.ts +0 -6
  66. package/lib/log.js +0 -1
  67. package/lib/n3parser.d.ts +1 -0
  68. package/lib/n3parser.js +39 -359
  69. package/lib/named-node.js +7 -35
  70. package/lib/namespace.js +0 -3
  71. package/lib/node-internal.js +11 -21
  72. package/lib/node.js +1 -11
  73. package/lib/parse.d.ts +1 -1
  74. package/lib/parse.js +8 -27
  75. package/lib/patch-parser.js +10 -33
  76. package/lib/query-to-sparql.js +0 -23
  77. package/lib/query.js +46 -152
  78. package/lib/rdfaparser.js +72 -220
  79. package/lib/rdfxmlparser.js +31 -132
  80. package/lib/serialize.d.ts +1 -1
  81. package/lib/serialize.js +8 -43
  82. package/lib/serializer.d.ts +1 -0
  83. package/lib/serializer.js +121 -297
  84. package/lib/sparql-to-query.js +42 -167
  85. package/lib/statement.js +8 -24
  86. package/lib/store.d.ts +2 -2
  87. package/lib/store.js +102 -282
  88. package/lib/tf-types.d.ts +4 -4
  89. package/lib/types.d.ts +8 -8
  90. package/lib/types.js +3 -3
  91. package/lib/update-manager.d.ts +2 -2
  92. package/lib/update-manager.js +110 -254
  93. package/lib/updates-via.js +0 -30
  94. package/lib/uri.js +5 -58
  95. package/lib/utils/default-graph-uri.js +1 -3
  96. package/lib/utils/termValue.js +0 -2
  97. package/lib/utils/terms.js +15 -34
  98. package/lib/utils-js.js +16 -80
  99. package/lib/utils.js +4 -21
  100. package/lib/variable.js +0 -25
  101. package/lib/xsd-internal.js +0 -3
  102. package/lib/xsd.js +0 -4
  103. package/package.json +40 -39
  104. package/src/fetcher.ts +2 -2
  105. package/src/index.ts +0 -2
  106. package/src/jsonldparser.js +13 -4
  107. package/src/n3parser.js +12 -4
  108. package/src/serialize.ts +4 -11
  109. package/src/serializer.js +24 -0
  110. package/src/update-manager.ts +18 -7
  111. package/changes.txt +0 -59
  112. package/esm/convert.js +0 -61
  113. package/lib/convert.d.ts +0 -2
  114. package/lib/convert.js +0 -77
  115. package/src/convert.js +0 -70
package/esm/serializer.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
2
  import _createClass from "@babel/runtime/helpers/createClass";
3
3
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
-
5
4
  /* Serialization of RDF Graphs
6
5
  **
7
6
  ** Tim Berners-Lee 2006
@@ -16,6 +15,8 @@ import * as Util from './utils-js';
16
15
  import CanonicalDataFactory from './factories/canonical-data-factory';
17
16
  import { createXSD } from './xsd';
18
17
  import solidNs from 'solid-namespace';
18
+ // import * as jsonld from 'jsonld'
19
+ import * as ttl2jsonld from '@frogcat/ttl2jsonld';
19
20
  export default function createSerializer(store) {
20
21
  return new Serializer(store);
21
22
  }
@@ -23,50 +24,34 @@ export default function createSerializer(store) {
23
24
  export var Serializer = /*#__PURE__*/function () {
24
25
  function Serializer(store) {
25
26
  _classCallCheck(this, Serializer);
26
-
27
27
  _defineProperty(this, "_notQNameChars", '\t\r\n !"#$%&\'()*.,+/;<=>?@[\\]^`{|}~');
28
-
29
28
  _defineProperty(this, "_notNameChars", this._notQNameChars + ':');
30
-
31
29
  _defineProperty(this, "validPrefix", new RegExp(/^[a-zA-Z][a-zA-Z0-9]*$/));
32
-
33
30
  _defineProperty(this, "forbidden1", new RegExp(/[\\"\b\f\r\v\t\n\u0080-\uffff]/gm));
34
-
35
31
  _defineProperty(this, "forbidden3", new RegExp(/[\\"\b\f\r\v\u0080-\uffff]/gm));
36
-
37
32
  this.flags = '';
38
33
  this.base = null;
39
34
  this.prefixes = []; // suggested prefixes
40
-
41
35
  this.namespaces = []; // complementary
42
-
43
36
  var nsKeys = Object.keys(solidNs());
44
-
45
37
  for (var i in nsKeys) {
46
38
  var uri = solidNs()[nsKeys[i]]('');
47
39
  var prefix = nsKeys[i];
48
40
  this.prefixes[uri] = prefix;
49
41
  this.namespaces[prefix] = uri;
50
42
  }
51
-
52
43
  this.suggestPrefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); // XML code assumes this!
53
-
54
44
  this.suggestPrefix('xml', 'reserved:reservedForFutureUse'); // XML reserves xml: in the spec.
55
45
 
56
46
  this.namespacesUsed = []; // Count actually used and so needed in @prefixes
57
-
58
47
  this.keywords = ['a']; // The only one we generate at the moment
59
-
60
48
  this.prefixchars = 'abcdefghijklmnopqustuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
61
49
  this.incoming = null; // Array not calculated yet
62
-
63
50
  this.formulas = []; // remembering original formulae from hashes
64
-
65
51
  this.store = store;
66
52
  this.rdfFactory = store.rdfFactory || CanonicalDataFactory;
67
53
  this.xsd = createXSD(this.rdfFactory);
68
54
  }
69
-
70
55
  _createClass(Serializer, [{
71
56
  key: "setBase",
72
57
  value: function setBase(base) {
@@ -83,7 +68,6 @@ export var Serializer = /*#__PURE__*/function () {
83
68
  key: "toStr",
84
69
  value: function toStr(x) {
85
70
  var s = x.toNT();
86
-
87
71
  if (x.termType === 'Graph') {
88
72
  this.formulas[s] = x; // remember as reverse does not work
89
73
  }
@@ -98,124 +82,109 @@ export var Serializer = /*#__PURE__*/function () {
98
82
  if (!x) console.log('No formula object for ' + s);
99
83
  return x;
100
84
  }
101
-
102
85
  return this.store.fromNT(s);
103
86
  }
87
+
104
88
  /**
105
89
  * Defines a set of [prefix, namespace] pairs to be used by this Serializer instance.
106
90
  * Overrides previous prefixes if any
107
91
  * @param namespaces
108
92
  * @return {Serializer}
109
93
  */
110
-
111
94
  }, {
112
95
  key: "setNamespaces",
113
96
  value: function setNamespaces(namespaces) {
114
97
  for (var px in namespaces) {
115
98
  this.setPrefix(px, namespaces[px]);
116
99
  }
117
-
118
100
  return this;
119
101
  }
102
+
120
103
  /**
121
104
  * Defines a namespace prefix, overriding any existing prefix for that URI
122
105
  * @param prefix
123
106
  * @param uri
124
107
  */
125
-
126
108
  }, {
127
109
  key: "setPrefix",
128
110
  value: function setPrefix(prefix, uri) {
129
111
  if (prefix.slice(0, 7) === 'default') return; // Try to weed these out
130
-
131
112
  if (prefix.slice(0, 2) === 'ns') return; // From others inferior algos
132
-
133
113
  if (!prefix || !uri) return; // empty strings not suitable
114
+
134
115
  // remove any existing prefix targeting this uri
135
116
  // for (let existingPrefix in this.namespaces) {
136
117
  // if (this.namespaces[existingPrefix] == uri)
137
118
  // delete this.namespaces[existingPrefix];
138
119
  // }
139
- // remove any existing mapping for this prefix
140
120
 
121
+ // remove any existing mapping for this prefix
141
122
  for (var existingNs in this.prefixes) {
142
123
  if (this.prefixes[existingNs] == prefix) delete this.prefixes[existingNs];
143
124
  }
144
-
145
125
  this.prefixes[uri] = prefix;
146
126
  this.namespaces[prefix] = uri;
147
127
  }
128
+
148
129
  /* Accumulate Namespaces
149
130
  **
150
131
  ** These are only hints. If two overlap, only one gets used
151
132
  ** There is therefore no guarantee in general.
152
133
  */
153
-
154
134
  }, {
155
135
  key: "suggestPrefix",
156
136
  value: function suggestPrefix(prefix, uri) {
157
137
  if (prefix.slice(0, 7) === 'default') return; // Try to weed these out
158
-
159
138
  if (prefix.slice(0, 2) === 'ns') return; // From others inferior algos
160
-
161
139
  if (!prefix || !uri) return; // empty strings not suitable
162
-
163
140
  if (prefix in this.namespaces || uri in this.prefixes) return; // already used
164
-
165
141
  this.prefixes[uri] = prefix;
166
142
  this.namespaces[prefix] = uri;
167
- } // Takes a namespace -> prefix map
143
+ }
168
144
 
145
+ // Takes a namespace -> prefix map
169
146
  }, {
170
147
  key: "suggestNamespaces",
171
148
  value: function suggestNamespaces(namespaces) {
172
149
  for (var px in namespaces) {
173
150
  this.suggestPrefix(px, namespaces[px]);
174
151
  }
175
-
176
152
  return this;
177
153
  }
178
154
  }, {
179
155
  key: "checkIntegrity",
180
156
  value: function checkIntegrity() {
181
157
  var p, ns;
182
-
183
158
  for (p in this.namespaces) {
184
159
  if (this.prefixes[this.namespaces[p]] !== p) {
185
160
  throw new Error('Serializer integity error 1: ' + p + ', ' + this.namespaces[p] + ', ' + this.prefixes[this.namespaces[p]] + '!');
186
161
  }
187
162
  }
188
-
189
163
  for (ns in this.prefixes) {
190
164
  if (this.namespaces[this.prefixes[ns]] !== ns) {
191
165
  throw new Error('Serializer integity error 2: ' + ns + ', ' + this.prefixs[ns] + ', ' + this.namespaces[this.prefixes[ns]] + '!');
192
166
  }
193
167
  }
194
- } // Make up an unused prefix for a random namespace
168
+ }
195
169
 
170
+ // Make up an unused prefix for a random namespace
196
171
  }, {
197
172
  key: "makeUpPrefix",
198
173
  value: function makeUpPrefix(uri) {
199
174
  var p = uri;
200
-
201
175
  function canUseMethod(pp) {
202
176
  if (!this.validPrefix.test(pp)) return false; // bad format
203
-
204
177
  if (pp === 'ns') return false; // boring
205
-
206
178
  if (pp in this.namespaces) return false; // already used
207
-
208
179
  this.prefixes[uri] = pp;
209
180
  this.namespaces[pp] = uri;
210
181
  return pp;
211
182
  }
212
-
213
183
  var canUse = canUseMethod.bind(this);
214
184
  if ('#/'.indexOf(p[p.length - 1]) >= 0) p = p.slice(0, -1);
215
185
  var slash = p.lastIndexOf('/');
216
186
  if (slash >= 0) p = p.slice(slash + 1);
217
187
  var i = 0;
218
-
219
188
  while (i < p.length) {
220
189
  if (this.prefixchars.indexOf(p[i])) {
221
190
  i++;
@@ -223,23 +192,18 @@ export var Serializer = /*#__PURE__*/function () {
223
192
  break;
224
193
  }
225
194
  }
226
-
227
195
  p = p.slice(0, i);
228
196
  if (p.length < 6 && canUse(p)) return p; // exact is best
229
-
230
197
  if (canUse(p.slice(0, 3))) return p.slice(0, 3);
231
198
  if (canUse(p.slice(0, 2))) return p.slice(0, 2);
232
199
  if (canUse(p.slice(0, 4))) return p.slice(0, 4);
233
200
  if (canUse(p.slice(0, 1))) return p.slice(0, 1);
234
201
  if (canUse(p.slice(0, 5))) return p.slice(0, 5);
235
-
236
202
  if (!this.validPrefix.test(p)) {
237
203
  p = 'n'; // Otherwise the loop below may never termimnate
238
204
  }
239
205
 
240
- for (var j = 0;; j++) {
241
- if (canUse(p.slice(0, 3) + j)) return p.slice(0, 3) + j;
242
- }
206
+ for (var j = 0;; j++) if (canUse(p.slice(0, 3) + j)) return p.slice(0, 3) + j;
243
207
  }
244
208
  }, {
245
209
  key: "rootSubjects",
@@ -247,6 +211,7 @@ export var Serializer = /*#__PURE__*/function () {
247
211
  var incoming = {};
248
212
  var subjects = {};
249
213
  var allBnodes = {};
214
+
250
215
  /* This scan is to find out which nodes will have to be the roots of trees
251
216
  ** in the serialized form. This will be any symbols, and any bnodes
252
217
  ** which hve more or less than one incoming arc, and any bnodes which have
@@ -254,10 +219,8 @@ export var Serializer = /*#__PURE__*/function () {
254
219
  ** This should be kept linear time with repect to the number of statements.
255
220
  ** Note it does not use any indexing of the store.
256
221
  */
257
-
258
222
  for (var i = 0; i < sts.length; i++) {
259
223
  var st = sts[i];
260
-
261
224
  var checkMentions = function checkMentions(x) {
262
225
  if (!incoming.hasOwnProperty(x)) incoming[x] = [];
263
226
  incoming[x].push(st.subject); // List of things which will cause this to be printed
@@ -273,43 +236,39 @@ export var Serializer = /*#__PURE__*/function () {
273
236
  });
274
237
  }
275
238
  });
239
+
276
240
  checkMentions(sts[i].object);
277
241
  var ss = subjects[this.toStr(st.subject)]; // Statements with this as subject
278
-
279
242
  if (!ss) ss = [];
280
243
  ss.push(st);
281
244
  subjects[this.toStr(st.subject)] = ss; // Make hash. @@ too slow for formula?
282
245
  }
283
246
 
284
247
  var roots = [];
285
-
286
248
  for (var xNT in subjects) {
287
249
  if (!subjects.hasOwnProperty(xNT)) continue;
288
250
  var y = this.fromStr(xNT);
289
-
290
251
  if (y.termType !== 'BlankNode' || !incoming[y] || incoming[y].length !== 1) {
291
252
  roots.push(y);
292
253
  continue;
293
254
  }
294
255
  }
295
-
296
256
  this.incoming = incoming; // Keep for serializing @@ Bug for nested formulas
297
- // Now do the scan using existing roots
298
257
 
258
+ // Now do the scan using existing roots
299
259
  var rootsHash = {};
300
-
301
260
  for (var k = 0; k < roots.length; k++) {
302
261
  rootsHash[roots[k].toNT()] = true;
303
262
  }
304
-
305
263
  return {
306
264
  'roots': roots,
307
265
  'subjects': subjects,
308
266
  'rootsHash': rootsHash,
309
267
  'incoming': incoming
310
268
  };
311
- } // //////////////////////////////////////////////////////
269
+ }
312
270
 
271
+ // //////////////////////////////////////////////////////
313
272
  }, {
314
273
  key: "toN3",
315
274
  value: function toN3(f) {
@@ -326,7 +285,6 @@ export var Serializer = /*#__PURE__*/function () {
326
285
  } else {
327
286
  uri = hexify(uri);
328
287
  }
329
-
330
288
  return '<' + uri + '>';
331
289
  }
332
290
  }, {
@@ -339,41 +297,33 @@ export var Serializer = /*#__PURE__*/function () {
339
297
  var self = this;
340
298
  var kb = this.store;
341
299
  var factory = this.rdfFactory;
342
-
343
300
  var termToNT = function termToNT(x) {
344
301
  if (x.termType !== 'Collection') {
345
302
  return self.atomicTermToN3(x);
346
303
  }
347
-
348
304
  var list = x.elements;
349
305
  var rest = kb.sym(rdfns + 'nill');
350
-
351
306
  for (var i = list.length - 1; i >= 0; i--) {
352
307
  var bnode = factory.blankNode();
353
308
  str += termToNT(bnode) + ' ' + termToNT(kb.sym(rdfns + 'first')) + ' ' + termToNT(list[i]) + '.\n';
354
309
  str += termToNT(bnode) + ' ' + termToNT(kb.sym(rdfns + 'rest')) + ' ' + termToNT(rest) + '.\n';
355
310
  rest = bnode;
356
311
  }
357
-
358
312
  return self.atomicTermToN3(rest);
359
313
  };
360
-
361
314
  for (var i = 0; i < sorted.length; i++) {
362
315
  var st = sorted[i];
363
316
  var s = '';
364
317
  s += termToNT(st.subject) + ' ';
365
318
  s += termToNT(st.predicate) + ' ';
366
319
  s += termToNT(st.object) + ' ';
367
-
368
320
  if (this.flags.indexOf('q') >= 0) {
369
321
  // Do quads not nrtiples
370
322
  s += termToNT(st.why) + ' ';
371
323
  }
372
-
373
324
  s += '.\n';
374
325
  str += s;
375
326
  }
376
-
377
327
  return str;
378
328
  }
379
329
  }, {
@@ -381,59 +331,46 @@ export var Serializer = /*#__PURE__*/function () {
381
331
  value: function statementsToN3(sts) {
382
332
  var indent = 4;
383
333
  var width = 80;
384
- var kb = this.store; // A URI Map alows us to put the type statemnts at the top.
385
-
334
+ var kb = this.store;
335
+ // A URI Map alows us to put the type statemnts at the top.
386
336
  var uriMap = {
387
337
  'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'aaa:00'
388
338
  };
389
-
390
339
  var SPO = function SPO(x, y) {
391
340
  // Do limited canonicalization of bnodes
392
341
  return Util.heavyCompareSPO(x, y, kb, uriMap);
393
342
  };
394
-
395
343
  sts.sort(SPO);
396
-
397
344
  if (this.base && !this.defaultNamespace) {
398
345
  this.defaultNamespace = this.base + '#';
399
346
  }
400
-
401
347
  var predMap = {};
402
-
403
348
  if (this.flags.indexOf('s') < 0) {
404
349
  predMap['http://www.w3.org/2002/07/owl#sameAs'] = '=';
405
350
  }
406
-
407
351
  if (this.flags.indexOf('t') < 0) {
408
352
  predMap['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = 'a';
409
353
  }
410
-
411
354
  if (this.flags.indexOf('i') < 0) {
412
355
  predMap['http://www.w3.org/2000/10/swap/log#implies'] = '=>';
413
- } // //////////////////////// Arrange the bits of text
414
-
356
+ }
357
+ // //////////////////////// Arrange the bits of text
415
358
 
416
359
  var spaces = function spaces(n) {
417
360
  var s = '';
418
-
419
- for (var i = 0; i < n; i++) {
420
- s += ' ';
421
- }
422
-
361
+ for (var i = 0; i < n; i++) s += ' ';
423
362
  return s;
424
363
  };
425
-
426
364
  var treeToLine = function treeToLine(tree) {
427
365
  var str = '';
428
-
429
366
  for (var i = 0; i < tree.length; i++) {
430
367
  var branch = tree[i];
431
- var s2 = typeof branch === 'string' ? branch : treeToLine(branch); // Note the space before the dot in case statement ends with 123 or colon. which is in fact allowed but be conservative.
432
-
368
+ var s2 = typeof branch === 'string' ? branch : treeToLine(branch);
369
+ // Note the space before the dot in case statement ends with 123 or colon. which is in fact allowed but be conservative.
433
370
  if (i !== 0) {
434
371
  var ch = str.slice(-1) || ' ';
435
-
436
- if (s2 === ',' || s2 === ';') {// no gap
372
+ if (s2 === ',' || s2 === ';') {
373
+ // no gap
437
374
  } else if (s2 === '.' && !'0123456789.:'.includes(ch)) {// no gap except after number and colon
438
375
  // no gap
439
376
  } else {
@@ -443,69 +380,55 @@ export var Serializer = /*#__PURE__*/function () {
443
380
 
444
381
  str += s2;
445
382
  }
446
-
447
383
  return str;
448
- }; // Convert a nested tree of lists and strings to a string
449
-
384
+ };
450
385
 
386
+ // Convert a nested tree of lists and strings to a string
451
387
  var treeToString = function treeToString(tree, level) {
452
388
  var str = '';
453
389
  var lastLength = 100000;
454
390
  if (level === undefined) level = -1;
455
-
456
391
  for (var i = 0; i < tree.length; i++) {
457
392
  var branch = tree[i];
458
-
459
393
  if (typeof branch !== 'string') {
460
394
  var substr = treeToString(branch, level + 1);
461
-
462
395
  if (substr.length < 10 * (width - indent * level) && substr.indexOf('"""') < 0) {
463
396
  // Don't mess up multiline strings
464
397
  var line = treeToLine(branch);
465
-
466
398
  if (line.length < width - indent * level) {
467
399
  branch = line; // Note! treat as string below
468
-
469
400
  substr = '';
470
401
  }
471
402
  }
472
-
473
403
  if (substr) lastLength = 10000;
474
404
  str += substr;
475
405
  }
476
-
477
406
  if (typeof branch === 'string') {
478
407
  if (branch.length === 1 && str.slice(-1) === '\n') {
479
408
  if (',.;'.indexOf(branch) >= 0) {
480
- str = str.slice(0, -1); // be conservative and ensure a whitespace between some chars and a final dot, as in treeToLine above
481
-
409
+ str = str.slice(0, -1);
410
+ // be conservative and ensure a whitespace between some chars and a final dot, as in treeToLine above
482
411
  if (branch == '.' && '0123456789.:'.includes(str.charAt(str.length - 1))) {
483
412
  str += ' ';
484
413
  lastLength += 1;
485
414
  }
486
-
487
415
  str += branch + '\n'; // slip punct'n on end
488
-
489
416
  lastLength += 1;
490
417
  continue;
491
418
  }
492
419
  }
493
-
494
- if (lastLength < indent * level + 4 || // if new line not necessary
420
+ if (lastLength < indent * level + 4 ||
421
+ // if new line not necessary
495
422
  lastLength + branch.length + 1 < width && ';.'.indexOf(str[str.length - 2]) < 0) {
496
423
  // or the string fits on last line
497
424
  str = str.slice(0, -1) + ' ' + branch + '\n'; // then continue on this line
498
-
499
425
  lastLength += branch.length + 1;
500
426
  } else {
501
427
  var _line = spaces(indent * level) + branch;
502
-
503
428
  str += _line + '\n';
504
429
  lastLength = _line.length;
505
-
506
430
  if (level < 0) {
507
431
  str += '\n'; // extra blank line
508
-
509
432
  lastLength = 100000; // don't touch
510
433
  }
511
434
  }
@@ -513,48 +436,41 @@ export var Serializer = /*#__PURE__*/function () {
513
436
  }
514
437
 
515
438
  return str;
516
- }; // //////////////////////////////////////////// Structure for N3
517
- // Convert a set of statements into a nested tree of lists and strings
518
-
439
+ };
519
440
 
441
+ // //////////////////////////////////////////// Structure for N3
442
+ // Convert a set of statements into a nested tree of lists and strings
520
443
  function statementListToTreeMethod(statements) {
521
444
  var stats = this.rootSubjects(statements);
522
445
  var roots = stats.roots;
523
446
  var results = [];
524
-
525
447
  for (var i = 0; i < roots.length; i++) {
526
448
  var root = roots[i];
527
449
  results.push(subjectTree(root, stats));
528
450
  }
529
-
530
451
  return results;
531
452
  }
453
+ var statementListToTree = statementListToTreeMethod.bind(this);
532
454
 
533
- var statementListToTree = statementListToTreeMethod.bind(this); // The tree for a subject
534
-
455
+ // The tree for a subject
535
456
  function subjectTree(subject, stats) {
536
457
  if (subject.termType === 'BlankNode' && !stats.incoming[subject]) {
537
458
  return objectTree(subject, stats, true).concat(['.']); // Anonymous bnode subject
538
459
  }
539
460
 
540
461
  return [termToN3(subject, stats)].concat([propertyTree(subject, stats)]).concat(['.']);
541
- } // The property tree for a single subject or anonymous node
542
-
543
-
462
+ }
463
+ // The property tree for a single subject or anonymous node
544
464
  function propertyTreeMethod(subject, stats) {
545
465
  var results = [];
546
466
  var lastPred = null;
547
467
  var sts = stats.subjects[this.toStr(subject)] || []; // relevant statements
548
-
549
468
  if (typeof sts === 'undefined') {
550
469
  throw new Error('Cant find statements for ' + subject);
551
470
  }
552
-
553
471
  var objects = [];
554
-
555
472
  for (var i = 0; i < sts.length; i++) {
556
473
  var st = sts[i];
557
-
558
474
  if (st.predicate.uri === lastPred) {
559
475
  objects.push(',');
560
476
  } else {
@@ -562,20 +478,15 @@ export var Serializer = /*#__PURE__*/function () {
562
478
  results = results.concat([objects]).concat([';']);
563
479
  objects = [];
564
480
  }
565
-
566
481
  results.push(predMap[st.predicate.uri] ? predMap[st.predicate.uri] : termToN3(st.predicate, stats));
567
482
  }
568
-
569
483
  lastPred = st.predicate.uri;
570
484
  objects.push(objectTree(st.object, stats));
571
485
  }
572
-
573
486
  results = results.concat([objects]);
574
487
  return results;
575
488
  }
576
-
577
489
  var propertyTree = propertyTreeMethod.bind(this);
578
-
579
490
  function objectTreeMethod(obj, stats, force) {
580
491
  if (obj.termType === 'BlankNode' && (force || stats.rootsHash[obj.toNT()] === undefined)) {
581
492
  // if not a root
@@ -585,63 +496,50 @@ export var Serializer = /*#__PURE__*/function () {
585
496
  return '[]';
586
497
  }
587
498
  }
588
-
589
499
  return termToN3(obj, stats);
590
500
  }
591
-
592
501
  var objectTree = objectTreeMethod.bind(this);
593
-
594
502
  function termToN3Method(expr, stats) {
595
503
  //
596
504
  var i, res;
597
-
598
505
  switch (expr.termType) {
599
506
  case 'Graph':
600
507
  res = ['{'];
601
508
  res = res.concat(statementListToTree(expr.statements));
602
509
  return res.concat(['}']);
603
-
604
510
  case 'Collection':
605
511
  res = ['('];
606
-
607
512
  for (i = 0; i < expr.elements.length; i++) {
608
513
  res.push([objectTree(expr.elements[i], stats)]);
609
514
  }
610
-
611
515
  res.push(')');
612
516
  return res;
613
-
614
517
  default:
615
518
  return this.atomicTermToN3(expr);
616
519
  }
617
520
  }
618
-
619
521
  Serializer.prototype.termToN3 = termToN3;
620
522
  var termToN3 = termToN3Method.bind(this);
621
-
622
523
  function prefixDirectivesMethod() {
623
524
  var str = '';
624
-
625
525
  if (this.defaultNamespace) {
626
526
  str += '@prefix : ' + this.explicitURI(this.defaultNamespace) + '.\n';
627
527
  }
628
-
629
528
  for (var ns in this.prefixes) {
630
529
  if (!this.prefixes.hasOwnProperty(ns)) continue;
631
530
  if (!this.namespacesUsed[ns]) continue;
632
531
  str += '@prefix ' + this.prefixes[ns] + ': ' + this.explicitURI(ns) + '.\n';
633
532
  }
634
-
635
533
  return str + '\n';
636
534
  }
637
-
638
- var prefixDirectives = prefixDirectivesMethod.bind(this); // Body of statementsToN3:
639
-
535
+ var prefixDirectives = prefixDirectivesMethod.bind(this);
536
+ // Body of statementsToN3:
640
537
  var tree = statementListToTree(sts);
641
538
  return prefixDirectives() + treeToString(tree);
642
- } // //////////////////////////////////////////// Atomic Terms
643
- // Deal with term level things and nesting with no bnode structure
539
+ }
540
+ // //////////////////////////////////////////// Atomic Terms
644
541
 
542
+ // Deal with term level things and nesting with no bnode structure
645
543
  }, {
646
544
  key: "atomicTermToN3",
647
545
  value: function atomicTermToN3(expr, stats) {
@@ -649,26 +547,21 @@ export var Serializer = /*#__PURE__*/function () {
649
547
  case 'BlankNode':
650
548
  case 'Variable':
651
549
  return expr.toNT();
652
-
653
550
  case 'Literal':
654
551
  var val = expr.value;
655
-
656
552
  if (typeof val !== 'string') {
657
553
  throw new TypeError('Value of RDF literal node must be a string');
658
- } // var val = expr.value.toString() // should be a string already
659
-
660
-
554
+ }
555
+ // var val = expr.value.toString() // should be a string already
661
556
  if (expr.datatype && this.flags.indexOf('x') < 0) {
662
557
  // Supress native numbers
663
558
  switch (expr.datatype.uri) {
664
559
  case 'http://www.w3.org/2001/XMLSchema#integer':
665
560
  return val;
666
-
667
561
  case 'http://www.w3.org/2001/XMLSchema#decimal':
668
562
  // In Turtle, must have dot
669
563
  if (val.indexOf('.') < 0) val += '.0';
670
564
  return val;
671
-
672
565
  case 'http://www.w3.org/2001/XMLSchema#double':
673
566
  {
674
567
  // Must force use of 'e'
@@ -677,33 +570,27 @@ export var Serializer = /*#__PURE__*/function () {
677
570
  if (!eNotation) val += 'e0';
678
571
  return val;
679
572
  }
680
-
681
573
  case 'http://www.w3.org/2001/XMLSchema#boolean':
682
574
  return expr.value === '1' ? 'true' : 'false';
683
575
  }
684
576
  }
685
-
686
577
  var str = this.stringToN3(expr.value);
687
-
688
578
  if (expr.language) {
689
579
  str += '@' + expr.language;
690
580
  } else if (!expr.datatype.equals(this.xsd.string)) {
691
581
  str += '^^' + this.atomicTermToN3(expr.datatype, stats);
692
582
  }
693
-
694
583
  return str;
695
-
696
584
  case 'NamedNode':
697
585
  return this.symbolToN3(expr);
698
-
699
586
  case 'DefaultGraph':
700
587
  return '';
701
-
702
588
  default:
703
589
  throw new Error('Internal: atomicTermToN3 cannot handle ' + expr + ' of termType: ' + expr.termType);
704
590
  }
705
- } // stringToN3: String escaping for N3
591
+ }
706
592
 
593
+ // stringToN3: String escaping for N3
707
594
  }, {
708
595
  key: "stringToN3",
709
596
  value: function stringToN3(str, flags) {
@@ -712,10 +599,12 @@ export var Serializer = /*#__PURE__*/function () {
712
599
  var i, j, k;
713
600
  var delim;
714
601
  var forbidden;
715
-
716
- if (str.length > 20 && // Long enough to make sense
717
- str.slice(-1) !== '"' && // corner case'
718
- flags.indexOf('n') < 0 && ( // Force single line
602
+ if (str.length > 20 &&
603
+ // Long enough to make sense
604
+ str.slice(-1) !== '"' &&
605
+ // corner case'
606
+ flags.indexOf('n') < 0 && (
607
+ // Force single line
719
608
  str.indexOf('\n') > 0 || str.indexOf('"') > 0)) {
720
609
  delim = '"""';
721
610
  forbidden = this.forbidden3;
@@ -723,7 +612,6 @@ export var Serializer = /*#__PURE__*/function () {
723
612
  delim = '"';
724
613
  forbidden = this.forbidden1;
725
614
  }
726
-
727
615
  for (i = 0; i < str.length;) {
728
616
  forbidden.lastIndex = 0;
729
617
  var m = forbidden.exec(str.slice(i));
@@ -731,12 +619,10 @@ export var Serializer = /*#__PURE__*/function () {
731
619
  j = i + forbidden.lastIndex - 1;
732
620
  res += str.slice(i, j);
733
621
  var ch = str[j];
734
-
735
622
  if (ch === '"' && delim === '"""' && str.slice(j, j + 3) !== '"""') {
736
623
  res += ch;
737
624
  } else {
738
625
  k = '\b\f\r\t\v\n\\"'.indexOf(ch); // No escaping of bell (7)?
739
-
740
626
  if (k >= 0) {
741
627
  res += '\\' + 'bfrtvn\\"'[k];
742
628
  } else {
@@ -749,28 +635,24 @@ export var Serializer = /*#__PURE__*/function () {
749
635
  }
750
636
  }
751
637
  }
752
-
753
638
  i = j + 1;
754
639
  }
755
-
756
640
  return delim + res + str.slice(i) + delim;
757
- } // A single symbol, either in <> or namespace notation
758
-
641
+ }
642
+ // A single symbol, either in <> or namespace notation
759
643
  }, {
760
644
  key: "symbolToN3",
761
645
  value: function symbolToN3(x) {
762
646
  // c.f. symbolString() in notation3.py
763
647
  var uri = x.uri;
764
648
  var j = uri.indexOf('#');
765
-
766
649
  if (j < 0 && this.flags.indexOf('/') < 0) {
767
650
  j = uri.lastIndexOf('/');
768
651
  }
769
-
770
- if (j >= 0 && this.flags.indexOf('p') < 0 && ( // Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?)
652
+ if (j >= 0 && this.flags.indexOf('p') < 0 && (
653
+ // Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?)
771
654
  uri.indexOf('http') === 0 || uri.indexOf('ws') === 0 || uri.indexOf('file') === 0)) {
772
655
  var canSplit = true;
773
-
774
656
  for (var k = j + 1; k < uri.length; k++) {
775
657
  if (this._notNameChars.indexOf(uri[k]) >= 0) {
776
658
  canSplit = false;
@@ -786,61 +668,61 @@ export var Serializer = /*#__PURE__*/function () {
786
668
  }
787
669
  }
788
670
  */
789
-
790
-
791
671
  if (canSplit) {
792
672
  var localid = uri.slice(j + 1);
793
673
  var namesp = uri.slice(0, j + 1);
794
-
795
674
  if (this.defaultNamespace && this.defaultNamespace === namesp && this.flags.indexOf('d') < 0) {
796
675
  // d -> suppress default
797
676
  if (this.flags.indexOf('k') >= 0 && this.keyords.indexOf(localid) < 0) {
798
677
  return localid;
799
678
  }
800
-
801
679
  return ':' + localid;
802
- } // this.checkIntegrity() // @@@ Remove when not testing
803
-
804
-
680
+ }
681
+ // this.checkIntegrity() // @@@ Remove when not testing
805
682
  var prefix = this.prefixes[namesp];
806
683
  if (!prefix) prefix = this.makeUpPrefix(namesp);
807
-
808
684
  if (prefix) {
809
685
  this.namespacesUsed[namesp] = true;
810
686
  return prefix + ':' + localid;
811
- } // Fall though if can't do qname
812
-
687
+ }
688
+ // Fall though if can't do qname
813
689
  }
814
690
  }
815
691
 
816
692
  return this.explicitURI(uri);
817
- } // /////////////////////////// Quad store serialization
693
+ }
694
+
695
+ // /////////////////////////// Quad store serialization
696
+
818
697
  // @para. write - a function taking a single string to be output
819
698
  //
820
-
821
699
  }, {
822
700
  key: "writeStore",
823
701
  value: function writeStore(write) {
824
702
  var kb = this.store;
825
703
  var fetcher = kb.fetcher;
826
- var session = fetcher && fetcher.appNode; // The core data
704
+ var session = fetcher && fetcher.appNode;
827
705
 
828
- var sources = this.store.index[3];
706
+ // The core data
829
707
 
708
+ var sources = this.store.index[3];
830
709
  for (var s in sources) {
831
710
  // -> assume we can use -> as short for log:semantics
832
711
  var source = kb.fromNT(s);
833
712
  if (session && source.equals(session)) continue;
834
713
  write('\n' + this.atomicTermToN3(source) + ' ' + this.atomicTermToN3(kb.sym('http://www.w3.org/2000/10/swap/log#semantics')) + ' { ' + this.statementsToN3(kb.statementsMatching(undefined, undefined, undefined, source)) + ' }.\n');
835
- } // The metadata from HTTP interactions:
714
+ }
836
715
 
716
+ // The metadata from HTTP interactions:
837
717
 
838
718
  kb.statementsMatching(undefined, kb.sym('http://www.w3.org/2007/ont/link#requestedURI')).map(function (st) {
839
719
  write('\n<' + st.object.value + '> log:metadata {\n');
840
720
  var sts = kb.statementsMatching(undefined, undefined, undefined, st.subject);
841
721
  write(this.statementsToN3(this.statementsToN3(sts)));
842
722
  write('}.\n');
843
- }); // Inferences we have made ourselves not attributable to anyone else
723
+ });
724
+
725
+ // Inferences we have made ourselves not attributable to anyone else
844
726
 
845
727
  var metaSources = [];
846
728
  if (session) metaSources.push(session);
@@ -849,69 +731,56 @@ export var Serializer = /*#__PURE__*/function () {
849
731
  metadata = metadata.concat(kb.statementsMatching(undefined, undefined, undefined, source));
850
732
  });
851
733
  write(this.statementsToN3(metadata));
852
- } // ////////////////////////////////////////////// XML serialization
734
+ }
853
735
 
736
+ // ////////////////////////////////////////////// XML serialization
854
737
  }, {
855
738
  key: "statementsToXML",
856
739
  value: function statementsToXML(sts) {
857
740
  var indent = 4;
858
741
  var width = 80;
859
742
  var namespaceCounts = []; // which have been used
860
-
861
743
  namespaceCounts['http://www.w3.org/1999/02/22-rdf-syntax-ns#'] = true;
862
744
  var liPrefix = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_'; // prefix for ordered list items
745
+
863
746
  // //////////////////////// Arrange the bits of XML text
864
747
 
865
748
  var spaces = function spaces(n) {
866
749
  var s = '';
867
-
868
- for (var i = 0; i < n; i++) {
869
- s += ' ';
870
- }
871
-
750
+ for (var i = 0; i < n; i++) s += ' ';
872
751
  return s;
873
752
  };
874
-
875
753
  var XMLtreeToLine = function XMLtreeToLine(tree) {
876
754
  var str = '';
877
-
878
755
  for (var i = 0; i < tree.length; i++) {
879
756
  var branch = tree[i];
880
757
  var s2 = typeof branch === 'string' ? branch : XMLtreeToLine(branch);
881
758
  str += s2;
882
759
  }
883
-
884
760
  return str;
885
- }; // Convert a nested tree of lists and strings to a string
886
-
761
+ };
887
762
 
763
+ // Convert a nested tree of lists and strings to a string
888
764
  var XMLtreeToString = function XMLtreeToString(tree, level) {
889
765
  var str = '';
890
766
  var line;
891
767
  var lastLength = 100000;
892
768
  if (!level) level = 0;
893
-
894
769
  for (var i = 0; i < tree.length; i++) {
895
770
  var branch = tree[i];
896
-
897
771
  if (typeof branch !== 'string') {
898
772
  var substr = XMLtreeToString(branch, level + 1);
899
-
900
773
  if (substr.length < 10 * (width - indent * level) && substr.indexOf('"""') < 0) {
901
774
  // Don't mess up multiline strings
902
775
  line = XMLtreeToLine(branch);
903
-
904
776
  if (line.length < width - indent * level) {
905
777
  branch = ' ' + line; // @@ Hack: treat as string below
906
-
907
778
  substr = '';
908
779
  }
909
780
  }
910
-
911
781
  if (substr) lastLength = 10000;
912
782
  str += substr;
913
783
  }
914
-
915
784
  if (typeof branch === 'string') {
916
785
  if (lastLength < indent * level + 4) {
917
786
  // continue
@@ -925,36 +794,28 @@ export var Serializer = /*#__PURE__*/function () {
925
794
  } else {// not string
926
795
  }
927
796
  }
928
-
929
797
  return str;
930
798
  };
931
-
932
799
  function statementListToXMLTreeMethod(statements) {
933
800
  this.suggestPrefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
934
801
  var stats = this.rootSubjects(statements);
935
802
  var roots = stats.roots;
936
803
  var results = [];
937
-
938
804
  for (var i = 0; i < roots.length; i++) {
939
805
  var root = roots[i];
940
806
  results.push(subjectXMLTree(root, stats));
941
807
  }
942
-
943
808
  return results;
944
809
  }
945
-
946
810
  var statementListToXMLTree = statementListToXMLTreeMethod.bind(this);
947
-
948
811
  function escapeForXML(str) {
949
812
  if (typeof str === 'undefined') return '@@@undefined@@@@';
950
813
  return str.replace(/[&<"]/g, function (m) {
951
814
  switch (m[0]) {
952
815
  case '&':
953
816
  return '&amp;';
954
-
955
817
  case '<':
956
818
  return '&lt;';
957
-
958
819
  case '"':
959
820
  return '&quot;';
960
821
  // '
@@ -965,73 +826,63 @@ export var Serializer = /*#__PURE__*/function () {
965
826
  function relURIMethod(term) {
966
827
  return escapeForXML(this.base ? Util.uri.refTo(this.base, term.uri) : term.uri);
967
828
  }
829
+ var relURI = relURIMethod.bind(this);
968
830
 
969
- var relURI = relURIMethod.bind(this); // The tree for a subject
970
-
831
+ // The tree for a subject
971
832
  function subjectXMLTreeMethod(subject, stats) {
972
833
  var results = [];
973
834
  var type, t, st, pred;
974
835
  var sts = stats.subjects[this.toStr(subject)]; // relevant statements
975
-
976
836
  if (typeof sts === 'undefined') {
977
837
  // empty bnode
978
838
  return propertyXMLTree(subject, stats);
979
- } // Sort only on the predicate, leave the order at object
839
+ }
840
+
841
+ // Sort only on the predicate, leave the order at object
980
842
  // level undisturbed. This leaves multilingual content in
981
843
  // the order of entry (for partner literals), which helps
982
844
  // readability.
983
845
  //
984
846
  // For the predicate sort, we attempt to split the uri
985
847
  // as a hint to the sequence
986
-
987
-
988
848
  sts.sort(function (a, b) {
989
849
  var ap = a.predicate.uri;
990
850
  var bp = b.predicate.uri;
991
-
992
851
  if (ap.substring(0, liPrefix.length) === liPrefix || bp.substring(0, liPrefix.length) === liPrefix) {
993
852
  // we're only interested in sorting list items
994
853
  return ap.localeCompare(bp);
995
854
  }
996
-
997
855
  var as = ap.substring(liPrefix.length);
998
856
  var bs = bp.substring(liPrefix.length);
999
857
  var an = parseInt(as, 10);
1000
858
  var bn = parseInt(bs, 10);
1001
-
1002
859
  if (isNaN(an) || isNaN(bn) || an !== as || bn !== bs) {
1003
860
  // we only care about integers
1004
861
  return ap.localeCompare(bp);
1005
862
  }
1006
-
1007
863
  return an - bn;
1008
864
  });
1009
-
1010
865
  for (var i = 0; i < sts.length; i++) {
1011
- st = sts[i]; // look for a type
1012
-
866
+ st = sts[i];
867
+ // look for a type
1013
868
  if (st.predicate.uri === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' && !type && st.object.termType === 'NamedNode') {
1014
869
  type = st.object;
1015
870
  continue; // don't include it as a child element
1016
- } // see whether predicate can be replaced with "li"
1017
-
871
+ }
1018
872
 
873
+ // see whether predicate can be replaced with "li"
1019
874
  pred = st.predicate;
1020
-
1021
875
  if (pred.uri.substr(0, liPrefix.length) === liPrefix) {
1022
- var number = pred.uri.substr(liPrefix.length); // make sure these are actually numeric list items
1023
-
876
+ var number = pred.uri.substr(liPrefix.length);
877
+ // make sure these are actually numeric list items
1024
878
  var intNumber = parseInt(number, 10);
1025
-
1026
879
  if (number === intNumber.toString()) {
1027
880
  // was numeric; don't need to worry about ordering since we've already
1028
881
  // sorted the statements
1029
882
  pred = this.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#li');
1030
883
  }
1031
884
  }
1032
-
1033
885
  t = qname(pred);
1034
-
1035
886
  switch (st.object.termType) {
1036
887
  case 'BlankNode':
1037
888
  if (stats.incoming[st.object].length === 1) {
@@ -1040,30 +891,23 @@ export var Serializer = /*#__PURE__*/function () {
1040
891
  } else {
1041
892
  results = results.concat(['<' + t + ' rdf:nodeID="' + st.object.toNT().slice(2) + '"/>']);
1042
893
  }
1043
-
1044
894
  break;
1045
-
1046
895
  case 'NamedNode':
1047
896
  results = results.concat(['<' + t + ' rdf:resource="' + relURI(st.object) + '"/>']);
1048
897
  break;
1049
-
1050
898
  case 'Literal':
1051
899
  results = results.concat(['<' + t + (st.object.datatype.equals(this.xsd.string) ? '' : ' rdf:datatype="' + escapeForXML(st.object.datatype.uri) + '"') + (st.object.language ? ' xml:lang="' + st.object.language + '"' : '') + '>' + escapeForXML(st.object.value) + '</' + t + '>']);
1052
900
  break;
1053
-
1054
901
  case 'Collection':
1055
902
  results = results.concat(['<' + t + ' rdf:parseType="Collection">', collectionXMLTree(st.object, stats), '</' + t + '>']);
1056
903
  break;
1057
-
1058
904
  default:
1059
905
  throw new Error("Can't serialize object of type " + st.object.termType + ' into XML');
1060
906
  } // switch
1061
-
1062
907
  }
1063
908
 
1064
909
  var tag = type ? qname(type) : 'rdf:Description';
1065
910
  var attrs = '';
1066
-
1067
911
  if (subject.termType === 'BlankNode') {
1068
912
  if (!stats.incoming[subject] || stats.incoming[subject].length !== 1) {
1069
913
  // not an anonymous bnode
@@ -1072,34 +916,25 @@ export var Serializer = /*#__PURE__*/function () {
1072
916
  } else {
1073
917
  attrs = ' rdf:about="' + relURI(subject) + '"';
1074
918
  }
1075
-
1076
919
  return ['<' + tag + attrs + '>'].concat([results]).concat(['</' + tag + '>']);
1077
920
  }
1078
-
1079
921
  var subjectXMLTree = subjectXMLTreeMethod.bind(this);
1080
-
1081
922
  function collectionXMLTree(subject, stats) {
1082
923
  var res = [];
1083
-
1084
924
  for (var i = 0; i < subject.elements.length; i++) {
1085
925
  res.push(subjectXMLTree(subject.elements[i], stats));
1086
926
  }
1087
-
1088
927
  return res;
1089
- } // The property tree for a single subject or anonymos node
1090
-
928
+ }
1091
929
 
930
+ // The property tree for a single subject or anonymos node
1092
931
  function propertyXMLTreeMethod(subject, stats) {
1093
932
  var results = [];
1094
933
  var sts = stats.subjects[this.toStr(subject)]; // relevant statements
1095
-
1096
934
  if (!sts) return results; // No relevant statements
1097
-
1098
935
  sts.sort();
1099
-
1100
936
  for (var i = 0; i < sts.length; i++) {
1101
937
  var st = sts[i];
1102
-
1103
938
  switch (st.object.termType) {
1104
939
  case 'BlankNode':
1105
940
  if (stats.rootsHash[st.object.toNT()]) {
@@ -1108,101 +943,103 @@ export var Serializer = /*#__PURE__*/function () {
1108
943
  } else {
1109
944
  results = results.concat(['<' + qname(st.predicate) + ' rdf:parseType="Resource">', propertyXMLTree(st.object, stats), '</' + qname(st.predicate) + '>']);
1110
945
  }
1111
-
1112
946
  break;
1113
-
1114
947
  case 'NamedNode':
1115
948
  results = results.concat(['<' + qname(st.predicate) + ' rdf:resource="' + relURI(st.object) + '"/>']);
1116
949
  break;
1117
-
1118
950
  case 'Literal':
1119
951
  results = results.concat(['<' + qname(st.predicate) + (st.object.datatype.equals(this.xsd.string) ? '' : ' rdf:datatype="' + escapeForXML(st.object.datatype.value) + '"') + (st.object.language ? ' xml:lang="' + st.object.language + '"' : '') + '>' + escapeForXML(st.object.value) + '</' + qname(st.predicate) + '>']);
1120
952
  break;
1121
-
1122
953
  case 'Collection':
1123
954
  results = results.concat(['<' + qname(st.predicate) + ' rdf:parseType="Collection">', collectionXMLTree(st.object, stats), '</' + qname(st.predicate) + '>']);
1124
955
  break;
1125
-
1126
956
  default:
1127
957
  throw new Error("Can't serialize object of type " + st.object.termType + ' into XML');
1128
958
  } // switch
1129
-
1130
959
  }
1131
960
 
1132
961
  return results;
1133
962
  }
1134
-
1135
963
  var propertyXMLTree = propertyXMLTreeMethod.bind(this);
1136
-
1137
964
  function qnameMethod(term) {
1138
965
  var uri = term.uri;
1139
966
  var j = uri.indexOf('#');
1140
-
1141
967
  if (j < 0 && this.flags.indexOf('/') < 0) {
1142
968
  j = uri.lastIndexOf('/');
1143
969
  }
1144
-
1145
970
  if (j < 0) throw new Error('Cannot make qname out of <' + uri + '>');
1146
-
1147
971
  for (var k = j + 1; k < uri.length; k++) {
1148
972
  if (this._notNameChars.indexOf(uri[k]) >= 0) {
1149
973
  throw new Error('Invalid character "' + uri[k] + '" cannot be in XML qname for URI: ' + uri);
1150
974
  }
1151
975
  }
1152
-
1153
976
  var localid = uri.slice(j + 1);
1154
977
  var namesp = uri.slice(0, j + 1);
1155
-
1156
978
  if (this.defaultNamespace && this.defaultNamespace === namesp && this.flags.indexOf('d') < 0) {
1157
979
  // d -> suppress default
1158
980
  return localid;
1159
981
  }
1160
-
1161
982
  var prefix = this.prefixes[namesp];
1162
983
  if (!prefix) prefix = this.makeUpPrefix(namesp);
1163
984
  namespaceCounts[namesp] = true;
1164
985
  return prefix + ':' + localid;
1165
986
  }
987
+ var qname = qnameMethod.bind(this);
1166
988
 
1167
- var qname = qnameMethod.bind(this); // Body of toXML:
989
+ // Body of toXML:
1168
990
 
1169
991
  var tree = statementListToXMLTree(sts);
1170
992
  var str = '<rdf:RDF';
1171
-
1172
993
  if (this.defaultNamespace) {
1173
994
  str += ' xmlns="' + escapeForXML(this.defaultNamespace) + '"';
1174
995
  }
1175
-
1176
996
  for (var ns in namespaceCounts) {
1177
- if (!namespaceCounts.hasOwnProperty(ns)) continue; // Rel uris in xml ns is not strictly allowed in the XMLNS spec but needed in practice often
1178
-
997
+ if (!namespaceCounts.hasOwnProperty(ns)) continue;
998
+ // Rel uris in xml ns is not strictly allowed in the XMLNS spec but needed in practice often
1179
999
  var ns2 = this.base && this.flags.includes('z') ? Util.uri.refTo(this.base, ns) : ns;
1180
1000
  str += '\n xmlns:' + this.prefixes[ns] + '="' + escapeForXML(ns2) + '"';
1181
1001
  }
1182
-
1183
1002
  str += '>';
1184
1003
  var tree2 = [str, tree, '</rdf:RDF>']; // @@ namespace declrations
1185
-
1186
1004
  return XMLtreeToString(tree2, -1);
1187
1005
  } // End @@ body
1188
-
1006
+ }, {
1007
+ key: "statementsToJsonld",
1008
+ value: function statementsToJsonld(sts) {
1009
+ // ttl2jsonld creates context keys for all ttl prefix
1010
+ // context keys must be full IRI
1011
+ function findId(itemObj) {
1012
+ if (itemObj['@id']) {
1013
+ var item = itemObj['@id'].split(':');
1014
+ if (keys[item[0]]) itemObj['@id'] = jsonldObj['@context'][item[0]] + item[1];
1015
+ }
1016
+ var itemValues = Object.values(itemObj);
1017
+ for (var i in itemValues) {
1018
+ if (typeof itemValues[i] !== 'string') {
1019
+ // @list contains array
1020
+ findId(itemValues[i]);
1021
+ }
1022
+ }
1023
+ }
1024
+ var turtleDoc = this.statementsToN3(sts);
1025
+ var jsonldObj = ttl2jsonld.parse(turtleDoc);
1026
+ return JSON.stringify(jsonldObj, null, 2);
1027
+ }
1189
1028
  }]);
1190
-
1191
1029
  return Serializer;
1192
- }(); // String escaping utilities
1030
+ }();
1031
+
1032
+ // String escaping utilities
1193
1033
 
1194
1034
  function hexify(str) {
1195
1035
  // also used in parser
1196
1036
  return encodeURI(str);
1197
1037
  }
1198
-
1199
1038
  function backslashUify(str) {
1200
1039
  var res = '';
1201
1040
  var k;
1202
-
1203
1041
  for (var i = 0; i < str.length; i++) {
1204
1042
  k = str.charCodeAt(i);
1205
-
1206
1043
  if (k > 65535) {
1207
1044
  res += "\\U" + ('00000000' + k.toString(16)).slice(-8); // convert to upper?
1208
1045
  } else if (k > 126) {
@@ -1211,6 +1048,5 @@ function backslashUify(str) {
1211
1048
  res += str[i];
1212
1049
  }
1213
1050
  }
1214
-
1215
1051
  return res;
1216
1052
  }