Skip to content

Commit

Permalink
fix(assert): rewrite dedent with for-loop (#117)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bartveneman committed May 31, 2021
1 parent 1d75357 commit b84c0a4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/assert.js
Expand Up @@ -2,9 +2,10 @@ import { dequal } from 'dequal';
import { compare, lines } from 'uvu/diff';

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

export class Assertion extends Error {
Expand Down

0 comments on commit b84c0a4

Please sign in to comment.