From 8e9d85f10f34dfe5f468f3576beee746366c924b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 27 Sep 2021 21:04:30 -0700 Subject: [PATCH] Add fetchCSV() example macro --- examples/macros/fetchCSV.tsx | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/macros/fetchCSV.tsx diff --git a/examples/macros/fetchCSV.tsx b/examples/macros/fetchCSV.tsx new file mode 100644 index 00000000000000..0709fd50053ab2 --- /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 = ( + + {rows.map((columns) => ( + + {columnIndices.map((columnIndex) => ( + + ))} + + ))} + + ); + console.timeEnd("Render"); + console.timeEnd("fetchCSV Total"); + return value; +}