Skip to content

Commit 855678a

Browse files
committed
add fetch csv data function and boilerplate method for validating time
data
1 parent f5eb94e commit 855678a

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"immutable-transform-matrix": "^0.6.0",
1818
"lodash": "^4.17.20",
1919
"moment": "^2.29.1",
20+
"papaparse": "^5.3.0",
2021
"react": "^16.13.1",
2122
"react-dom": "^16.13.1",
2223
"react-measure": "^2.5.2",

src/components/ReactTimeSeries/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import Measure from "react-measure"
99
import MainLayout from "../MainLayout"
1010

1111
import fetchAudioData from "../../utils/fetch-audio-data"
12+
import fetchCSVData from "../../utils/fetch-csv-data"
13+
14+
import validateTimeData from "../../utils/validate-time-data"
1215

1316
const emptyAr = []
1417

@@ -51,9 +54,10 @@ export const ReactTimeSeriesWithoutContext = ({
5154

5255
const timeData = useAsyncMemo(
5356
async () => {
54-
if (sampleTimeData) return sampleTimeData
55-
return await fetchAudioData(audioUrl)
56-
// TODO load audioUrl
57+
if (sampleTimeData) return validateTimeData(sampleTimeData)
58+
if (audioUrl) return validateTimeData(await fetchAudioData(audioUrl))
59+
if (csvUrl) return validateTimeData(await fetchCSVData(csvUrl))
60+
return []
5761
// TODO load csvUrl
5862
},
5963
[sampleTimeData, audioUrl, csvUrl],

src/utils/fetch-csv-data.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Papa from "papaparse"
2+
3+
const possibleTimeHeaders = ["time", "date", "t"]
4+
5+
export default async (csvUrl) => {
6+
const csvText = await fetch(csvUrl).then((res) => res.text())
7+
8+
const parseResult = Papa.parse(csvText, { header: true })
9+
10+
const header = parseResult.meta.fields
11+
12+
if (!possibleTimeHeaders.some((columnName) => header.includes(columnName))) {
13+
throw new Error(
14+
`No time fields in header. Acceptable fields: ${possibleTimeHeaders.join(
15+
","
16+
)}`
17+
)
18+
}
19+
20+
if (!header.includes("time")) {
21+
for (const row of parseResult.data) {
22+
row.time = row.date || row.t
23+
}
24+
}
25+
26+
for (const row of parseResult.data) {
27+
row.time = new Date(row.time).valueOf()
28+
}
29+
30+
return parseResult.data
31+
}

src/utils/validate-time-data.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default (timeData) => {
2+
return timeData
3+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11882,6 +11882,11 @@ pako@~1.0.5:
1188211882
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.11.tgz#6c9599d340d54dfd3946380252a35705a6b992bf"
1188311883
integrity sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==
1188411884

11885+
papaparse@^5.3.0:
11886+
version "5.3.0"
11887+
resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.3.0.tgz#ab1702feb96e79ab4309652f36db9536563ad05a"
11888+
integrity sha512-Lb7jN/4bTpiuGPrYy4tkKoUS8sTki8zacB5ke1p5zolhcSE4TlWgrlsxjrDTbG/dFVh07ck7X36hUf/b5V68pg==
11889+
1188511890
parallel-transform@^1.1.0:
1188611891
version "1.2.0"
1188711892
resolved "https://registry.yarnpkg.com/parallel-transform/-/parallel-transform-1.2.0.tgz#9049ca37d6cb2182c3b1d2c720be94d14a5814fc"

0 commit comments

Comments
 (0)