aboutsummaryrefslogtreecommitdiff
path: root/examples/macros/fetchCSV.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'examples/macros/fetchCSV.tsx')
-rw-r--r--examples/macros/fetchCSV.tsx46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/macros/fetchCSV.tsx b/examples/macros/fetchCSV.tsx
new file mode 100644
index 000000000..0709fd500
--- /dev/null
+++ b/examples/macros/fetchCSV.tsx
@@ -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;
+}