Skip to content

Commit

Permalink
fix: use papaparse to parse csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
cyntler committed Nov 22, 2022
1 parent 68ad824 commit fa61995
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
39 changes: 28 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"jest": {
"moduleNameMapper": {
"^csv-parse/browser/esm": "<rootDir>/node_modules/csv-parse/dist/cjs/index.cjs"
}
},
"engines": {
"node": ">=12.0.0"
},
Expand All @@ -38,8 +33,9 @@
},
"dependencies": {
"@types/mustache": "^4.2.1",
"csv-parse": "^5.3.2",
"@types/papaparse": "^5.3.5",
"mustache": "^4.2.0",
"papaparse": "^5.3.2",
"react-pdf": "5.7.2",
"styled-components": "^5.3.6",
"wl-msg-reader": "^0.2.0"
Expand Down
20 changes: 8 additions & 12 deletions src/plugins/csv/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { parse } from "csv-parse/browser/esm";
import { parse } from "papaparse";
import { DocRenderer } from "../..";
import { textFileLoader } from "../../utils/fileLoaders";

Expand All @@ -11,17 +11,13 @@ const CSVRenderer: DocRenderer = ({

useEffect(() => {
if (currentDocument?.fileData) {
parse(
currentDocument.fileData as string,
{
delimiter: config?.csvDelimiter ?? ",",
},
(err, records) => {
if (!err && records) {
setRows(records);
}
}
);
const parseResult = parse(currentDocument.fileData as string, {
delimiter: config?.csvDelimiter ?? ",",
});

if (!parseResult.errors?.length && parseResult.data) {
setRows(parseResult.data as string[][]);
}
}
}, [currentDocument, config?.csvDelimiter]);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react",
Expand Down

0 comments on commit fa61995

Please sign in to comment.