rdflib 2.2.17 → 2.2.18-12f6a201

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/esm/formula.js CHANGED
@@ -22,7 +22,7 @@ import CanonicalDataFactory from './factories/canonical-data-factory';
22
22
  import log from './log';
23
23
  import Namespace from './namespace';
24
24
  import Node from './node-internal';
25
- import Serializer from './serialize';
25
+ import _serialize from './serialize';
26
26
  import { GraphTermType } from './types';
27
27
  import { isStatement } from './utils/terms';
28
28
  import Variable from './variable';
@@ -767,39 +767,14 @@ var Formula = /*#__PURE__*/function (_Node) {
767
767
  * @param base - The base string
768
768
  * @param contentType - The content type of the syntax to use
769
769
  * @param provenance - The provenance URI
770
+ * @param options - options to pass to the serializer, as defined in serialize method
770
771
  */
771
772
 
772
773
  }, {
773
774
  key: "serialize",
774
- value: function serialize(base, contentType, provenance) {
775
- var documentString;
776
- var sts;
777
- var sz;
778
- sz = Serializer(this);
779
- sz.suggestNamespaces(this.ns);
780
- sz.setBase(base);
781
-
782
- if (provenance) {
783
- sts = this.statementsMatching(void 0, void 0, void 0, provenance);
784
- } else {
785
- sts = this.statements;
786
- }
787
-
788
- switch (contentType != null ? contentType : 'text/n3') {
789
- case 'application/rdf+xml':
790
- documentString = sz.statementsToXML(sts);
791
- break;
792
-
793
- case 'text/n3':
794
- case 'text/turtle':
795
- documentString = sz.statementsToN3(sts);
796
- break;
797
-
798
- default:
799
- throw new Error('serialize: Content-type ' + contentType + ' not supported.');
800
- }
801
-
802
- return documentString;
775
+ value: function serialize(base, contentType, provenance, options) {
776
+ // delegate the graph serialization to the implementation in ./serialize
777
+ return _serialize(provenance, this, base, contentType, undefined, options);
803
778
  }
804
779
  /**
805
780
  * Creates a new formula with the substituting bindings applied
package/esm/index.js CHANGED
@@ -3,6 +3,7 @@ import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized
3
3
  import _inherits from "@babel/runtime/helpers/inherits";
4
4
  import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
5
5
  import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
6
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
6
7
 
7
8
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
8
9
 
@@ -83,6 +84,9 @@ export var ConnectedStore = /*#__PURE__*/function (_Store) {
83
84
  _classCallCheck(this, ConnectedStore);
84
85
 
85
86
  _this = _super.call(this, features);
87
+
88
+ _defineProperty(_assertThisInitialized(_this), "fetcher", void 0);
89
+
86
90
  _this.fetcher = new Fetcher(_assertThisInitialized(_this), {});
87
91
  return _this;
88
92
  }
@@ -100,6 +104,9 @@ export var LiveStore = /*#__PURE__*/function (_ConnectedStore) {
100
104
  _classCallCheck(this, LiveStore);
101
105
 
102
106
  _this2 = _super2.call(this, features);
107
+
108
+ _defineProperty(_assertThisInitialized(_this2), "updater", void 0);
109
+
103
110
  _this2.updater = new UpdateManager(_assertThisInitialized(_this2));
104
111
  return _this2;
105
112
  }
@@ -74,29 +74,44 @@ export default function jsonldParser(str, kb, base, callback) {
74
74
  base: baseString
75
75
  }).then(function (flattened) {
76
76
  return flattened.reduce(function (store, flatResource) {
77
- var id = flatResource['@id'] ? kb.rdfFactory.namedNode(flatResource['@id']) : kb.rdfFactory.blankNode();
77
+ kb = processResource(kb, base, flatResource);
78
+ return kb;
79
+ }, kb);
80
+ }).then(callback).catch(callback);
81
+ }
78
82
 
79
- for (var _i = 0, _Object$keys = Object.keys(flatResource); _i < _Object$keys.length; _i++) {
80
- var property = _Object$keys[_i];
83
+ function processResource(kb, base, flatResource) {
84
+ var id = flatResource['@id'] ? kb.rdfFactory.namedNode(flatResource['@id']) : kb.rdfFactory.blankNode();
81
85
 
82
- if (property === '@id') {
83
- continue;
84
- }
86
+ for (var _i = 0, _Object$keys = Object.keys(flatResource); _i < _Object$keys.length; _i++) {
87
+ var property = _Object$keys[_i];
85
88
 
86
- var value = flatResource[property];
89
+ if (property === '@id') {
90
+ continue;
91
+ } else if (property == '@graph') {
92
+ // the JSON-LD flattened structure may contain nested graphs
93
+ // the id value for this object is the new base (named graph id) for all nested flat resources
94
+ var graphId = id; // this is an array of resources
87
95
 
88
- if (Array.isArray(value)) {
89
- for (var i = 0; i < value.length; i++) {
90
- kb.addStatement(createStatement(kb, id, property, value[i], base));
91
- }
92
- } else {
93
- kb.addStatement(createStatement(kb, id, property, value, base));
94
- }
96
+ var nestedFlatResources = flatResource[property]; // recursively process all flat resources in the array, but with the graphId as base.
97
+
98
+ for (var i = 0; i < nestedFlatResources.length; i++) {
99
+ kb = processResource(kb, graphId, nestedFlatResources[i]);
95
100
  }
101
+ }
96
102
 
97
- return kb;
98
- }, kb);
99
- }).then(callback).catch(callback);
103
+ var value = flatResource[property];
104
+
105
+ if (Array.isArray(value)) {
106
+ for (var _i2 = 0; _i2 < value.length; _i2++) {
107
+ kb.addStatement(createStatement(kb, id, property, value[_i2], base));
108
+ }
109
+ } else {
110
+ kb.addStatement(createStatement(kb, id, property, value, base));
111
+ }
112
+ }
113
+
114
+ return kb;
100
115
  }
101
116
  /**
102
117
  * Create statement quad depending on @type being a type node
@@ -107,6 +122,7 @@ export default function jsonldParser(str, kb, base, callback) {
107
122
  * @return quad statement
108
123
  */
109
124
 
125
+
110
126
  function createStatement(kb, id, property, value, base) {
111
127
  var predicate, object;
112
128
 
@@ -252,7 +252,7 @@ var RDFParser = /*#__PURE__*/function () {
252
252
  }
253
253
  /**
254
254
  * Build our initial scope frame and parse the DOM into triples
255
- * @param {DOMTree} document The DOM to parse
255
+ * @param {HTMLDocument} document The DOM to parse
256
256
  * @param {String} base The base URL to use
257
257
  * @param {Object} why The context to which this resource belongs
258
258
  */
package/esm/serialize.js CHANGED
@@ -15,7 +15,7 @@ kb, base,
15
15
  * Defaults to Turtle.
16
16
  */
17
17
  contentType, callback, options) {
18
- base = base || target.value;
18
+ base = base || (target === null || target === void 0 ? void 0 : target.value);
19
19
  var opts = options || {};
20
20
  contentType = contentType || TurtleContentType; // text/n3 if complex?
21
21
 
@@ -25,8 +25,17 @@ contentType, callback, options) {
25
25
  var sz = Serializer(kb);
26
26
  if (opts.flags) sz.setFlags(opts.flags);
27
27
  var newSts = kb.statementsMatching(undefined, undefined, undefined, target);
28
- var n3String;
29
- sz.suggestNamespaces(kb.namespaces);
28
+ var n3String; // If an IndexedFormula, use the namespaces from the given graph as suggestions
29
+
30
+ if ('namespaces' in kb) {
31
+ sz.suggestNamespaces(kb.namespaces);
32
+ } // use the provided options.namespaces are mandatory prefixes
33
+
34
+
35
+ if (opts.namespaces) {
36
+ sz.setNamespaces(opts.namespaces);
37
+ }
38
+
30
39
  sz.setBase(base);
31
40
 
32
41
  switch (contentType) {
package/esm/serializer.js CHANGED
@@ -101,6 +101,50 @@ export var Serializer = /*#__PURE__*/function () {
101
101
 
102
102
  return this.store.fromNT(s);
103
103
  }
104
+ /**
105
+ * Defines a set of [prefix, namespace] pairs to be uised by this Serializer instance.
106
+ * Overrides previous prefixes if any
107
+ * @param namespaces
108
+ * @return {Serializer}
109
+ */
110
+
111
+ }, {
112
+ key: "setNamespaces",
113
+ value: function setNamespaces(namespaces) {
114
+ for (var px in namespaces) {
115
+ this.setPrefix(px, namespaces[px]);
116
+ }
117
+
118
+ return this;
119
+ }
120
+ /**
121
+ * Defines a namespace prefix, overriding any existing prefix for that URI
122
+ * @param prefix
123
+ * @param uri
124
+ */
125
+
126
+ }, {
127
+ key: "setPrefix",
128
+ value: function setPrefix(prefix, uri) {
129
+ if (prefix.slice(0, 7) === 'default') return; // Try to weed these out
130
+
131
+ if (prefix.slice(0, 2) === 'ns') return; // From others inferior algos
132
+
133
+ if (!prefix || !uri) return; // empty strings not suitable
134
+ // remove any existing prefix targeting this uri
135
+ // for (let existingPrefix in this.namespaces) {
136
+ // if (this.namespaces[existingPrefix] == uri)
137
+ // delete this.namespaces[existingPrefix];
138
+ // }
139
+ // remove any existing mapping for this prefix
140
+
141
+ for (var existingNs in this.prefixes) {
142
+ if (this.prefixes[existingNs] == prefix) delete this.prefixes[existingNs];
143
+ }
144
+
145
+ this.prefixes[uri] = prefix;
146
+ this.namespaces[prefix] = uri;
147
+ }
104
148
  /* Accumulate Namespaces
105
149
  **
106
150
  ** These are only hints. If two overlap, only one gets used
@@ -384,13 +428,13 @@ export var Serializer = /*#__PURE__*/function () {
384
428
 
385
429
  for (var i = 0; i < tree.length; i++) {
386
430
  var branch = tree[i];
387
- var s2 = typeof branch === 'string' ? branch : treeToLine(branch); // Note the space before the dot in case statement ends 123. which is in fact allowed but be conservative.
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.
388
432
 
389
433
  if (i !== 0) {
390
434
  var ch = str.slice(-1) || ' ';
391
435
 
392
436
  if (s2 === ',' || s2 === ';') {// no gap
393
- } else if (s2 === '.' && !'0123456789.'.includes(ch)) {// no gap except after number
437
+ } else if (s2 === '.' && !'0123456789.:'.includes(ch)) {// no gap except after number and colon
394
438
  // no gap
395
439
  } else {
396
440
  str += ' '; // separate from previous token
@@ -433,7 +477,14 @@ export var Serializer = /*#__PURE__*/function () {
433
477
  if (typeof branch === 'string') {
434
478
  if (branch.length === 1 && str.slice(-1) === '\n') {
435
479
  if (',.;'.indexOf(branch) >= 0) {
436
- str = str.slice(0, -1) + branch + '\n'; // slip punct'n on end
480
+ str = str.slice(0, -1); // be conservative and ensure a whitespace between some chars and a final dot, as in treeToLine above
481
+
482
+ if (branch == '.' && '0123456789.:'.includes(str.charAt(str.length - 1))) {
483
+ str += ' ';
484
+ lastLength += 1;
485
+ }
486
+
487
+ str += branch + '\n'; // slip punct'n on end
437
488
 
438
489
  lastLength += 1;
439
490
  continue;
package/esm/store.js CHANGED
@@ -6,6 +6,10 @@ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstruct
6
6
  import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
7
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
8
 
9
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
10
+
11
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
12
+
9
13
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
10
14
 
11
15
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -46,6 +50,7 @@ import { Query, indexedFormulaQuery } from './query';
46
50
  import { BlankNodeTermType, CollectionTermType, DefaultGraphTermType, EmptyTermType, GraphTermType, LiteralTermType, NamedNodeTermType, VariableTermType } from './types';
47
51
  import NamedNode from './named-node';
48
52
  import { namedNode } from './index';
53
+ import _serialize from "./serialize";
49
54
  import BlankNode from './blank-node';
50
55
  import DefaultGraph from './default-graph';
51
56
  import Literal from './literal';
@@ -1158,6 +1163,11 @@ var IndexedFormula = /*#__PURE__*/function (_Formula) {
1158
1163
 
1159
1164
  if (prefix.slice(0, 2) === 'ns' || prefix.slice(0, 7) === 'default') {
1160
1165
  return;
1166
+ } // remove any prefix that currently targets nsuri
1167
+
1168
+
1169
+ for (var existingPrefix in this.namespaces) {
1170
+ if (this.namespaces[existingPrefix] == nsuri) delete this.namespaces[existingPrefix];
1161
1171
  }
1162
1172
 
1163
1173
  this.namespaces[prefix] = nsuri;
@@ -1290,6 +1300,20 @@ var IndexedFormula = /*#__PURE__*/function (_Formula) {
1290
1300
 
1291
1301
  return res;
1292
1302
  }
1303
+ }, {
1304
+ key: "serialize",
1305
+ value: function serialize(base, contentType, provenance, options) {
1306
+ var _options;
1307
+
1308
+ // override Formula.serialize to force the serializer namespace prefixes
1309
+ // to those of this IndexedFormula
1310
+ // if namespaces are explicitly passed in options, let them override the existing namespaces in this formula
1311
+ var namespaces = (_options = options) !== null && _options !== void 0 && _options.namespaces ? _objectSpread(_objectSpread({}, this.namespaces), options.namespaces) : _objectSpread({}, this.namespaces);
1312
+ options = _objectSpread(_objectSpread({}, options || {}), {}, {
1313
+ namespaces: namespaces
1314
+ });
1315
+ return _serialize(provenance, this, base, contentType, undefined, options);
1316
+ }
1293
1317
  }], [{
1294
1318
  key: "defaultGraphURI",
1295
1319
  get: function get() {
package/lib/formula.d.ts CHANGED
@@ -268,8 +268,9 @@ export default class Formula extends Node {
268
268
  * @param base - The base string
269
269
  * @param contentType - The content type of the syntax to use
270
270
  * @param provenance - The provenance URI
271
+ * @param options - options to pass to the serializer, as defined in serialize method
271
272
  */
272
- serialize(base: any, contentType: any, provenance: any): any;
273
+ serialize(base: any, contentType: any, provenance: any, options?: any): string | undefined;
273
274
  /**
274
275
  * Creates a new formula with the substituting bindings applied
275
276
  * @param bindings - The bindings to substitute
package/lib/formula.js CHANGED
@@ -33,7 +33,7 @@ var _namespace = _interopRequireDefault(require("./namespace"));
33
33
 
34
34
  var _nodeInternal = _interopRequireDefault(require("./node-internal"));
35
35
 
36
- var _serialize = _interopRequireDefault(require("./serialize"));
36
+ var _serialize2 = _interopRequireDefault(require("./serialize"));
37
37
 
38
38
  var _types = require("./types");
39
39
 
@@ -784,39 +784,14 @@ var Formula = /*#__PURE__*/function (_Node) {
784
784
  * @param base - The base string
785
785
  * @param contentType - The content type of the syntax to use
786
786
  * @param provenance - The provenance URI
787
+ * @param options - options to pass to the serializer, as defined in serialize method
787
788
  */
788
789
 
789
790
  }, {
790
791
  key: "serialize",
791
- value: function serialize(base, contentType, provenance) {
792
- var documentString;
793
- var sts;
794
- var sz;
795
- sz = (0, _serialize.default)(this);
796
- sz.suggestNamespaces(this.ns);
797
- sz.setBase(base);
798
-
799
- if (provenance) {
800
- sts = this.statementsMatching(void 0, void 0, void 0, provenance);
801
- } else {
802
- sts = this.statements;
803
- }
804
-
805
- switch (contentType != null ? contentType : 'text/n3') {
806
- case 'application/rdf+xml':
807
- documentString = sz.statementsToXML(sts);
808
- break;
809
-
810
- case 'text/n3':
811
- case 'text/turtle':
812
- documentString = sz.statementsToN3(sts);
813
- break;
814
-
815
- default:
816
- throw new Error('serialize: Content-type ' + contentType + ' not supported.');
817
- }
818
-
819
- return documentString;
792
+ value: function serialize(base, contentType, provenance, options) {
793
+ // delegate the graph serialization to the implementation in ./serialize
794
+ return (0, _serialize2.default)(provenance, this, base, contentType, undefined, options);
820
795
  }
821
796
  /**
822
797
  * Creates a new formula with the substituting bindings applied
package/lib/index.d.ts CHANGED
@@ -39,8 +39,10 @@ export { BlankNode, Collection, convert, DataFactory, Empty, Fetcher, Formula, S
39
39
  NextId, fromNT, fetcher, graph, lit, st, namedNode as sym, blankNode, defaultGraph, literal, namedNode, quad, triple, variable, };
40
40
  export { termValue } from './utils/termValue';
41
41
  export declare class ConnectedStore extends Store {
42
+ fetcher: Fetcher;
42
43
  constructor(features: any);
43
44
  }
44
45
  export declare class LiveStore extends ConnectedStore {
46
+ updater: UpdateManager;
45
47
  constructor(features: any);
46
48
  }
package/lib/index.js CHANGED
@@ -260,6 +260,8 @@ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime
260
260
 
261
261
  var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
262
262
 
263
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
264
+
263
265
  var _blankNode = _interopRequireDefault(require("./blank-node"));
264
266
 
265
267
  var _collection = _interopRequireDefault(require("./collection"));
@@ -403,6 +405,7 @@ var ConnectedStore = /*#__PURE__*/function (_Store) {
403
405
 
404
406
  (0, _classCallCheck2.default)(this, ConnectedStore);
405
407
  _this = _super.call(this, features);
408
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fetcher", void 0);
406
409
  _this.fetcher = new _fetcher.default((0, _assertThisInitialized2.default)(_this), {});
407
410
  return _this;
408
411
  }
@@ -422,6 +425,7 @@ var LiveStore = /*#__PURE__*/function (_ConnectedStore) {
422
425
 
423
426
  (0, _classCallCheck2.default)(this, LiveStore);
424
427
  _this2 = _super2.call(this, features);
428
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "updater", void 0);
425
429
  _this2.updater = new _updateManager.default((0, _assertThisInitialized2.default)(_this2));
426
430
  return _this2;
427
431
  }
@@ -86,29 +86,44 @@ function jsonldParser(str, kb, base, callback) {
86
86
  base: baseString
87
87
  }).then(function (flattened) {
88
88
  return flattened.reduce(function (store, flatResource) {
89
- var id = flatResource['@id'] ? kb.rdfFactory.namedNode(flatResource['@id']) : kb.rdfFactory.blankNode();
89
+ kb = processResource(kb, base, flatResource);
90
+ return kb;
91
+ }, kb);
92
+ }).then(callback).catch(callback);
93
+ }
90
94
 
91
- for (var _i = 0, _Object$keys = Object.keys(flatResource); _i < _Object$keys.length; _i++) {
92
- var property = _Object$keys[_i];
95
+ function processResource(kb, base, flatResource) {
96
+ var id = flatResource['@id'] ? kb.rdfFactory.namedNode(flatResource['@id']) : kb.rdfFactory.blankNode();
93
97
 
94
- if (property === '@id') {
95
- continue;
96
- }
98
+ for (var _i = 0, _Object$keys = Object.keys(flatResource); _i < _Object$keys.length; _i++) {
99
+ var property = _Object$keys[_i];
97
100
 
98
- var value = flatResource[property];
101
+ if (property === '@id') {
102
+ continue;
103
+ } else if (property == '@graph') {
104
+ // the JSON-LD flattened structure may contain nested graphs
105
+ // the id value for this object is the new base (named graph id) for all nested flat resources
106
+ var graphId = id; // this is an array of resources
99
107
 
100
- if (Array.isArray(value)) {
101
- for (var i = 0; i < value.length; i++) {
102
- kb.addStatement(createStatement(kb, id, property, value[i], base));
103
- }
104
- } else {
105
- kb.addStatement(createStatement(kb, id, property, value, base));
106
- }
108
+ var nestedFlatResources = flatResource[property]; // recursively process all flat resources in the array, but with the graphId as base.
109
+
110
+ for (var i = 0; i < nestedFlatResources.length; i++) {
111
+ kb = processResource(kb, graphId, nestedFlatResources[i]);
107
112
  }
113
+ }
108
114
 
109
- return kb;
110
- }, kb);
111
- }).then(callback).catch(callback);
115
+ var value = flatResource[property];
116
+
117
+ if (Array.isArray(value)) {
118
+ for (var _i2 = 0; _i2 < value.length; _i2++) {
119
+ kb.addStatement(createStatement(kb, id, property, value[_i2], base));
120
+ }
121
+ } else {
122
+ kb.addStatement(createStatement(kb, id, property, value, base));
123
+ }
124
+ }
125
+
126
+ return kb;
112
127
  }
113
128
  /**
114
129
  * Create statement quad depending on @type being a type node
@@ -40,11 +40,11 @@ export default class RDFParser {
40
40
  getAttributeNodeNS(node: any, uri: any, name: any): any;
41
41
  /**
42
42
  * Build our initial scope frame and parse the DOM into triples
43
- * @param {DOMTree} document The DOM to parse
43
+ * @param {HTMLDocument} document The DOM to parse
44
44
  * @param {String} base The base URL to use
45
45
  * @param {Object} why The context to which this resource belongs
46
46
  */
47
- parse(document: any, base: string, why: any): boolean;
47
+ parse(document: HTMLDocument, base: string, why: any): boolean;
48
48
  base: string | undefined;
49
49
  parseDOM(frame: any): void;
50
50
  /**
@@ -269,7 +269,7 @@ var RDFParser = /*#__PURE__*/function () {
269
269
  }
270
270
  /**
271
271
  * Build our initial scope frame and parse the DOM into triples
272
- * @param {DOMTree} document The DOM to parse
272
+ * @param {HTMLDocument} document The DOM to parse
273
273
  * @param {String} base The base URL to use
274
274
  * @param {Object} why The context to which this resource belongs
275
275
  */
@@ -1,15 +1,14 @@
1
1
  import Formula from './formula';
2
2
  import { ContentType } from './types';
3
- import IndexedFormula from './store';
4
3
  import { BlankNode, NamedNode } from './tf-types';
5
4
  /**
6
5
  * Serialize to the appropriate format
7
6
  */
8
7
  export default function serialize(
9
8
  /** The graph or nodes that should be serialized */
10
- target: Formula | NamedNode | BlankNode,
9
+ target: Formula | NamedNode | BlankNode | null,
11
10
  /** The store */
12
- kb?: IndexedFormula, base?: unknown,
11
+ kb: Formula, base?: unknown,
13
12
  /**
14
13
  * The mime type.
15
14
  * Defaults to Turtle.
@@ -19,5 +18,9 @@ contentType?: string | ContentType, callback?: (err: Error | undefined | null, r
19
18
  * A string of letters, each of which set an options
20
19
  * e.g. `deinprstux`
21
20
  */
22
- flags: string;
21
+ flags?: string;
22
+ /**
23
+ * A set of [prefix, uri] pairs that define namespace prefixes
24
+ */
25
+ namespaces?: Record<string, string>;
23
26
  }): string | undefined;
package/lib/serialize.js CHANGED
@@ -32,7 +32,7 @@ kb, base,
32
32
  * Defaults to Turtle.
33
33
  */
34
34
  contentType, callback, options) {
35
- base = base || target.value;
35
+ base = base || (target === null || target === void 0 ? void 0 : target.value);
36
36
  var opts = options || {};
37
37
  contentType = contentType || _types.TurtleContentType; // text/n3 if complex?
38
38
 
@@ -42,8 +42,17 @@ contentType, callback, options) {
42
42
  var sz = (0, _serializer.default)(kb);
43
43
  if (opts.flags) sz.setFlags(opts.flags);
44
44
  var newSts = kb.statementsMatching(undefined, undefined, undefined, target);
45
- var n3String;
46
- sz.suggestNamespaces(kb.namespaces);
45
+ var n3String; // If an IndexedFormula, use the namespaces from the given graph as suggestions
46
+
47
+ if ('namespaces' in kb) {
48
+ sz.suggestNamespaces(kb.namespaces);
49
+ } // use the provided options.namespaces are mandatory prefixes
50
+
51
+
52
+ if (opts.namespaces) {
53
+ sz.setNamespaces(opts.namespaces);
54
+ }
55
+
47
56
  sz.setBase(base);
48
57
 
49
58
  switch (contentType) {
@@ -25,6 +25,19 @@ export class Serializer {
25
25
  setFlags(flags: any): Serializer;
26
26
  toStr(x: any): any;
27
27
  fromStr(s: any): any;
28
+ /**
29
+ * Defines a set of [prefix, namespace] pairs to be uised by this Serializer instance.
30
+ * Overrides previous prefixes if any
31
+ * @param namespaces
32
+ * @return {Serializer}
33
+ */
34
+ setNamespaces(namespaces: any): Serializer;
35
+ /**
36
+ * Defines a namespace prefix, overriding any existing prefix for that URI
37
+ * @param prefix
38
+ * @param uri
39
+ */
40
+ setPrefix(prefix: any, uri: any): void;
28
41
  suggestPrefix(prefix: any, uri: any): void;
29
42
  suggestNamespaces(namespaces: any): Serializer;
30
43
  checkIntegrity(): void;