Skip to content

Commit c32dc6c

Browse files
authored
refactor: simplify value checks in parseTable and parseTextTable functions
1 parent 0b6ef2a commit c32dc6c

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/parse-table.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,18 @@ function parseTable(inputString) {
164164
columnTypes.forEach((type, colIndex) => {
165165
if (type === "numeric") {
166166
const values = data.map((row) => row[colIndex]).filter(value => value !== "");
167-
const allIntegers = values.every((value) => value && utils.isInt(value));
167+
const allIntegers = values.every((value) => utils.isInt(value));
168168
if (allIntegers) {
169169
columnTypes[colIndex] = "integer";
170170
}
171171
}
172172
});
173-
173+
174174
// Check if all values in a string column are boolean
175175
columnTypes.forEach((type, colIndex) => {
176176
if (type === "string") {
177177
const values = data.map((row) => row[colIndex]).filter(value => value !== "");
178-
const allBool = values.every((value) => value && utils.isBool(value));
178+
const allBool = values.every((value) => utils.isBool(value));
179179
if (allBool) {
180180
columnTypes[colIndex] = "boolean";
181181
}
@@ -186,7 +186,7 @@ function parseTable(inputString) {
186186
const convertedData = data.map((row) =>
187187
row.map((value, colIndex) =>
188188
columnTypes[colIndex] !== "string" &&
189-
columnTypes[colIndex] !== "boolean"
189+
columnTypes[colIndex] !== "boolean"
190190
? utils.convertValue(value)
191191
: value
192192
)
@@ -210,7 +210,7 @@ function parseTextTable(textString) {
210210

211211
// Delimiters: TAB (spreadsheets, IDEs), comma (CSV), semicolon (CSV), pipe (TSV)
212212
// or by spaces (fixed width)
213-
const patterns = [ '\\t|\\s\\t', ',', ';', '\\|', '\\s+' ];
213+
const patterns = ['\\t|\\s\\t', ',', ';', '\\|', '\\s+'];
214214
let results = [];
215215
let columns = [];
216216
// Finds the best pattern to split the table
@@ -220,7 +220,7 @@ function parseTextTable(textString) {
220220
let matrix = rows.map((row) => row.split(regex));
221221
let cols = getNumSplitRows(matrix, i);
222222
// Check if the pattern perfectly split all rows with same number of columns
223-
if ((cols[0] > 1) && (cols[0] >= len2) && cols[1] >= rlen) {
223+
if (cols[0] >= rlen && cols[1] >= rlen) {
224224
return matrix;
225225
}
226226
results[i] = matrix;
@@ -267,7 +267,7 @@ function getMaxCols(matrix) {
267267

268268
function getNumSplitRows(matrix, index) {
269269
let numRowSplit = 0;
270-
let numColsEqual = -1;
270+
let numColsEqual = 0;
271271
let numCols = -1;
272272
let numRows = matrix.length;
273273
for (let i = 0; i < numRows; i++) {
@@ -276,6 +276,7 @@ function getNumSplitRows(matrix, index) {
276276
numRowSplit += 1;
277277
if (numCols <= 0) {
278278
numCols = cols;
279+
numColsEqual = 1; // First row establishes the column count
279280
} else if (cols == numCols) {
280281
numColsEqual += 1;
281282
}

0 commit comments

Comments
 (0)