Skip to content

Commit e53c87f

Browse files
regex is used to split strings instead of separating them by ,
Example:'"Adri, Doe", "24,4", Paraguay' => ['"Adri, Doe"','"24,4"',' Paraguay']
1 parent 16c2ef3 commit e53c87f

File tree

1 file changed

+12
-5
lines changed
  • src/common/components/mock-components/front-rich-components/table

1 file changed

+12
-5
lines changed

src/common/components/mock-components/front-rich-components/table/table.utils.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//Example:'"Adri, Doe", "24,4", Paraguay' => ['"Adri, Doe"','"24,4"',' Paraguay']
2+
const divideIntoColumns = (row: string): string[] =>
3+
row.match(/\s*"([^"]*)"|\s*([^,]+)/g) || [];
14
export const knowMaxColumns = (rows: string[]): number => {
25
return rows.reduce((maxColumns, row) => {
3-
const columns = row.split(',').length;
6+
const columns = divideIntoColumns(row).length;
47
return columns > maxColumns ? columns : maxColumns;
58
}, 0);
69
};
@@ -10,7 +13,7 @@ export const parseCSVRowsIntoArray = (csvContent: string): string[] => {
1013
const maxColumns = knowMaxColumns(arrayRows);
1114

1215
arrayRows = arrayRows.map(row => {
13-
const currentColumnCount = row.split(',').length;
16+
const currentColumnCount = divideIntoColumns(row).length;
1417

1518
// If a row is empty, return a string of commas
1619
if (currentColumnCount === 0) {
@@ -23,7 +26,7 @@ export const parseCSVRowsIntoArray = (csvContent: string): string[] => {
2326
}
2427

2528
// If a row has less columns than maxColumns, add commas at the end
26-
if (currentColumnCount < maxColumns) {
29+
if (currentColumnCount <= maxColumns) {
2730
return row + ','.repeat(maxColumns - currentColumnCount); // Añadir comas al final
2831
}
2932

@@ -71,10 +74,14 @@ export const extractDataRows = (
7174
return widthRow
7275
? rows
7376
.slice(1, rows.length - 1)
74-
.map(row => row.split(',').map(cell => cell.trim()))
77+
.map(row =>
78+
divideIntoColumns(row).map(value => value.replace(/"/g, '').trim())
79+
)
7580
: rows
7681
.slice(1, rows.length)
77-
.map(row => row.split(',').map(cell => cell.trim()));
82+
.map(row =>
83+
divideIntoColumns(row).map(value => value.replace(/"/g, '').trim())
84+
);
7885
};
7986

8087
export const extractWidthRow = (

0 commit comments

Comments
 (0)