Skip to content

Commit 5157b25

Browse files
committed
assert: refactor to avoid unsafe array iteration
1 parent 521c08d commit 5157b25

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ function createErrDiff(actual, expected, operator) {
313313
return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}${indicator}`;
314314
}
315315

316+
function addEllipsis(string) {
317+
const lines = StringPrototypeSplit(string, '\n');
318+
if (lines.length > 10) {
319+
lines.length = 10;
320+
return `${ArrayPrototypeJoin(lines, '\n')}\n...`;
321+
} else if (string.length > 512) {
322+
return `${StringPrototypeSlice(string, 512)}...`;
323+
}
324+
}
325+
316326
class AssertionError extends Error {
317327
constructor(options) {
318328
validateObject(options, 'options');
@@ -467,16 +477,11 @@ class AssertionError extends Error {
467477
const tmpActual = this.actual;
468478
const tmpExpected = this.expected;
469479

470-
for (const name of ['actual', 'expected']) {
471-
if (typeof this[name] === 'string') {
472-
const lines = StringPrototypeSplit(this[name], '\n');
473-
if (lines.length > 10) {
474-
lines.length = 10;
475-
this[name] = `${ArrayPrototypeJoin(lines, '\n')}\n...`;
476-
} else if (this[name].length > 512) {
477-
this[name] = `${StringPrototypeSlice(this[name], 512)}...`;
478-
}
479-
}
480+
if (typeof this.actual === 'string') {
481+
this.actual = addEllipsis(this.actual);
482+
}
483+
if (typeof this.expected === 'string') {
484+
this.expected = addEllipsis(this.expected);
480485
}
481486

482487
// This limits the `actual` and `expected` property default inspection to

0 commit comments

Comments
 (0)