Skip to content

Commit

Permalink
Add fetchCSV() example macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Sep 28, 2021
1 parent 70bae80 commit 8e9d85f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/macros/fetchCSV.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Pappa from "papaparse";
export async function fetchCSV(callExpression) {
console.time("fetchCSV Total");
const [
urlNode,
{
properties: { last: limit = 10, columns = [] },
},
] = callExpression.arguments;
const url = urlNode.get();

console.time("Fetch");
const response = await fetch(url);
const csvText = await response.text();
console.timeEnd("Fetch");

console.time("Parse");
let rows = Pappa.parse(csvText, { fastMode: true }).data;
console.timeEnd("Parse");

console.time("Render");
const columnIndices = new Array(columns.length);

for (let i = 0; i < columns.length; i++) {
columnIndices[i] = rows[0].indexOf(columns[i]);
}

rows = rows
.slice(Math.max(limit, rows.length) - limit)
.reverse()
.filter((columns) => columns.every(Boolean));
const value = (
<array>
{rows.map((columns) => (
<array>
{columnIndices.map((columnIndex) => (
<string value={columns[columnIndex]} />
))}
</array>
))}
</array>
);
console.timeEnd("Render");
console.timeEnd("fetchCSV Total");
return value;
}

0 comments on commit 8e9d85f

Please sign in to comment.