aboutsummaryrefslogtreecommitdiff
path: root/examples/macros/fetchCSV.tsx
blob: 55a12bc4242b5da27581c96e40ae7a0c2e2a1951 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Pappa from "papaparse";
// Example usage:
// const rows = fetchCSV(
//   "https://covid19.who.int/WHO-COVID-19-global-data.csv",
//   {
//     last: 100,
//     columns: ["New_cases", "Date_reported", "Country"],
//   }
// );
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;
}