n3 1.17.0 → 1.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -6
- package/browser/n3.min.js +1 -1
- package/lib/IRIs.js +4 -5
- package/lib/N3DataFactory.js +121 -137
- package/lib/N3Lexer.js +106 -139
- package/lib/N3Parser.js +275 -403
- package/lib/N3Store.js +294 -317
- package/lib/N3StreamParser.js +11 -14
- package/lib/N3StreamWriter.js +7 -11
- package/lib/N3Util.js +19 -25
- package/lib/N3Writer.js +124 -154
- package/lib/index.js +4 -18
- package/package.json +41 -18
- package/src/N3DataFactory.js +31 -24
- package/src/N3Store.js +68 -49
- package/src/N3Writer.js +2 -2
package/lib/N3Writer.js
CHANGED
|
@@ -4,58 +4,54 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _IRIs = _interopRequireDefault(require("./IRIs"));
|
|
9
|
-
|
|
10
8
|
var _N3DataFactory = _interopRequireWildcard(require("./N3DataFactory"));
|
|
11
|
-
|
|
12
9
|
var _N3Util = require("./N3Util");
|
|
13
|
-
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
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; }
|
|
17
|
-
|
|
10
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
11
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
18
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
13
|
// **N3Writer** writes N3 documents.
|
|
21
|
-
const DEFAULTGRAPH = _N3DataFactory.default.defaultGraph();
|
|
22
14
|
|
|
15
|
+
const DEFAULTGRAPH = _N3DataFactory.default.defaultGraph();
|
|
23
16
|
const {
|
|
24
17
|
rdf,
|
|
25
18
|
xsd
|
|
26
|
-
} = _IRIs.default;
|
|
19
|
+
} = _IRIs.default;
|
|
27
20
|
|
|
21
|
+
// Characters in literals that require escaping
|
|
28
22
|
const escape = /["\\\t\n\r\b\f\u0000-\u0019\ud800-\udbff]/,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
|
|
23
|
+
escapeAll = /["\\\t\n\r\b\f\u0000-\u0019]|[\ud800-\udbff][\udc00-\udfff]/g,
|
|
24
|
+
escapedCharacters = {
|
|
25
|
+
'\\': '\\\\',
|
|
26
|
+
'"': '\\"',
|
|
27
|
+
'\t': '\\t',
|
|
28
|
+
'\n': '\\n',
|
|
29
|
+
'\r': '\\r',
|
|
30
|
+
'\b': '\\b',
|
|
31
|
+
'\f': '\\f'
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// ## Placeholder class to represent already pretty-printed terms
|
|
40
35
|
class SerializedTerm extends _N3DataFactory.Term {
|
|
41
36
|
// Pretty-printed nodes are not equal to any other node
|
|
42
37
|
// (e.g., [] does not equal [])
|
|
43
|
-
equals() {
|
|
44
|
-
return
|
|
38
|
+
equals(other) {
|
|
39
|
+
return other === this;
|
|
45
40
|
}
|
|
41
|
+
}
|
|
46
42
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
// ## Constructor
|
|
50
44
|
class N3Writer {
|
|
51
45
|
constructor(outputStream, options) {
|
|
52
46
|
// ### `_prefixRegex` matches a prefixed name or IRI that begins with one of the added prefixes
|
|
53
|
-
this._prefixRegex = /$0^/;
|
|
47
|
+
this._prefixRegex = /$0^/;
|
|
54
48
|
|
|
49
|
+
// Shift arguments if the first argument is not a stream
|
|
55
50
|
if (outputStream && typeof outputStream.write !== 'function') options = outputStream, outputStream = null;
|
|
56
51
|
options = options || {};
|
|
57
|
-
this._lists = options.lists;
|
|
52
|
+
this._lists = options.lists;
|
|
58
53
|
|
|
54
|
+
// If no output stream given, send the output as string through the end callback
|
|
59
55
|
if (!outputStream) {
|
|
60
56
|
let output = '';
|
|
61
57
|
this._outputStream = {
|
|
@@ -63,7 +59,6 @@ class N3Writer {
|
|
|
63
59
|
output += chunk;
|
|
64
60
|
done && done();
|
|
65
61
|
},
|
|
66
|
-
|
|
67
62
|
end: done => {
|
|
68
63
|
done && done(null, output);
|
|
69
64
|
}
|
|
@@ -72,17 +67,15 @@ class N3Writer {
|
|
|
72
67
|
} else {
|
|
73
68
|
this._outputStream = outputStream;
|
|
74
69
|
this._endStream = options.end === undefined ? true : !!options.end;
|
|
75
|
-
}
|
|
76
|
-
|
|
70
|
+
}
|
|
77
71
|
|
|
72
|
+
// Initialize writer, depending on the format
|
|
78
73
|
this._subject = null;
|
|
79
|
-
|
|
80
74
|
if (!/triple|quad/i.test(options.format)) {
|
|
81
75
|
this._lineMode = false;
|
|
82
76
|
this._graph = DEFAULTGRAPH;
|
|
83
77
|
this._prefixIRIs = Object.create(null);
|
|
84
78
|
options.prefixes && this.addPrefixes(options.prefixes);
|
|
85
|
-
|
|
86
79
|
if (options.baseIRI) {
|
|
87
80
|
this._baseMatcher = new RegExp(`^${escapeRegex(options.baseIRI)}${options.baseIRI.endsWith('/') ? '' : '[#?]'}`);
|
|
88
81
|
this._baseLength = options.baseIRI.length;
|
|
@@ -91,69 +84,69 @@ class N3Writer {
|
|
|
91
84
|
this._lineMode = true;
|
|
92
85
|
this._writeQuad = this._writeQuadLine;
|
|
93
86
|
}
|
|
94
|
-
}
|
|
95
|
-
// ### Whether the current graph is the default graph
|
|
87
|
+
}
|
|
96
88
|
|
|
89
|
+
// ## Private methods
|
|
97
90
|
|
|
91
|
+
// ### Whether the current graph is the default graph
|
|
98
92
|
get _inDefaultGraph() {
|
|
99
93
|
return DEFAULTGRAPH.equals(this._graph);
|
|
100
|
-
}
|
|
101
|
-
|
|
94
|
+
}
|
|
102
95
|
|
|
96
|
+
// ### `_write` writes the argument to the output stream
|
|
103
97
|
_write(string, callback) {
|
|
104
98
|
this._outputStream.write(string, 'utf8', callback);
|
|
105
|
-
}
|
|
106
|
-
|
|
99
|
+
}
|
|
107
100
|
|
|
101
|
+
// ### `_writeQuad` writes the quad to the output stream
|
|
108
102
|
_writeQuad(subject, predicate, object, graph, done) {
|
|
109
103
|
try {
|
|
110
104
|
// Write the graph's label if it has changed
|
|
111
105
|
if (!graph.equals(this._graph)) {
|
|
112
106
|
// Close the previous graph and start the new one
|
|
113
107
|
this._write((this._subject === null ? '' : this._inDefaultGraph ? '.\n' : '\n}\n') + (DEFAULTGRAPH.equals(graph) ? '' : `${this._encodeIriOrBlank(graph)} {\n`));
|
|
114
|
-
|
|
115
108
|
this._graph = graph;
|
|
116
109
|
this._subject = null;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
110
|
+
}
|
|
111
|
+
// Don't repeat the subject if it's the same
|
|
120
112
|
if (subject.equals(this._subject)) {
|
|
121
113
|
// Don't repeat the predicate if it's the same
|
|
122
|
-
if (predicate.equals(this._predicate)) this._write(`, ${this._encodeObject(object)}`, done);
|
|
114
|
+
if (predicate.equals(this._predicate)) this._write(`, ${this._encodeObject(object)}`, done);
|
|
115
|
+
// Same subject, different predicate
|
|
123
116
|
else this._write(`;\n ${this._encodePredicate(this._predicate = predicate)} ${this._encodeObject(object)}`, done);
|
|
124
|
-
}
|
|
117
|
+
}
|
|
118
|
+
// Different subject; write the whole quad
|
|
125
119
|
else this._write(`${(this._subject === null ? '' : '.\n') + this._encodeSubject(this._subject = subject)} ${this._encodePredicate(this._predicate = predicate)} ${this._encodeObject(object)}`, done);
|
|
126
120
|
} catch (error) {
|
|
127
121
|
done && done(error);
|
|
128
122
|
}
|
|
129
|
-
}
|
|
130
|
-
|
|
123
|
+
}
|
|
131
124
|
|
|
125
|
+
// ### `_writeQuadLine` writes the quad to the output stream as a single line
|
|
132
126
|
_writeQuadLine(subject, predicate, object, graph, done) {
|
|
133
127
|
// Write the quad without prefixes
|
|
134
128
|
delete this._prefixMatch;
|
|
135
|
-
|
|
136
129
|
this._write(this.quadToString(subject, predicate, object, graph), done);
|
|
137
|
-
}
|
|
138
|
-
|
|
130
|
+
}
|
|
139
131
|
|
|
132
|
+
// ### `quadToString` serializes a quad as a string
|
|
140
133
|
quadToString(subject, predicate, object, graph) {
|
|
141
134
|
return `${this._encodeSubject(subject)} ${this._encodeIriOrBlank(predicate)} ${this._encodeObject(object)}${graph && graph.value ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
|
|
142
|
-
}
|
|
143
|
-
|
|
135
|
+
}
|
|
144
136
|
|
|
137
|
+
// ### `quadsToString` serializes an array of quads as a string
|
|
145
138
|
quadsToString(quads) {
|
|
146
139
|
return quads.map(t => {
|
|
147
140
|
return this.quadToString(t.subject, t.predicate, t.object, t.graph);
|
|
148
141
|
}).join('');
|
|
149
|
-
}
|
|
150
|
-
|
|
142
|
+
}
|
|
151
143
|
|
|
144
|
+
// ### `_encodeSubject` represents a subject
|
|
152
145
|
_encodeSubject(entity) {
|
|
153
146
|
return entity.termType === 'Quad' ? this._encodeQuad(entity) : this._encodeIriOrBlank(entity);
|
|
154
|
-
}
|
|
155
|
-
|
|
147
|
+
}
|
|
156
148
|
|
|
149
|
+
// ### `_encodeIriOrBlank` represents an IRI or blank node
|
|
157
150
|
_encodeIriOrBlank(entity) {
|
|
158
151
|
// A blank node or list is represented as-is
|
|
159
152
|
if (entity.termType !== 'NamedNode') {
|
|
@@ -161,26 +154,26 @@ class N3Writer {
|
|
|
161
154
|
if (this._lists && entity.value in this._lists) entity = this.list(this._lists[entity.value]);
|
|
162
155
|
return 'id' in entity ? entity.id : `_:${entity.value}`;
|
|
163
156
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
157
|
+
let iri = entity.value;
|
|
158
|
+
// Use relative IRIs if requested and possible
|
|
159
|
+
if (this._baseMatcher && this._baseMatcher.test(iri)) iri = iri.substr(this._baseLength);
|
|
160
|
+
// Escape special characters
|
|
161
|
+
if (escape.test(iri)) iri = iri.replace(escapeAll, characterReplacer);
|
|
162
|
+
// Try to represent the IRI as prefixed name
|
|
171
163
|
const prefixMatch = this._prefixRegex.exec(iri);
|
|
172
|
-
|
|
173
164
|
return !prefixMatch ? `<${iri}>` : !prefixMatch[1] ? iri : this._prefixIRIs[prefixMatch[1]] + prefixMatch[2];
|
|
174
|
-
}
|
|
175
|
-
|
|
165
|
+
}
|
|
176
166
|
|
|
167
|
+
// ### `_encodeLiteral` represents a literal
|
|
177
168
|
_encodeLiteral(literal) {
|
|
178
169
|
// Escape special characters
|
|
179
170
|
let value = literal.value;
|
|
180
|
-
if (escape.test(value)) value = value.replace(escapeAll, characterReplacer);
|
|
171
|
+
if (escape.test(value)) value = value.replace(escapeAll, characterReplacer);
|
|
181
172
|
|
|
182
|
-
|
|
173
|
+
// Write a language-tagged literal
|
|
174
|
+
if (literal.language) return `"${value}"@${literal.language}`;
|
|
183
175
|
|
|
176
|
+
// Write dedicated literals per data type
|
|
184
177
|
if (this._lineMode) {
|
|
185
178
|
// Only abbreviate strings in N-Triples or N-Quads
|
|
186
179
|
if (literal.datatype.value === xsd.string) return `"${value}"`;
|
|
@@ -189,49 +182,43 @@ class N3Writer {
|
|
|
189
182
|
switch (literal.datatype.value) {
|
|
190
183
|
case xsd.string:
|
|
191
184
|
return `"${value}"`;
|
|
192
|
-
|
|
193
185
|
case xsd.boolean:
|
|
194
186
|
if (value === 'true' || value === 'false') return value;
|
|
195
187
|
break;
|
|
196
|
-
|
|
197
188
|
case xsd.integer:
|
|
198
189
|
if (/^[+-]?\d+$/.test(value)) return value;
|
|
199
190
|
break;
|
|
200
|
-
|
|
201
191
|
case xsd.decimal:
|
|
202
192
|
if (/^[+-]?\d*\.\d+$/.test(value)) return value;
|
|
203
193
|
break;
|
|
204
|
-
|
|
205
194
|
case xsd.double:
|
|
206
195
|
if (/^[+-]?(?:\d+\.\d*|\.?\d+)[eE][+-]?\d+$/.test(value)) return value;
|
|
207
196
|
break;
|
|
208
197
|
}
|
|
209
|
-
}
|
|
210
|
-
|
|
198
|
+
}
|
|
211
199
|
|
|
200
|
+
// Write a regular datatyped literal
|
|
212
201
|
return `"${value}"^^${this._encodeIriOrBlank(literal.datatype)}`;
|
|
213
|
-
}
|
|
214
|
-
|
|
202
|
+
}
|
|
215
203
|
|
|
204
|
+
// ### `_encodePredicate` represents a predicate
|
|
216
205
|
_encodePredicate(predicate) {
|
|
217
206
|
return predicate.value === rdf.type ? 'a' : this._encodeIriOrBlank(predicate);
|
|
218
|
-
}
|
|
219
|
-
|
|
207
|
+
}
|
|
220
208
|
|
|
209
|
+
// ### `_encodeObject` represents an object
|
|
221
210
|
_encodeObject(object) {
|
|
222
211
|
switch (object.termType) {
|
|
223
212
|
case 'Quad':
|
|
224
213
|
return this._encodeQuad(object);
|
|
225
|
-
|
|
226
214
|
case 'Literal':
|
|
227
215
|
return this._encodeLiteral(object);
|
|
228
|
-
|
|
229
216
|
default:
|
|
230
217
|
return this._encodeIriOrBlank(object);
|
|
231
218
|
}
|
|
232
|
-
}
|
|
233
|
-
|
|
219
|
+
}
|
|
234
220
|
|
|
221
|
+
// ### `_encodeQuad` encodes an RDF* quad
|
|
235
222
|
_encodeQuad({
|
|
236
223
|
subject,
|
|
237
224
|
predicate,
|
|
@@ -239,177 +226,160 @@ class N3Writer {
|
|
|
239
226
|
graph
|
|
240
227
|
}) {
|
|
241
228
|
return `<<${this._encodeSubject(subject)} ${this._encodePredicate(predicate)} ${this._encodeObject(object)}${(0, _N3Util.isDefaultGraph)(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`}>>`;
|
|
242
|
-
}
|
|
243
|
-
|
|
229
|
+
}
|
|
244
230
|
|
|
231
|
+
// ### `_blockedWrite` replaces `_write` after the writer has been closed
|
|
245
232
|
_blockedWrite() {
|
|
246
233
|
throw new Error('Cannot write because the writer has been closed.');
|
|
247
|
-
}
|
|
248
|
-
|
|
234
|
+
}
|
|
249
235
|
|
|
236
|
+
// ### `addQuad` adds the quad to the output stream
|
|
250
237
|
addQuad(subject, predicate, object, graph, done) {
|
|
251
238
|
// The quad was given as an object, so shift parameters
|
|
252
|
-
if (object === undefined) this._writeQuad(subject.subject, subject.predicate, subject.object, subject.graph, predicate);
|
|
253
|
-
|
|
239
|
+
if (object === undefined) this._writeQuad(subject.subject, subject.predicate, subject.object, subject.graph, predicate);
|
|
240
|
+
// The optional `graph` parameter was not provided
|
|
241
|
+
else if (typeof graph === 'function') this._writeQuad(subject, predicate, object, DEFAULTGRAPH, graph);
|
|
242
|
+
// The `graph` parameter was provided
|
|
254
243
|
else this._writeQuad(subject, predicate, object, graph || DEFAULTGRAPH, done);
|
|
255
|
-
}
|
|
256
|
-
|
|
244
|
+
}
|
|
257
245
|
|
|
246
|
+
// ### `addQuads` adds the quads to the output stream
|
|
258
247
|
addQuads(quads) {
|
|
259
248
|
for (let i = 0; i < quads.length; i++) this.addQuad(quads[i]);
|
|
260
|
-
}
|
|
261
|
-
|
|
249
|
+
}
|
|
262
250
|
|
|
251
|
+
// ### `addPrefix` adds the prefix to the output stream
|
|
263
252
|
addPrefix(prefix, iri, done) {
|
|
264
253
|
const prefixes = {};
|
|
265
254
|
prefixes[prefix] = iri;
|
|
266
255
|
this.addPrefixes(prefixes, done);
|
|
267
|
-
}
|
|
268
|
-
|
|
256
|
+
}
|
|
269
257
|
|
|
258
|
+
// ### `addPrefixes` adds the prefixes to the output stream
|
|
270
259
|
addPrefixes(prefixes, done) {
|
|
271
260
|
// Ignore prefixes if not supported by the serialization
|
|
272
|
-
if (!this._prefixIRIs) return done && done();
|
|
261
|
+
if (!this._prefixIRIs) return done && done();
|
|
273
262
|
|
|
263
|
+
// Write all new prefixes
|
|
274
264
|
let hasPrefixes = false;
|
|
275
|
-
|
|
276
265
|
for (let prefix in prefixes) {
|
|
277
266
|
let iri = prefixes[prefix];
|
|
278
267
|
if (typeof iri !== 'string') iri = iri.value;
|
|
279
|
-
hasPrefixes = true;
|
|
280
|
-
|
|
268
|
+
hasPrefixes = true;
|
|
269
|
+
// Finish a possible pending quad
|
|
281
270
|
if (this._subject !== null) {
|
|
282
271
|
this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
|
|
283
|
-
|
|
284
272
|
this._subject = null, this._graph = '';
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
273
|
+
}
|
|
274
|
+
// Store and write the prefix
|
|
288
275
|
this._prefixIRIs[iri] = prefix += ':';
|
|
289
|
-
|
|
290
276
|
this._write(`@prefix ${prefix} <${iri}>.\n`);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
|
|
277
|
+
}
|
|
278
|
+
// Recreate the prefix matcher
|
|
294
279
|
if (hasPrefixes) {
|
|
295
280
|
let IRIlist = '',
|
|
296
|
-
|
|
297
|
-
|
|
281
|
+
prefixList = '';
|
|
298
282
|
for (const prefixIRI in this._prefixIRIs) {
|
|
299
283
|
IRIlist += IRIlist ? `|${prefixIRI}` : prefixIRI;
|
|
300
284
|
prefixList += (prefixList ? '|' : '') + this._prefixIRIs[prefixIRI];
|
|
301
285
|
}
|
|
302
|
-
|
|
303
286
|
IRIlist = escapeRegex(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
|
|
304
287
|
this._prefixRegex = new RegExp(`^(?:${prefixList})[^\/]*$|` + `^(${IRIlist})([_a-zA-Z][\\-_a-zA-Z0-9]*)$`);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
288
|
+
}
|
|
289
|
+
// End a prefix block with a newline
|
|
308
290
|
this._write(hasPrefixes ? '\n' : '', done);
|
|
309
|
-
}
|
|
310
|
-
|
|
291
|
+
}
|
|
311
292
|
|
|
293
|
+
// ### `blank` creates a blank node with the given content
|
|
312
294
|
blank(predicate, object) {
|
|
313
295
|
let children = predicate,
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if (predicate === undefined) children = [];
|
|
296
|
+
child,
|
|
297
|
+
length;
|
|
298
|
+
// Empty blank node
|
|
299
|
+
if (predicate === undefined) children = [];
|
|
300
|
+
// Blank node passed as blank(Term("predicate"), Term("object"))
|
|
318
301
|
else if (predicate.termType) children = [{
|
|
319
302
|
predicate: predicate,
|
|
320
303
|
object: object
|
|
321
|
-
}];
|
|
304
|
+
}];
|
|
305
|
+
// Blank node passed as blank({ predicate: predicate, object: object })
|
|
322
306
|
else if (!('length' in predicate)) children = [predicate];
|
|
323
|
-
|
|
324
307
|
switch (length = children.length) {
|
|
325
308
|
// Generate an empty blank node
|
|
326
309
|
case 0:
|
|
327
310
|
return new SerializedTerm('[]');
|
|
328
311
|
// Generate a non-nested one-triple blank node
|
|
329
|
-
|
|
330
312
|
case 1:
|
|
331
313
|
child = children[0];
|
|
332
314
|
if (!(child.object instanceof SerializedTerm)) return new SerializedTerm(`[ ${this._encodePredicate(child.predicate)} ${this._encodeObject(child.object)} ]`);
|
|
333
315
|
// Generate a multi-triple or nested blank node
|
|
334
|
-
|
|
335
316
|
default:
|
|
336
|
-
let contents = '[';
|
|
337
|
-
|
|
317
|
+
let contents = '[';
|
|
318
|
+
// Write all triples in order
|
|
338
319
|
for (let i = 0; i < length; i++) {
|
|
339
|
-
child = children[i];
|
|
340
|
-
|
|
341
|
-
if (child.predicate.equals(predicate)) contents += `, ${this._encodeObject(child.object)}`;
|
|
320
|
+
child = children[i];
|
|
321
|
+
// Write only the object is the predicate is the same as the previous
|
|
322
|
+
if (child.predicate.equals(predicate)) contents += `, ${this._encodeObject(child.object)}`;
|
|
323
|
+
// Otherwise, write the predicate and the object
|
|
342
324
|
else {
|
|
343
325
|
contents += `${(i ? ';\n ' : '\n ') + this._encodePredicate(child.predicate)} ${this._encodeObject(child.object)}`;
|
|
344
326
|
predicate = child.predicate;
|
|
345
327
|
}
|
|
346
328
|
}
|
|
347
|
-
|
|
348
329
|
return new SerializedTerm(`${contents}\n]`);
|
|
349
330
|
}
|
|
350
|
-
}
|
|
351
|
-
|
|
331
|
+
}
|
|
352
332
|
|
|
333
|
+
// ### `list` creates a list node with the given content
|
|
353
334
|
list(elements) {
|
|
354
335
|
const length = elements && elements.length || 0,
|
|
355
|
-
|
|
356
|
-
|
|
336
|
+
contents = new Array(length);
|
|
357
337
|
for (let i = 0; i < length; i++) contents[i] = this._encodeObject(elements[i]);
|
|
358
|
-
|
|
359
338
|
return new SerializedTerm(`(${contents.join(' ')})`);
|
|
360
|
-
}
|
|
361
|
-
|
|
339
|
+
}
|
|
362
340
|
|
|
341
|
+
// ### `end` signals the end of the output stream
|
|
363
342
|
end(done) {
|
|
364
343
|
// Finish a possible pending quad
|
|
365
344
|
if (this._subject !== null) {
|
|
366
345
|
this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
|
|
367
|
-
|
|
368
346
|
this._subject = null;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
this._write = this._blockedWrite; // Try to end the underlying stream, ensuring done is called exactly one time
|
|
347
|
+
}
|
|
348
|
+
// Disallow further writing
|
|
349
|
+
this._write = this._blockedWrite;
|
|
373
350
|
|
|
351
|
+
// Try to end the underlying stream, ensuring done is called exactly one time
|
|
374
352
|
let singleDone = done && ((error, result) => {
|
|
375
353
|
singleDone = null, done(error, result);
|
|
376
354
|
});
|
|
377
|
-
|
|
378
355
|
if (this._endStream) {
|
|
379
356
|
try {
|
|
380
357
|
return this._outputStream.end(singleDone);
|
|
381
|
-
} catch (error) {
|
|
382
|
-
/* error closing stream */
|
|
383
|
-
}
|
|
358
|
+
} catch (error) {/* error closing stream */}
|
|
384
359
|
}
|
|
385
|
-
|
|
386
360
|
singleDone && singleDone();
|
|
387
361
|
}
|
|
362
|
+
}
|
|
388
363
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
364
|
+
// Replaces a character by its escaped version
|
|
392
365
|
exports.default = N3Writer;
|
|
393
|
-
|
|
394
366
|
function characterReplacer(character) {
|
|
395
367
|
// Replace a single character by its escaped version
|
|
396
368
|
let result = escapedCharacters[character];
|
|
397
|
-
|
|
398
369
|
if (result === undefined) {
|
|
399
370
|
// Replace a single character with its 4-bit unicode escape sequence
|
|
400
371
|
if (character.length === 1) {
|
|
401
372
|
result = character.charCodeAt(0).toString(16);
|
|
402
373
|
result = '\\u0000'.substr(0, 6 - result.length) + result;
|
|
403
|
-
}
|
|
374
|
+
}
|
|
375
|
+
// Replace a surrogate pair with its 8-bit unicode escape sequence
|
|
404
376
|
else {
|
|
405
377
|
result = ((character.charCodeAt(0) - 0xD800) * 0x400 + character.charCodeAt(1) + 0x2400).toString(16);
|
|
406
378
|
result = '\\U00000000'.substr(0, 10 - result.length) + result;
|
|
407
379
|
}
|
|
408
380
|
}
|
|
409
|
-
|
|
410
381
|
return result;
|
|
411
382
|
}
|
|
412
|
-
|
|
413
383
|
function escapeRegex(regex) {
|
|
414
384
|
return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
|
|
415
385
|
}
|
package/lib/index.js
CHANGED
|
@@ -107,34 +107,21 @@ Object.defineProperty(exports, "termToId", {
|
|
|
107
107
|
return _N3DataFactory.termToId;
|
|
108
108
|
}
|
|
109
109
|
});
|
|
110
|
-
|
|
111
110
|
var _N3Lexer = _interopRequireDefault(require("./N3Lexer"));
|
|
112
|
-
|
|
113
111
|
var _N3Parser = _interopRequireDefault(require("./N3Parser"));
|
|
114
|
-
|
|
115
112
|
var _N3Writer = _interopRequireDefault(require("./N3Writer"));
|
|
116
|
-
|
|
117
113
|
var _N3Store = _interopRequireDefault(require("./N3Store"));
|
|
118
|
-
|
|
119
114
|
var _N3StreamParser = _interopRequireDefault(require("./N3StreamParser"));
|
|
120
|
-
|
|
121
115
|
var _N3StreamWriter = _interopRequireDefault(require("./N3StreamWriter"));
|
|
122
|
-
|
|
123
116
|
var Util = _interopRequireWildcard(require("./N3Util"));
|
|
124
|
-
|
|
125
117
|
exports.Util = Util;
|
|
126
|
-
|
|
127
118
|
var _N3DataFactory = _interopRequireWildcard(require("./N3DataFactory"));
|
|
128
|
-
|
|
129
|
-
function
|
|
130
|
-
|
|
131
|
-
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; }
|
|
132
|
-
|
|
119
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
120
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
133
121
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
134
|
-
|
|
135
122
|
// Named exports
|
|
136
123
|
// Export all named exports as a default object for backward compatibility
|
|
137
|
-
var _default = {
|
|
124
|
+
var _default = exports.default = {
|
|
138
125
|
Lexer: _N3Lexer.default,
|
|
139
126
|
Parser: _N3Parser.default,
|
|
140
127
|
Writer: _N3Writer.default,
|
|
@@ -153,5 +140,4 @@ var _default = {
|
|
|
153
140
|
Triple: _N3DataFactory.Triple,
|
|
154
141
|
termFromId: _N3DataFactory.termFromId,
|
|
155
142
|
termToId: _N3DataFactory.termToId
|
|
156
|
-
};
|
|
157
|
-
exports.default = _default;
|
|
143
|
+
};
|