-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
46 lines (42 loc) · 1.18 KB
/
index.js
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
const withElevation = require('../withElevation');
const MountainHub = require('./mountainhub');
const SnowPilot = require('./snowpilot');
const RegObs = require('./regobs');
// 604800000
const ONE_WEEK = 604800000;
const retrieveObservation = async function (
provider,
startDate = undefined,
endDate = undefined
) {
try {
startDate =
parseDate(startDate) || new Date(new Date().getTime() - ONE_WEEK);
endDate = parseDate(endDate) || new Date(new Date().getTime());
const rawData = await provider.rawData(startDate, endDate);
let data = rawData.map(provider.parseData).filter((x) => x);
data = await withElevation(data);
return data;
} catch (error) {
return [];
}
};
const parseDate = (date) => {
date = new Date(date);
return date instanceof Date && !isNaN(date) ? date : null;
};
module.exports = {
retrieveObservations: async function (
providers,
startDate = undefined,
endDate = undefined
) {
const data = await Promise.all(
providers.map((provider) =>
retrieveObservation(provider, startDate, endDate)
)
);
return [].concat.apply([], data);
},
providers: [MountainHub, SnowPilot, RegObs]
};