Skip to content

Commit 221c89f

Browse files
1.1.3 - upgrading csv parsing/sanitization process
1 parent f40cb6c commit 221c89f

File tree

10 files changed

+40
-25
lines changed

10 files changed

+40
-25
lines changed

component/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

component/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "active-table",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "Framework agnostic table component for editable data experience",
55
"main": "./dist/activeTable.js",
66
"module": "./dist/activeTable.js",

component/src/utils/outerTableComponents/files/CSV/CSVImport.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,24 @@ export class CSVImport {
77
return rowsOfData.map((row) => row.concat(Array(largestRowLength).fill('')).slice(0, largestRowLength));
88
}
99

10-
private static parseDataFromRow(row: string, rowsOfData: string[][], largestRowLength: number) {
11-
const data = row.split(',');
12-
if (data.length > 0) {
13-
rowsOfData.push(data);
14-
if (data.length > largestRowLength) largestRowLength = data.length;
10+
private static splitRow(row: string) {
11+
// Matches commas outside of double-quotes
12+
const regex = /("[^"]*"|[^,]+)(,|$)/g;
13+
const rowCells: string[] = [];
14+
row.replace(regex, (_, value) => {
15+
rowCells.push(value);
16+
// Return an empty string to continue the iteration
17+
return '';
18+
});
19+
20+
return rowCells;
21+
}
22+
23+
private static parseDataFromRow(row: string, cells: string[][], largestRowLength: number) {
24+
const rowCells = CSVImport.splitRow(row);
25+
if (rowCells.length > 0) {
26+
cells.push(rowCells);
27+
if (rowCells.length > largestRowLength) largestRowLength = rowCells.length;
1528
}
1629
return largestRowLength;
1730
}
@@ -20,12 +33,12 @@ export class CSVImport {
2033
private static parseCSV(csvText: string) {
2134
try {
2235
const rows = csvText.split(/\r\n|\n/) as string[];
23-
const rowsOfData: string[][] = [];
36+
const cells: string[][] = [];
2437
let largestRowLength = 0;
2538
rows.forEach((row) => {
26-
largestRowLength = CSVImport.parseDataFromRow(row, rowsOfData, largestRowLength);
39+
largestRowLength = CSVImport.parseDataFromRow(row, cells, largestRowLength);
2740
});
28-
return CSVImport.getPaddedArray(rowsOfData, largestRowLength);
41+
return CSVImport.getPaddedArray(cells, largestRowLength);
2942
} catch (errorMessage) {
3043
console.error('Incorrect format');
3144
return null;

component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class OverwriteCellsViaCSVOnPaste {
1313

1414
// prettier-ignore
1515
public static overwrite(at: ActiveTable,
16-
clipboardText: string, event: ClipboardEvent, rowIndex: number, columnIndex: number,) {
16+
clipboardText: string, event: ClipboardEvent, rowIndex: number, columnIndex: number) {
1717
event.preventDefault();
1818
const CSV = ParseCSVClipboardText.parse(clipboardText);
1919
OverwriteCellsViaCSVOnPaste.focusOriginalCellAfterProcess(at,

component/src/utils/paste/CSV/parseCSVClipboardText.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export class ParseCSVClipboardText {
3535
const linesOfText: string[] = processedText.split(newLine);
3636
return linesOfText.map((lineOfText: string) => {
3737
// row indexes in worksheets end with \\t\\t\\t\\t\\t
38-
return lineOfText.split(tab);
38+
const cells = lineOfText.split(tab);
39+
// when pasting data with ", it is parsed as \\"
40+
return cells.map((cell) => cell.replace(/\\"/g, ''));
3941
});
4042
}
4143
}

other-packages/react/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

other-packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "active-table-react",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "Active Table wrapper for React",
55
"main": "./dist/activeTable.js",
66
"module": "./dist/activeTable.js",

website/docs/docs/installation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ npm install active-table-react
1919
Access the component via CDN:
2020

2121
```
22-
https://unpkg.com/[email protected].2/dist/activeTable.bundle.js
22+
https://unpkg.com/[email protected].3/dist/activeTable.bundle.js
2323
```

website/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@docusaurus/preset-classic": "^2.3.0",
1919
"@docusaurus/theme-search-algolia": "^2.3.1",
2020
"@mdx-js/react": "^1.6.22",
21-
"active-table-react": "^1.1.2",
21+
"active-table-react": "^1.1.3",
2222
"clsx": "^1.2.1",
2323
"prism-react-renderer": "^1.3.5",
2424
"react": "^17.0.2",

0 commit comments

Comments
 (0)