Skip to content

Commit a04f084

Browse files
committed
looking better
1 parent 5edee75 commit a04f084

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

Diff for: src/csvParser.ts

+35-7
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,42 @@ export function parseCSVWithPapa(csv: string): CsvData {
2828
}
2929
});
3030

31-
// convert cells to just their lengths
32-
const rowsOfCellLengthArrays = result.data.map(
33-
rowOfCells => (
34-
rowOfCells.map(cell => cell.length)
35-
)
36-
);
31+
// Step 2: Reconstruct raw text and capture value lengths
32+
let valueLengths: number[][] = [];
33+
let rows = csv.split(/\r?\n/); // Split raw content by newlines
34+
35+
rows.forEach((row) => {
36+
let inQuotes = false;
37+
let currentValue = '';
38+
let valueStart = 0;
39+
let rowLengths = [];
40+
41+
for (let i = 0; i < row.length; i++) {
42+
let char = row[i];
43+
44+
// Toggle inQuotes when encountering double quotes
45+
if (char === '"') {
46+
inQuotes = !inQuotes;
47+
}
48+
49+
// If we're not in quotes and hit a delimiter, calculate the value length
50+
if (char === delimiter && !inQuotes) {
51+
rowLengths.push(i - valueStart); // Length from start to current delimiter
52+
valueStart = i + 1; // Start of next value
53+
}
54+
}
55+
56+
// After the last delimiter, push the final value's length
57+
rowLengths.push(row.length - valueStart);
58+
59+
// Collect the row's value lengths
60+
valueLengths.push(rowLengths);
61+
});
62+
63+
console.log("Delimiter:", delimiter);
64+
console.log("Value lengths per row:", valueLengths);
3765

3866
// `result.data` contains the parsed array of arrays
39-
return {data: rowsOfCellLengthArrays, delimiter: delimiter};
67+
return {data: valueLengths, delimiter: delimiter};
4068
}
4169

0 commit comments

Comments
 (0)