Skip to content

Commit cfff3b4

Browse files
aduh95targos
authored andcommitted
assert: refactor to avoid unsafe array iteration
PR-URL: #37344 Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent f5541dd commit cfff3b4

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎lib/internal/assert/assertion_error.js‎

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

316+
functionaddEllipsis(string){
317+
constlines=StringPrototypeSplit(string,'\n',11);
318+
if(lines.length>10){
319+
lines.length=10;
320+
return`${ArrayPrototypeJoin(lines,'\n')}\n...`;
321+
}elseif(string.length>512){
322+
return`${StringPrototypeSlice(string,512)}...`;
323+
}
324+
returnstring;
325+
}
326+
316327
classAssertionErrorextendsError{
317328
constructor(options){
318329
if(typeofoptions!=='object'||options===null){
@@ -469,16 +480,11 @@ class AssertionError extends Error {
469480
consttmpActual=this.actual;
470481
consttmpExpected=this.expected;
471482

472-
for(constnameof['actual','expected']){
473-
if(typeofthis[name]==='string'){
474-
constlines=StringPrototypeSplit(this[name],'\n');
475-
if(lines.length>10){
476-
lines.length=10;
477-
this[name]=`${ArrayPrototypeJoin(lines,'\n')}\n...`;
478-
}elseif(this[name].length>512){
479-
this[name]=`${StringPrototypeSlice(this[name],512)}...`;
480-
}
481-
}
483+
if(typeofthis.actual==='string'){
484+
this.actual=addEllipsis(this.actual);
485+
}
486+
if(typeofthis.expected==='string'){
487+
this.expected=addEllipsis(this.expected);
482488
}
483489

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

0 commit comments

Comments
 (0)