Skip to content

Commit 42c9936

Browse files
Merge pull request #22 from OriginTrail/fix/group-nquads-func
V8: fix group nquads
2 parents 69a02af + 4763013 commit 42c9936

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

index.cjs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ function groupNquadsBySubject(nquadsArray, sort = false) {
289289
const nestedPredicate = subject.predicate.value;
290290
const nestedObject =
291291
subject.object.termType === "Literal"
292-
? `"""${subject.object.value}"""`
293-
: `<${subject.object.value}>`;
292+
? `"${escapeLiteral(subject.object.value)}"`
293+
: `<${escapeLiteral(subject.object.value)}>`;
294294
subjectKey = `<<<${nestedSubject}> <${nestedPredicate}> ${nestedObject}>>`;
295295
} else {
296296
subjectKey = `<${subject.value}>`;
@@ -301,7 +301,9 @@ function groupNquadsBySubject(nquadsArray, sort = false) {
301301
}
302302

303303
const objectValue =
304-
object.termType === "Literal" ? `"""${object.value}"""` : `<${object.value}>`;
304+
object.termType === "Literal"
305+
? `"${escapeLiteral(object.value)}"`
306+
: `<${escapeLiteral(object.value)}>`;
305307

306308
const quadString = `${subjectKey} <${predicate.value}> ${objectValue} .`;
307309
grouped[subjectKey].push(quadString);
@@ -426,6 +428,20 @@ function isEmptyObject(obj) {
426428
return Object.keys(obj).length === 0 && obj.constructor === Object;
427429
}
428430

431+
function escapeLiteral(value) {
432+
const ESCAPE_MAP = {
433+
'"': '\\"',
434+
"\\": "\\\\",
435+
"\b": "\\b",
436+
"\f": "\\f",
437+
"\n": "\\n",
438+
"\r": "\\r",
439+
"\t": "\\t",
440+
};
441+
442+
return value.replace(/["\\\b\f\n\r\t]/g, (char) => ESCAPE_MAP[char]);
443+
}
444+
429445
var knowledgeCollectionTools = /*#__PURE__*/Object.freeze({
430446
__proto__: null,
431447
calculateByteSize: calculateByteSize,

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "assertion-tools",
3-
"version": "8.0.1",
3+
"version": "8.0.2",
44
"description": "Common assertion tools used in ot-node and dkg.js",
55
"main": "index.js",
66
"type": "module",

src/knowledge-collection-tools.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ export function groupNquadsBySubject(nquadsArray, sort = false) {
157157
const nestedPredicate = subject.predicate.value;
158158
const nestedObject =
159159
subject.object.termType === "Literal"
160-
? `"""${subject.object.value}"""`
161-
: `<${subject.object.value}>`;
160+
? `"${escapeLiteral(subject.object.value)}"`
161+
: `<${escapeLiteral(subject.object.value)}>`;
162162
subjectKey = `<<<${nestedSubject}> <${nestedPredicate}> ${nestedObject}>>`;
163163
} else {
164164
subjectKey = `<${subject.value}>`;
@@ -169,7 +169,9 @@ export function groupNquadsBySubject(nquadsArray, sort = false) {
169169
}
170170

171171
const objectValue =
172-
object.termType === "Literal" ? `"""${object.value}"""` : `<${object.value}>`;
172+
object.termType === "Literal"
173+
? `"${escapeLiteral(object.value)}"`
174+
: `<${escapeLiteral(object.value)}>`;
173175

174176
const quadString = `${subjectKey} <${predicate.value}> ${objectValue} .`;
175177
grouped[subjectKey].push(quadString);
@@ -293,3 +295,17 @@ export function generateMissingIdsForBlankNodes(nquadsArray) {
293295
function isEmptyObject(obj) {
294296
return Object.keys(obj).length === 0 && obj.constructor === Object;
295297
}
298+
299+
function escapeLiteral(value) {
300+
const ESCAPE_MAP = {
301+
'"': '\\"',
302+
"\\": "\\\\",
303+
"\b": "\\b",
304+
"\f": "\\f",
305+
"\n": "\\n",
306+
"\r": "\\r",
307+
"\t": "\\t",
308+
};
309+
310+
return value.replace(/["\\\b\f\n\r\t]/g, (char) => ESCAPE_MAP[char]);
311+
}

0 commit comments

Comments
 (0)