Skip to content

Commit eef6537

Browse files
ZvonimirZvonimir
authored andcommitted
Merge branch 'v8/develop' into feature/calculate-merkle-root-from-proof
2 parents a9455af + 42c9936 commit eef6537

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
@@ -242,8 +242,8 @@ export function groupNquadsBySubject(nquadsArray, sort = false) {
242242
const nestedPredicate = subject.predicate.value;
243243
const nestedObject =
244244
subject.object.termType === "Literal"
245-
? `"${subject.object.value}"`
246-
: `<${subject.object.value}>`;
245+
? `"${escapeLiteral(subject.object.value)}"`
246+
: `<${escapeLiteral(subject.object.value)}>`;
247247
subjectKey = `<<<${nestedSubject}> <${nestedPredicate}> ${nestedObject}>>`;
248248
} else {
249249
subjectKey = `<${subject.value}>`;
@@ -254,7 +254,9 @@ export function groupNquadsBySubject(nquadsArray, sort = false) {
254254
}
255255

256256
const objectValue =
257-
object.termType === "Literal" ? `"${object.value}"` : `<${object.value}>`;
257+
object.termType === "Literal"
258+
? `"${escapeLiteral(object.value)}"`
259+
: `<${escapeLiteral(object.value)}>`;
258260

259261
const quadString = `${subjectKey} <${predicate.value}> ${objectValue} .`;
260262
grouped[subjectKey].push(quadString);
@@ -378,3 +380,17 @@ export function generateMissingIdsForBlankNodes(nquadsArray) {
378380
function isEmptyObject(obj) {
379381
return Object.keys(obj).length === 0 && obj.constructor === Object;
380382
}
383+
384+
function escapeLiteral(value) {
385+
const ESCAPE_MAP = {
386+
'"': '\\"',
387+
"\\": "\\\\",
388+
"\b": "\\b",
389+
"\f": "\\f",
390+
"\n": "\\n",
391+
"\r": "\\r",
392+
"\t": "\\t",
393+
};
394+
395+
return value.replace(/["\\\b\f\n\r\t]/g, (char) => ESCAPE_MAP[char]);
396+
}

0 commit comments

Comments
 (0)