diff options
author | 2021-09-27 21:04:30 -0700 | |
---|---|---|
committer | 2021-09-27 21:04:30 -0700 | |
commit | 8e9d85f10f34dfe5f468f3576beee746366c924b (patch) | |
tree | 1d4109d5a893a0dac9656daa610ec38ab4a5e99b /examples | |
parent | 70bae801dc12e2a704e9d1ee512d76a041590ebf (diff) | |
download | bun-8e9d85f10f34dfe5f468f3576beee746366c924b.tar.gz bun-8e9d85f10f34dfe5f468f3576beee746366c924b.tar.zst bun-8e9d85f10f34dfe5f468f3576beee746366c924b.zip |
Add fetchCSV() example macro
Diffstat (limited to 'examples')
-rw-r--r-- | examples/macros/fetchCSV.tsx | 46 |
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; +} |