-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForecastArray.js
86 lines (82 loc) · 2.26 KB
/
ForecastArray.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @classdesc An extended Array with getters for extracting specific forecast data.
* Getters: temperatures, precipitation, winds
*/
class ForecastArray extends Array {
/**
* @returns {Object[]} An array of temperature data: value (actual temp), humidex, windChill.
*/
get temperatures() {
if (this[0].hasOwnProperty('day')) {
return this.map(forecast => (
{
day: forecast.day,
value: forecast.temperatures?.temperature?.value,
humidex: forecast.humidex?.value,
windChill: forecast.windChill?.calculated?.value
}
));
}
if (this[0].hasOwnProperty('hour')) {
return this.map(forecast => (
{
hour: forecast.hour,
value: forecast.temperature?.value,
humidex: forecast.humidex?.value,
windChill: forecast.windChill?.value
}
));
}
}
/**
* @returns {Object[]} An array of precipitation data.
* Weekly contains pop (chance of) and type.
* Hourly contains lop (chance of) and condition.
*/
get precipitation() {
if (this[0].hasOwnProperty('day')) {
return this.map(forecast => (
{
day: forecast.day,
pop: forecast.abbreviatedForecast?.pop?.value,
type: forecast.precipitation?.precipType?.value
}
));
}
if (this[0].hasOwnProperty('hour')) {
return this.map(forecast => (
{
hour: forecast.hour,
condition: forecast.condition,
lop: forecast.lop?.value
}
));
}
}
/**
* @returns {Object[]} An array of wind data: speed, gust, direction.
*/
get winds() {
if (this[0].hasOwnProperty('day')) {
return this.map(forecast => (
{
day: forecast.day,
speed: forecast.winds?.wind?.[1]?.speed?.value,
gust: forecast.winds?.wind?.[1]?.gust?.value,
direction: forecast.winds?.wind?.[1]?.direction
}
));
}
if (this[0].hasOwnProperty('hour')) {
return this.map(forecast => (
{
hour: forecast.hour,
speed: forecast.wind?.speed?.value,
gust: forecast.wind?.gust?.value,
direction: forecast.wind?.direction?.value
}
));
}
}
}
module.exports = ForecastArray;