rdflib 2.2.21-d633bed7 → 2.2.21-e3e59ee9

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/convert.js ADDED
@@ -0,0 +1,60 @@
1
+ import asyncLib from 'async'; // @@ Goal: remove this dependency
2
+ import jsonld from 'jsonld';
3
+ import { Parser, Writer } from 'n3'; // @@ Goal: remove this dependency
4
+
5
+ export function convertToJson(n3String, jsonCallback) {
6
+ var jsonString;
7
+ var n3Parser = new Parser();
8
+ var n3Writer = new Writer({
9
+ format: 'N-Quads'
10
+ });
11
+ asyncLib.waterfall([function (callback) {
12
+ n3Parser.parse(n3String, function (error, quad, prefixes) {
13
+ if (error) {
14
+ callback(error);
15
+ } else if (quad !== null) {
16
+ n3Writer.addQuad(quad);
17
+ } else {
18
+ n3Writer.end(callback);
19
+ }
20
+ });
21
+ }, function (result, callback) {
22
+ try {
23
+ jsonld.fromRDF(result, {
24
+ format: 'application/nquads'
25
+ }).then(result => {
26
+ callback(null, result);
27
+ });
28
+ } catch (err) {
29
+ callback(err);
30
+ }
31
+ }, function (json, callback) {
32
+ jsonString = JSON.stringify(json);
33
+ jsonCallback(null, jsonString);
34
+ }], function (err, result) {
35
+ jsonCallback(err, jsonString);
36
+ });
37
+ }
38
+ export function convertToNQuads(n3String, nquadCallback) {
39
+ var nquadString;
40
+ var n3Parser = new Parser();
41
+ var n3Writer = new Writer({
42
+ format: 'N-Quads'
43
+ });
44
+ asyncLib.waterfall([function (callback) {
45
+ n3Parser.parse(n3String, function (error, triple, prefixes) {
46
+ if (error) {
47
+ callback(error);
48
+ } else if (quad !== null) {
49
+ n3Writer.addQuad(quad);
50
+ } else {
51
+ n3Writer.end(callback);
52
+ }
53
+ });
54
+ }, function (result, callback) {
55
+ nquadString = result;
56
+ nquadCallback(null, nquadString);
57
+ }], function (err, result) {
58
+ nquadCallback(err, nquadString);
59
+ });
60
+ }
package/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import BlankNode from './blank-node';
3
3
  import Collection from './collection';
4
+ import * as convert from './convert';
4
5
  import Empty from './empty';
5
6
  import Fetcher from './fetcher';
6
7
  import Formula from './formula';
@@ -56,7 +57,7 @@ const term = Node.fromValue;
56
57
  // it exports the _current_ value of nextId, which is always 0
57
58
  const NextId = BlankNode.nextId;
58
59
  export * from './utils/terms';
59
- export { BlankNode, Collection, DataFactory, Empty, Fetcher, Formula, Store, jsonParser, Literal, log, N3Parser, NamedNode, Namespace, Node, parse, Query, queryToSPARQL, RDFaProcessor, RDFParser, serialize, Serializer, SPARQLToQuery, sparqlUpdateParser, Statement, term, UpdateManager, UpdatesSocket, UpdatesVia, uri, Util, Variable, Store as IndexedFormula,
60
+ export { BlankNode, Collection, convert, DataFactory, Empty, Fetcher, Formula, Store, jsonParser, Literal, log, N3Parser, NamedNode, Namespace, Node, parse, Query, queryToSPARQL, RDFaProcessor, RDFParser, serialize, Serializer, SPARQLToQuery, sparqlUpdateParser, Statement, term, UpdateManager, UpdatesSocket, UpdatesVia, uri, Util, Variable, Store as IndexedFormula,
60
61
  // Alias
61
62
 
62
63
  NextId, fromNT, fetcher, graph, lit, st, namedNode as sym,
@@ -18,7 +18,7 @@ export function jsonldObjectToTerm(kb, obj) {
18
18
  return listToStatements(kb, obj);
19
19
  }
20
20
  if (Object.prototype.hasOwnProperty.call(obj, '@id')) {
21
- return nodeType(kb, obj);
21
+ return kb.rdfFactory.namedNode(obj['@id']);
22
22
  }
23
23
  if (Object.prototype.hasOwnProperty.call(obj, '@language')) {
24
24
  return kb.rdfFactory.literal(obj['@value'], obj['@language']);
@@ -36,7 +36,7 @@ export function jsonldObjectToTerm(kb, obj) {
36
36
  * Adds the statements in a json-ld list object to {kb}.
37
37
  */
38
38
  function listToStatements(kb, obj) {
39
- const listId = obj['@id'] ? nodeType(kb, obj) : kb.rdfFactory.blankNode();
39
+ const listId = obj['@id'] ? kb.rdfFactory.namedNode(obj['@id']) : kb.rdfFactory.blankNode();
40
40
  const items = obj['@list'].map(listItem => jsonldObjectToTerm(kb, listItem));
41
41
  const statements = arrayToStatements(kb.rdfFactory, listId, items);
42
42
  kb.addAll(statements);
@@ -63,17 +63,8 @@ export default function jsonldParser(str, kb, base, callback) {
63
63
  return kb;
64
64
  }, kb)).then(callback).catch(callback);
65
65
  }
66
- function nodeType(kb, obj) {
67
- if (obj['@id'].startsWith('_:')) {
68
- // This object is a Blank Node.. pass the id without the preceding _:
69
- return kb.rdfFactory.blankNode(obj['@id'].substring(2));
70
- } else {
71
- // This object is a Named Node
72
- return kb.rdfFactory.namedNode(obj['@id']);
73
- }
74
- }
75
66
  function processResource(kb, base, flatResource) {
76
- const id = flatResource['@id'] ? nodeType(kb, flatResource) : kb.rdfFactory.blankNode();
67
+ const id = flatResource['@id'] ? kb.rdfFactory.namedNode(flatResource['@id']) : kb.rdfFactory.blankNode();
77
68
  for (const property of Object.keys(flatResource)) {
78
69
  if (property === '@id') {
79
70
  continue;
package/esm/serialize.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as convert from './convert';
1
2
  import Serializer from './serializer';
2
3
  import { JSONLDContentType, N3ContentType, N3LegacyContentType, NQuadsAltContentType, NQuadsContentType, NTriplesContentType, RDFXMLContentType, TurtleContentType, TurtleLegacyContentType } from './types';
3
4
  /**
@@ -11,7 +12,7 @@ kb, base,
11
12
  * Defaults to Turtle.
12
13
  */
13
14
  contentType, callback, options) {
14
- base = base || (target === null || target === void 0 ? void 0 : target.value);
15
+ base = base || target?.value;
15
16
  const opts = options || {};
16
17
  contentType = contentType || TurtleContentType; // text/n3 if complex?
17
18
  var documentString = undefined;
@@ -19,6 +20,7 @@ contentType, callback, options) {
19
20
  var sz = Serializer(kb);
20
21
  if (opts.flags) sz.setFlags(opts.flags);
21
22
  var newSts = kb.statementsMatching(undefined, undefined, undefined, target);
23
+ var n3String;
22
24
 
23
25
  // If an IndexedFormula, use the namespaces from the given graph as suggestions
24
26
  if ('namespaces' in kb) {
@@ -49,13 +51,19 @@ contentType, callback, options) {
49
51
  return executeCallback(null, documentString);
50
52
  case JSONLDContentType:
51
53
  sz.setFlags('deinprstux'); // Use adapters to connect to incmpatible parser
52
- return sz.statementsToJsonld(newSts);
54
+ n3String = sz.statementsToNTriples(newSts);
55
+ // n3String = sz.statementsToN3(newSts)
56
+ convert.convertToJson(n3String, callback);
57
+ break;
53
58
  case NQuadsContentType:
54
59
  case NQuadsAltContentType:
55
60
  // @@@ just outpout the quads? Does not work for collections
56
61
  sz.setFlags('deinprstux q'); // Suppress nice parts of N3 to make ntriples
57
62
  documentString = sz.statementsToNTriples(newSts); // q in flag means actually quads
58
63
  return executeCallback(null, documentString);
64
+ // n3String = sz.statementsToN3(newSts)
65
+ // documentString = convert.convertToNQuads(n3String, callback)
66
+ // break
59
67
  default:
60
68
  throw new Error('Serialize: Content-type ' + contentType + ' not supported for data write.');
61
69
  }
package/esm/serializer.js CHANGED
@@ -13,7 +13,6 @@ import * as Util from './utils-js';
13
13
  import CanonicalDataFactory from './factories/canonical-data-factory';
14
14
  import { createXSD } from './xsd';
15
15
  import solidNs from 'solid-namespace';
16
- import * as jsonld from 'jsonld';
17
16
  export default function createSerializer(store) {
18
17
  return new Serializer(store);
19
18
  }
@@ -963,138 +962,6 @@ export class Serializer {
963
962
  var tree2 = [str, tree, '</rdf:RDF>']; // @@ namespace declrations
964
963
  return XMLtreeToString(tree2, -1);
965
964
  } // End @@ body
966
-
967
- statementsToJsonld(sts) {
968
- // TODO
969
- // - make relative with @id: '' in replacing fist @id
970
- // - shall I use @type ?
971
- /* for (var ns in this.prefixes) {
972
- if (!this.prefixes.hasOwnProperty(ns)) continue
973
- if (!this.namespacesUsed[ns]) continue
974
- // str += '@prefix ' + this.prefixes[ns] + ': ' + this.explicitURI(ns) +
975
- // '.\n'
976
- context += `"${this.prefixes[ns]}": "${this.explicitURI(ns)}",`
977
- } */
978
-
979
- async function toJsonld(item, context, base) {
980
- function makeRelative(jsonldString) {
981
- /* let j = base.indexOf("#") // cut before #
982
- if (j < 0 ) {
983
- j = base.length // base.lastIndexOf('/') + 1 // cut after /
984
- } */
985
- // console.log('Alain base ' + base + ' ' + base.slice(0, j))
986
- if (base.includes('#')) throw new Error(`${base} must be an URL, it is an URI`);
987
- const id = '"@id": "'; // find URI
988
- const regex = new RegExp(id + base, 'g'); // .slice(0, j), 'g')
989
- let result,
990
- indices = [];
991
- // store indices at begin and end of URI
992
- while (result = regex.exec(jsonldString)) {
993
- indices.push(result.index + id.length);
994
- indices.push(jsonldString.indexOf('"', result.index + id.length));
995
- }
996
- for (let i = indices.length - 2; i >= 0; i = i - 2) {
997
- const idUri = jsonldString.substring(indices[i], indices[i + 1]);
998
- jsonldString = jsonldString.replace(idUri, Uri.refTo(base, idUri));
999
- }
1000
- return jsonldString; // .replace(regex, id)
1001
- }
1002
- // console.log(context)
1003
- let jsonldContext = JSON.parse(context);
1004
- /* console.log('alain ' + base)
1005
- let j = base.indexOf("#")
1006
- if (j < 0 ) {
1007
- j = base.lastIndexOf('/')
1008
- }
1009
- console.log(base.slice(0, j))
1010
- const regex = new RegExp(base.slice(0, j), 'g') // alain
1011
- // const regex = new RegExp('http://localhost/t1.ttl', 'g') */
1012
- return jsonld.fromRDF(item, {
1013
- format: 'application/n-quads'
1014
- }).then(n3String => {
1015
- return jsonld.compact(n3String, jsonldContext);
1016
- })
1017
- // TODO add collections, compact do not do it ??
1018
- .then(jsonldObj => {
1019
- return JSON.stringify(jsonldObj, null, 2);
1020
- }) // format jsonld
1021
- .then(jsonldString => {
1022
- return makeRelative(jsonldString);
1023
- }).catch(e => {
1024
- throw e;
1025
- });
1026
- }
1027
- function contextMethod(n3String) {
1028
- let prefixes = new Set();
1029
- const regex = /<(.*?)>/g;
1030
- n3String.match(regex).map(string => {
1031
- const uri = string.slice(1).slice(0, -1);
1032
- console.log(this.base + ' ' + this.base);
1033
- if (uri.includes('//') && (!this.base || !uri.includes(this.base))) prefixes.add(addPrefix(uri));
1034
- });
1035
- let context = `{ `;
1036
- const test = [...prefixes].map(term => {
1037
- context += `${term},`;
1038
- });
1039
- /* console.log(this.prefixes)
1040
- let str = ''
1041
- console.log(this.namespacesUsed)
1042
- for (var ns in this.prefixes) {
1043
- if (!this.prefixes.hasOwnProperty(ns)) continue
1044
- if (!this.namespacesUsed[ns]) continue
1045
- str += '@prefix ' + this.prefixes[ns] + ': ' + this.explicitURI(ns) + '.\n'
1046
- // context += `"${this.prefixes[ns]}": "${this.explicitURI(ns)}",`
1047
- }
1048
- console.log('alain string ' +str)
1049
- */
1050
- return context.slice(0, -1) + `}`;
1051
- }
1052
- function addPrefixMethod(uri) {
1053
- var j = uri.indexOf('#');
1054
- if (j < 0) {
1055
- j = uri.lastIndexOf('/');
1056
- }
1057
- if (j >= 0 && (
1058
- // Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?)
1059
- uri.indexOf('http') === 0 || uri.indexOf('ws') === 0 || uri.indexOf('file') === 0)) {
1060
- var canSplit = true;
1061
- for (var k = j + 1; k < uri.length; k++) {
1062
- if (this._notNameChars.indexOf(uri[k]) >= 0) {
1063
- canSplit = false;
1064
- break;
1065
- }
1066
- }
1067
- if (canSplit) {
1068
- var localid = uri.slice(j + 1);
1069
- var namesp = uri.slice(0, j + 1);
1070
- /*if (this.defaultNamespace && this.defaultNamespace === namesp &&
1071
- this.flags.indexOf('d') < 0) { // d -> suppress default
1072
- if (this.flags.indexOf('k') >= 0 &&
1073
- this.keyords.indexOf(localid) < 0) {
1074
- return localid
1075
- }
1076
- return ':' + localid
1077
- } */
1078
- // this.checkIntegrity() // @@@ Remove when not testing
1079
- var prefix = this.prefixes[namesp];
1080
- if (!prefix) prefix = this.makeUpPrefix(namesp);
1081
- if (prefix) {
1082
- console.log(`Alain "${prefix}": "${namesp}"`);
1083
- return `"${prefix}": "${namesp}"`;
1084
- }
1085
- }
1086
- }
1087
- }
1088
- // return this.explicitURI(uri)
1089
- // }
1090
- var addPrefix = addPrefixMethod.bind(this);
1091
- var context = contextMethod.bind(this);
1092
- const n3String = this.statementsToNTriples(sts);
1093
- // console.log(n3String)
1094
- let contextString = context(n3String);
1095
- // console.log(this.namespacesUsed)
1096
- return toJsonld(n3String, contextString, this.base);
1097
- }
1098
965
  }
1099
966
 
1100
967
  // String escaping utilities
package/esm/store.js CHANGED
@@ -1037,12 +1037,11 @@ export default class IndexedFormula extends Formula {
1037
1037
  return res;
1038
1038
  }
1039
1039
  serialize(base, contentType, provenance, options) {
1040
- var _options;
1041
1040
  // override Formula.serialize to force the serializer namespace prefixes
1042
1041
  // to those of this IndexedFormula
1043
1042
 
1044
1043
  // if namespaces are explicitly passed in options, let them override the existing namespaces in this formula
1045
- const namespaces = (_options = options) !== null && _options !== void 0 && _options.namespaces ? {
1044
+ const namespaces = options?.namespaces ? {
1046
1045
  ...this.namespaces,
1047
1046
  ...options.namespaces
1048
1047
  } : {
@@ -675,6 +675,11 @@ export default class UpdateManager {
675
675
  // console.log(message)
676
676
  throw new Error(message);
677
677
  }
678
+ if (doc.termType !== 'NamedNode') {
679
+ let message = 'Error patching: document not a NamedNode:' + ds[0] + ', ' + is[0];
680
+ // console.log(message)
681
+ throw new Error(message);
682
+ }
678
683
  var control = this.patchControlFor(doc);
679
684
  var startTime = Date.now();
680
685
  var props = ['subject', 'predicate', 'object', 'why'];
@@ -0,0 +1,2 @@
1
+ export function convertToJson(n3String: any, jsonCallback: any): void;
2
+ export function convertToNQuads(n3String: any, nquadCallback: any): void;
package/lib/convert.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.convertToJson = convertToJson;
8
+ exports.convertToNQuads = convertToNQuads;
9
+ var _async = _interopRequireDefault(require("async"));
10
+ var _jsonld = _interopRequireDefault(require("jsonld"));
11
+ var _n = require("n3");
12
+ // @@ Goal: remove this dependency
13
+
14
+ // @@ Goal: remove this dependency
15
+
16
+ function convertToJson(n3String, jsonCallback) {
17
+ var jsonString;
18
+ var n3Parser = new _n.Parser();
19
+ var n3Writer = new _n.Writer({
20
+ format: 'N-Quads'
21
+ });
22
+ _async.default.waterfall([function (callback) {
23
+ n3Parser.parse(n3String, function (error, quad, prefixes) {
24
+ if (error) {
25
+ callback(error);
26
+ } else if (quad !== null) {
27
+ n3Writer.addQuad(quad);
28
+ } else {
29
+ n3Writer.end(callback);
30
+ }
31
+ });
32
+ }, function (result, callback) {
33
+ try {
34
+ _jsonld.default.fromRDF(result, {
35
+ format: 'application/nquads'
36
+ }).then(result => {
37
+ callback(null, result);
38
+ });
39
+ } catch (err) {
40
+ callback(err);
41
+ }
42
+ }, function (json, callback) {
43
+ jsonString = JSON.stringify(json);
44
+ jsonCallback(null, jsonString);
45
+ }], function (err, result) {
46
+ jsonCallback(err, jsonString);
47
+ });
48
+ }
49
+ function convertToNQuads(n3String, nquadCallback) {
50
+ var nquadString;
51
+ var n3Parser = new _n.Parser();
52
+ var n3Writer = new _n.Writer({
53
+ format: 'N-Quads'
54
+ });
55
+ _async.default.waterfall([function (callback) {
56
+ n3Parser.parse(n3String, function (error, triple, prefixes) {
57
+ if (error) {
58
+ callback(error);
59
+ } else if (quad !== null) {
60
+ n3Writer.addQuad(quad);
61
+ } else {
62
+ n3Writer.end(callback);
63
+ }
64
+ });
65
+ }, function (result, callback) {
66
+ nquadString = result;
67
+ nquadCallback(null, nquadString);
68
+ }], function (err, result) {
69
+ nquadCallback(err, nquadString);
70
+ });
71
+ }
package/lib/formula.d.ts CHANGED
@@ -270,7 +270,7 @@ export default class Formula extends Node {
270
270
  * @param provenance - The provenance URI
271
271
  * @param options - options to pass to the serializer, as defined in serialize method
272
272
  */
273
- serialize(base: any, contentType: any, provenance: any, options?: any): string | Promise<string> | undefined;
273
+ serialize(base: any, contentType: any, provenance: any, options?: any): string | undefined;
274
274
  /**
275
275
  * Creates a new formula with the substituting bindings applied
276
276
  * @param bindings - The bindings to substitute
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import BlankNode from './blank-node';
2
2
  import Collection from './collection';
3
+ import * as convert from './convert';
3
4
  import Empty from './empty';
4
5
  import Fetcher from './fetcher';
5
6
  import Formula from './formula';
@@ -34,7 +35,7 @@ declare const term: <T extends import("./types").FromValueReturns<any>>(value: i
34
35
  declare const NextId: number;
35
36
  export * from './utils/terms';
36
37
  export type { AutoInitOptions, ExtendedResponse, FetchError } from './fetcher';
37
- export { BlankNode, Collection, DataFactory, Empty, Fetcher, Formula, Store, jsonParser, Literal, log, N3Parser, NamedNode, Namespace, Node, parse, Query, queryToSPARQL, RDFaProcessor, RDFParser, serialize, Serializer, SPARQLToQuery, sparqlUpdateParser, Statement, term, UpdateManager, UpdatesSocket, UpdatesVia, uri, Util, Variable, Store as IndexedFormula, // Alias
38
+ export { BlankNode, Collection, convert, DataFactory, Empty, Fetcher, Formula, Store, jsonParser, Literal, log, N3Parser, NamedNode, Namespace, Node, parse, Query, queryToSPARQL, RDFaProcessor, RDFParser, serialize, Serializer, SPARQLToQuery, sparqlUpdateParser, Statement, term, UpdateManager, UpdatesSocket, UpdatesVia, uri, Util, Variable, Store as IndexedFormula, // Alias
38
39
  NextId, fromNT, fetcher, graph, lit, st, namedNode as sym, blankNode, defaultGraph, literal, namedNode, quad, triple, variable, };
39
40
  export { termValue } from './utils/termValue';
40
41
  export declare class ConnectedStore extends Store {
package/lib/index.js CHANGED
@@ -24,6 +24,7 @@ var _exportNames = {
24
24
  LiveStore: true,
25
25
  BlankNode: true,
26
26
  Collection: true,
27
+ convert: true,
27
28
  Empty: true,
28
29
  Fetcher: true,
29
30
  Formula: true,
@@ -197,7 +198,7 @@ Object.defineProperty(exports, "Variable", {
197
198
  return _variable.default;
198
199
  }
199
200
  });
200
- exports.graph = exports.fromNT = exports.fetcher = exports.defaultGraph = exports.blankNode = void 0;
201
+ exports.graph = exports.fromNT = exports.fetcher = exports.defaultGraph = exports.convert = exports.blankNode = void 0;
201
202
  Object.defineProperty(exports, "jsonParser", {
202
203
  enumerable: true,
203
204
  get: function () {
@@ -248,6 +249,8 @@ exports.variable = exports.uri = exports.triple = void 0;
248
249
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
249
250
  var _blankNode = _interopRequireDefault(require("./blank-node"));
250
251
  var _collection = _interopRequireDefault(require("./collection"));
252
+ var convert = _interopRequireWildcard(require("./convert"));
253
+ exports.convert = convert;
251
254
  var _empty = _interopRequireDefault(require("./empty"));
252
255
  var _fetcher = _interopRequireDefault(require("./fetcher"));
253
256
  var _formula = _interopRequireDefault(require("./formula"));
@@ -25,7 +25,7 @@ function jsonldObjectToTerm(kb, obj) {
25
25
  return listToStatements(kb, obj);
26
26
  }
27
27
  if (Object.prototype.hasOwnProperty.call(obj, '@id')) {
28
- return nodeType(kb, obj);
28
+ return kb.rdfFactory.namedNode(obj['@id']);
29
29
  }
30
30
  if (Object.prototype.hasOwnProperty.call(obj, '@language')) {
31
31
  return kb.rdfFactory.literal(obj['@value'], obj['@language']);
@@ -43,7 +43,7 @@ function jsonldObjectToTerm(kb, obj) {
43
43
  * Adds the statements in a json-ld list object to {kb}.
44
44
  */
45
45
  function listToStatements(kb, obj) {
46
- const listId = obj['@id'] ? nodeType(kb, obj) : kb.rdfFactory.blankNode();
46
+ const listId = obj['@id'] ? kb.rdfFactory.namedNode(obj['@id']) : kb.rdfFactory.blankNode();
47
47
  const items = obj['@list'].map(listItem => jsonldObjectToTerm(kb, listItem));
48
48
  const statements = (0, _utils.arrayToStatements)(kb.rdfFactory, listId, items);
49
49
  kb.addAll(statements);
@@ -70,17 +70,8 @@ function jsonldParser(str, kb, base, callback) {
70
70
  return kb;
71
71
  }, kb)).then(callback).catch(callback);
72
72
  }
73
- function nodeType(kb, obj) {
74
- if (obj['@id'].startsWith('_:')) {
75
- // This object is a Blank Node.. pass the id without the preceding _:
76
- return kb.rdfFactory.blankNode(obj['@id'].substring(2));
77
- } else {
78
- // This object is a Named Node
79
- return kb.rdfFactory.namedNode(obj['@id']);
80
- }
81
- }
82
73
  function processResource(kb, base, flatResource) {
83
- const id = flatResource['@id'] ? nodeType(kb, flatResource) : kb.rdfFactory.blankNode();
74
+ const id = flatResource['@id'] ? kb.rdfFactory.namedNode(flatResource['@id']) : kb.rdfFactory.blankNode();
84
75
  for (const property of Object.keys(flatResource)) {
85
76
  if (property === '@id') {
86
77
  continue;
@@ -23,4 +23,4 @@ contentType?: string | ContentType, callback?: (err: Error | undefined | null, r
23
23
  * A set of [prefix, uri] pairs that define namespace prefixes
24
24
  */
25
25
  namespaces?: Record<string, string>;
26
- }): string | undefined | Promise<string>;
26
+ }): string | undefined;
package/lib/serialize.js CHANGED
@@ -5,8 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.default = serialize;
8
+ var convert = _interopRequireWildcard(require("./convert"));
8
9
  var _serializer = _interopRequireDefault(require("./serializer"));
9
10
  var _types = require("./types");
11
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
13
  /**
11
14
  * Serialize to the appropriate format
12
15
  */
@@ -18,7 +21,7 @@ kb, base,
18
21
  * Defaults to Turtle.
19
22
  */
20
23
  contentType, callback, options) {
21
- base = base || (target === null || target === void 0 ? void 0 : target.value);
24
+ base = base || target?.value;
22
25
  const opts = options || {};
23
26
  contentType = contentType || _types.TurtleContentType; // text/n3 if complex?
24
27
  var documentString = undefined;
@@ -26,6 +29,7 @@ contentType, callback, options) {
26
29
  var sz = (0, _serializer.default)(kb);
27
30
  if (opts.flags) sz.setFlags(opts.flags);
28
31
  var newSts = kb.statementsMatching(undefined, undefined, undefined, target);
32
+ var n3String;
29
33
 
30
34
  // If an IndexedFormula, use the namespaces from the given graph as suggestions
31
35
  if ('namespaces' in kb) {
@@ -56,13 +60,19 @@ contentType, callback, options) {
56
60
  return executeCallback(null, documentString);
57
61
  case _types.JSONLDContentType:
58
62
  sz.setFlags('deinprstux'); // Use adapters to connect to incmpatible parser
59
- return sz.statementsToJsonld(newSts);
63
+ n3String = sz.statementsToNTriples(newSts);
64
+ // n3String = sz.statementsToN3(newSts)
65
+ convert.convertToJson(n3String, callback);
66
+ break;
60
67
  case _types.NQuadsContentType:
61
68
  case _types.NQuadsAltContentType:
62
69
  // @@@ just outpout the quads? Does not work for collections
63
70
  sz.setFlags('deinprstux q'); // Suppress nice parts of N3 to make ntriples
64
71
  documentString = sz.statementsToNTriples(newSts); // q in flag means actually quads
65
72
  return executeCallback(null, documentString);
73
+ // n3String = sz.statementsToN3(newSts)
74
+ // documentString = convert.convertToNQuads(n3String, callback)
75
+ // break
66
76
  default:
67
77
  throw new Error('Serialize: Content-type ' + contentType + ' not supported for data write.');
68
78
  }
@@ -63,6 +63,5 @@ export class Serializer {
63
63
  symbolToN3(x: any): any;
64
64
  writeStore(write: any): void;
65
65
  statementsToXML(sts: any): string;
66
- statementsToJsonld(sts: any): Promise<any>;
67
66
  }
68
67
  import NamedNode from "./named-node";