n3 3.0.0-alpha.7 → 3.0.0-alpha.8
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/browser/n3.esm.min.js +6 -6
- package/browser/n3.min.js +6 -6
- package/lib/N3Writer.js +9 -38
- package/package.json +1 -1
- package/src/N3Writer.js +12 -42
package/lib/N3Writer.js
CHANGED
|
@@ -22,15 +22,11 @@ const {
|
|
|
22
22
|
hasOwnProperty
|
|
23
23
|
} = Object.prototype;
|
|
24
24
|
|
|
25
|
-
// Characters that require escaping in
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// U+0000–U+001F, `"`, `\`, U+007F, U+FFFE, and U+FFFF.
|
|
31
|
-
// A negated class is as fast to test as a shorter class listing characters.
|
|
32
|
-
canonicalEscape = /[^ !#-\[\]-~\u0080-\uFFFD]/,
|
|
33
|
-
canonicalEscapeAll = /[^ !#-\[\]-~\u0080-\uFFFD]/g,
|
|
25
|
+
// Characters that require escaping, as in canonical N-Triples:
|
|
26
|
+
// U+0000–U+001F, `"`, `\`, U+007F, U+FFFE, and U+FFFF.
|
|
27
|
+
// A negated class is as fast to test as a shorter class listing characters.
|
|
28
|
+
const escape = /[^ !#-\[\]-~\u0080-\uFFFD]/,
|
|
29
|
+
escapeAll = /[^ !#-\[\]-~\u0080-\uFFFD]/g,
|
|
34
30
|
escapedCharacters = {
|
|
35
31
|
'\\': '\\\\',
|
|
36
32
|
'"': '\\"',
|
|
@@ -91,7 +87,6 @@ class N3Writer {
|
|
|
91
87
|
this._subject = null;
|
|
92
88
|
if (!/triple|quad/i.test(options.format)) {
|
|
93
89
|
this._lineMode = false;
|
|
94
|
-
this._escape = escape, this._escapeAll = escapeAll, this._characterReplacer = characterReplacer;
|
|
95
90
|
this._graph = DEFAULTGRAPH;
|
|
96
91
|
if (options.version) this._write(`@version "${options.version}".\n`);
|
|
97
92
|
this._prefixIRIs = Object.create(null);
|
|
@@ -105,9 +100,6 @@ class N3Writer {
|
|
|
105
100
|
} else {
|
|
106
101
|
this._lineMode = true;
|
|
107
102
|
this._writeQuad = this._writeQuadLine;
|
|
108
|
-
// N-Triples and N-Quads are written in their canonical form
|
|
109
|
-
this._escape = canonicalEscape, this._escapeAll = canonicalEscapeAll;
|
|
110
|
-
this._characterReplacer = canonicalCharacterReplacer;
|
|
111
103
|
if (options.version) this._write(`VERSION "${options.version}"\n`);
|
|
112
104
|
}
|
|
113
105
|
}
|
|
@@ -207,7 +199,7 @@ class N3Writer {
|
|
|
207
199
|
iri = this._baseIri.toRelative(iri);
|
|
208
200
|
}
|
|
209
201
|
// Escape special characters
|
|
210
|
-
if (
|
|
202
|
+
if (escape.test(iri)) iri = iri.replace(escapeAll, characterReplacer);
|
|
211
203
|
// Try to represent the IRI as prefixed name, unless no prefixes were added
|
|
212
204
|
const prefixMatch = this._hasPrefixes ? (this._prefixRegex || this._createPrefixRegex()).exec(iri) : null;
|
|
213
205
|
return !prefixMatch ? `<${iri}>` : this._prefixIRIs[prefixMatch[1]] + prefixMatch[2];
|
|
@@ -217,7 +209,7 @@ class N3Writer {
|
|
|
217
209
|
_encodeLiteral(literal) {
|
|
218
210
|
// Escape special characters
|
|
219
211
|
let value = literal.value;
|
|
220
|
-
if (
|
|
212
|
+
if (escape.test(value)) value = value.replace(escapeAll, characterReplacer);
|
|
221
213
|
|
|
222
214
|
// Write a language-tagged literal
|
|
223
215
|
const language = literal.language;
|
|
@@ -287,9 +279,7 @@ class N3Writer {
|
|
|
287
279
|
object,
|
|
288
280
|
graph
|
|
289
281
|
}) {
|
|
290
|
-
|
|
291
|
-
const space = this._lineMode ? ' ' : '';
|
|
292
|
-
return `<<(${space}${this._encodeSubject(subject)} ${this._encodePredicate(predicate)} ${this._encodeObject(object)}${(0, _N3Util.isDefaultGraph)(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`}${space})>>`;
|
|
282
|
+
return `<<( ${this._encodeSubject(subject)} ${this._encodePredicate(predicate)} ${this._encodeObject(object)}${(0, _N3Util.isDefaultGraph)(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`} )>>`;
|
|
293
283
|
}
|
|
294
284
|
|
|
295
285
|
// ### `_blockedWrite` replaces `_write` after the writer has been closed
|
|
@@ -425,26 +415,7 @@ class N3Writer {
|
|
|
425
415
|
// Replaces a character by its escaped version
|
|
426
416
|
exports.default = N3Writer;
|
|
427
417
|
function characterReplacer(character) {
|
|
428
|
-
//
|
|
429
|
-
let result = escapedCharacters[character];
|
|
430
|
-
if (result === undefined) {
|
|
431
|
-
// Replace a single character with its 4-bit unicode escape sequence
|
|
432
|
-
if (character.length === 1) {
|
|
433
|
-
result = character.charCodeAt(0).toString(16);
|
|
434
|
-
result = '\\u0000'.substr(0, 6 - result.length) + result;
|
|
435
|
-
}
|
|
436
|
-
// Replace a surrogate pair with its 8-bit unicode escape sequence
|
|
437
|
-
else {
|
|
438
|
-
result = ((character.charCodeAt(0) - 0xD800) * 0x400 + character.charCodeAt(1) + 0x2400).toString(16);
|
|
439
|
-
result = '\\U00000000'.substr(0, 10 - result.length) + result;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
return result;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// Replaces a character by its canonical N-Triples escape:
|
|
446
|
-
// a short escape where one exists, and an upper-case `\uXXXX` escape otherwise
|
|
447
|
-
function canonicalCharacterReplacer(character) {
|
|
418
|
+
// Use a short escape where one exists, and an upper-case `\uXXXX` escape otherwise
|
|
448
419
|
let result = escapedCharacters[character];
|
|
449
420
|
if (result === undefined) {
|
|
450
421
|
result = character.charCodeAt(0).toString(16).toUpperCase();
|
package/package.json
CHANGED
package/src/N3Writer.js
CHANGED
|
@@ -10,15 +10,11 @@ const DEFAULTGRAPH = N3DataFactory.defaultGraph();
|
|
|
10
10
|
const { rdf, xsd } = namespaces;
|
|
11
11
|
const { hasOwnProperty } = Object.prototype;
|
|
12
12
|
|
|
13
|
-
// Characters that require escaping in
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// U+0000–U+001F, `"`, `\`, U+007F, U+FFFE, and U+FFFF.
|
|
19
|
-
// A negated class is as fast to test as a shorter class listing characters.
|
|
20
|
-
canonicalEscape = /[^ !#-\[\]-~\u0080-\uFFFD]/,
|
|
21
|
-
canonicalEscapeAll = /[^ !#-\[\]-~\u0080-\uFFFD]/g,
|
|
13
|
+
// Characters that require escaping, as in canonical N-Triples:
|
|
14
|
+
// U+0000–U+001F, `"`, `\`, U+007F, U+FFFE, and U+FFFF.
|
|
15
|
+
// A negated class is as fast to test as a shorter class listing characters.
|
|
16
|
+
const escape = /[^ !#-\[\]-~\u0080-\uFFFD]/,
|
|
17
|
+
escapeAll = /[^ !#-\[\]-~\u0080-\uFFFD]/g,
|
|
22
18
|
escapedCharacters = {
|
|
23
19
|
'\\': '\\\\', '"': '\\"', '\t': '\\t',
|
|
24
20
|
'\n': '\\n', '\r': '\\r', '\b': '\\b', '\f': '\\f',
|
|
@@ -72,7 +68,6 @@ export default class N3Writer {
|
|
|
72
68
|
this._subject = null;
|
|
73
69
|
if (!(/triple|quad/i).test(options.format)) {
|
|
74
70
|
this._lineMode = false;
|
|
75
|
-
this._escape = escape, this._escapeAll = escapeAll, this._characterReplacer = characterReplacer;
|
|
76
71
|
this._graph = DEFAULTGRAPH;
|
|
77
72
|
if (options.version)
|
|
78
73
|
this._write(`@version "${options.version}".\n`);
|
|
@@ -89,9 +84,6 @@ export default class N3Writer {
|
|
|
89
84
|
else {
|
|
90
85
|
this._lineMode = true;
|
|
91
86
|
this._writeQuad = this._writeQuadLine;
|
|
92
|
-
// N-Triples and N-Quads are written in their canonical form
|
|
93
|
-
this._escape = canonicalEscape, this._escapeAll = canonicalEscapeAll;
|
|
94
|
-
this._characterReplacer = canonicalCharacterReplacer;
|
|
95
87
|
if (options.version)
|
|
96
88
|
this._write(`VERSION "${options.version}"\n`);
|
|
97
89
|
}
|
|
@@ -208,8 +200,8 @@ export default class N3Writer {
|
|
|
208
200
|
iri = this._baseIri.toRelative(iri);
|
|
209
201
|
}
|
|
210
202
|
// Escape special characters
|
|
211
|
-
if (
|
|
212
|
-
iri = iri.replace(
|
|
203
|
+
if (escape.test(iri))
|
|
204
|
+
iri = iri.replace(escapeAll, characterReplacer);
|
|
213
205
|
// Try to represent the IRI as prefixed name, unless no prefixes were added
|
|
214
206
|
const prefixMatch = this._hasPrefixes ? (this._prefixRegex || this._createPrefixRegex()).exec(iri) : null;
|
|
215
207
|
return !prefixMatch ? `<${iri}>` : this._prefixIRIs[prefixMatch[1]] + prefixMatch[2];
|
|
@@ -219,8 +211,8 @@ export default class N3Writer {
|
|
|
219
211
|
_encodeLiteral(literal) {
|
|
220
212
|
// Escape special characters
|
|
221
213
|
let value = literal.value;
|
|
222
|
-
if (
|
|
223
|
-
value = value.replace(
|
|
214
|
+
if (escape.test(value))
|
|
215
|
+
value = value.replace(escapeAll, characterReplacer);
|
|
224
216
|
|
|
225
217
|
// Write a language-tagged literal
|
|
226
218
|
const language = literal.language;
|
|
@@ -291,13 +283,11 @@ export default class N3Writer {
|
|
|
291
283
|
|
|
292
284
|
// ### `_encodeQuad` encodes an RDF-star quad
|
|
293
285
|
_encodeQuad({ subject, predicate, object, graph }) {
|
|
294
|
-
|
|
295
|
-
const space = this._lineMode ? ' ' : '';
|
|
296
|
-
return `<<(${space}${
|
|
286
|
+
return `<<( ${
|
|
297
287
|
this._encodeSubject(subject)} ${
|
|
298
288
|
this._encodePredicate(predicate)} ${
|
|
299
289
|
this._encodeObject(object)}${
|
|
300
|
-
isDefaultGraph(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`}
|
|
290
|
+
isDefaultGraph(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`} )>>`;
|
|
301
291
|
}
|
|
302
292
|
|
|
303
293
|
// ### `_blockedWrite` replaces `_write` after the writer has been closed
|
|
@@ -440,27 +430,7 @@ export default class N3Writer {
|
|
|
440
430
|
|
|
441
431
|
// Replaces a character by its escaped version
|
|
442
432
|
function characterReplacer(character) {
|
|
443
|
-
//
|
|
444
|
-
let result = escapedCharacters[character];
|
|
445
|
-
if (result === undefined) {
|
|
446
|
-
// Replace a single character with its 4-bit unicode escape sequence
|
|
447
|
-
if (character.length === 1) {
|
|
448
|
-
result = character.charCodeAt(0).toString(16);
|
|
449
|
-
result = '\\u0000'.substr(0, 6 - result.length) + result;
|
|
450
|
-
}
|
|
451
|
-
// Replace a surrogate pair with its 8-bit unicode escape sequence
|
|
452
|
-
else {
|
|
453
|
-
result = ((character.charCodeAt(0) - 0xD800) * 0x400 +
|
|
454
|
-
character.charCodeAt(1) + 0x2400).toString(16);
|
|
455
|
-
result = '\\U00000000'.substr(0, 10 - result.length) + result;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
return result;
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
// Replaces a character by its canonical N-Triples escape:
|
|
462
|
-
// a short escape where one exists, and an upper-case `\uXXXX` escape otherwise
|
|
463
|
-
function canonicalCharacterReplacer(character) {
|
|
433
|
+
// Use a short escape where one exists, and an upper-case `\uXXXX` escape otherwise
|
|
464
434
|
let result = escapedCharacters[character];
|
|
465
435
|
if (result === undefined) {
|
|
466
436
|
result = character.charCodeAt(0).toString(16).toUpperCase();
|