Skip to content

Commit b84c0a4

Browse files
authored
fix(assert): rewrite dedent with for-loop (#117)
Replaces `arr.map()` with a for-loop to avoid a "Max call stack size exceeded" error for fixtures with *many* lines to dedent. Closes #115
1 parent 1d75357 commit b84c0a4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/assert.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { dequal } from 'dequal';
22
import { compare, lines } from 'uvu/diff';
33

44
function dedent(str) {
5-
let arr = str.match(/^[ \t]*(?=\S)/gm);
6-
let min = !!arr && Math.min(...arr.map(x => x.length));
7-
return (!arr || !min) ? str : str.replace(new RegExp(`^[ \\t]{${min}}`, 'gm'), '');
5+
let arr = str.match(/^[ \t]*(?=\S)/gm);
6+
let i = 0, min = 1/0, len = (arr||[]).length;
7+
for (; i < len; i++) min = Math.min(min, arr[i].length);
8+
return len && min ? str.replace(new RegExp(`^[ \\t]{${min}}`, 'gm'), '') : str;
89
}
910

1011
export class Assertion extends Error {

0 commit comments

Comments
 (0)