Skip to content

Commit 817104f

Browse files
authored
Stack overflow error when opening large CSV files ("Maximum call stack size exceeded") (#70)
* Refactor to use reduce for max column calculation * Refactor column count calculation in CsvEditorProvider
1 parent 33dc8ac commit 817104f

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/CsvEditorProvider.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class CsvEditorController {
440440
const text = this.document.getText();
441441
const result = Papa.parse(text, { dynamicTyping: false, delimiter: separator });
442442
const data = result.data as string[][];
443-
const numColumns = Math.max(...data.map(r => r.length), 0);
443+
const numColumns = data.reduce((max, r) => Math.max(max, r.length), 0);
444444
const newRow = Array(numColumns).fill('');
445445
if (index > data.length) {
446446
while (data.length < index) data.push(Array(numColumns).fill(''));
@@ -466,7 +466,7 @@ class CsvEditorController {
466466
const text = this.document.getText();
467467
const result = Papa.parse(text, { dynamicTyping: false, delimiter: separator });
468468
const data = result.data as string[][];
469-
const numColumns = Math.max(...data.map(r => r.length), 0);
469+
const numColumns = data.reduce((max, r) => Math.max(max, r.length), 0);
470470
for (let k = 0; k < count; k++) {
471471
const newRow = Array(numColumns).fill('');
472472
if (index > data.length) {
@@ -603,7 +603,7 @@ class CsvEditorController {
603603
bodyData = data.slice(offset);
604604
}
605605
const visibleForWidth = headerFlag ? [headerRow, ...bodyData] : bodyData;
606-
let numColumns = Math.max(...visibleForWidth.map(row => row.length), 0);
606+
let numColumns = visibleForWidth.reduce((max, row) => Math.max(max, row.length), 0);
607607
if (numColumns === 0) numColumns = 1; // ensure at least 1 column for the virtual row
608608

609609
const columnData = Array.from({ length: numColumns }, (_, i) => bodyData.map(row => row[i] || ''));
@@ -743,11 +743,7 @@ class CsvEditorController {
743743
return true; // with only one row visible, lean toward header
744744
}
745745

746-
const numColumns = Math.max(
747-
headerRow.length,
748-
...body.map(r => r.length),
749-
0
750-
);
746+
const numColumns = body.reduce((max, r) => Math.max(max, r.length), Math.max(headerRow.length, 0));
751747
const bodyColData = Array.from({ length: numColumns }, (_, i) => body.map(r => r[i] || ''));
752748
const bodyTypes = bodyColData.map(col => this.estimateColumnDataType(col));
753749
const headerTypes = Array.from({ length: numColumns }, (_, i) => this.estimateColumnDataType([headerRow[i] || '']));
@@ -856,7 +852,7 @@ class CsvEditorController {
856852
// ───────────── Utilities ─────────────
857853

858854
private computeColumnWidths(data: string[][]): number[] {
859-
const numColumns = Math.max(...data.map(row => row.length), 0);
855+
const numColumns = data.reduce((max, row) => Math.max(max, row.length), 0);
860856
const widths = Array(numColumns).fill(0);
861857
for (const row of data) {
862858
for (let i = 0; i < numColumns; i++){

0 commit comments

Comments
 (0)