-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
164 lines (143 loc) · 4.81 KB
/
functions.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
export const helpers = {
celsiusToFarenheit: (temperature) => {
if (Number.isInteger(temperature)) {
return (temperature * 9) / 5 + 32;
} else {
return null
}
},
maxIconOccurency: (objWithIcons) => {
let maxIcon = '';
let maxOccunrency = 0;
for (let icon in objWithIcons) {
if (objWithIcons[icon] > maxOccunrency) {
maxOccunrency = objWithIcons[icon];
maxIcon = icon;
}
}
return maxIcon;
},
countIconsForecast: (data) => {
if (!(typeof data === 'object' && data !== null)) {
return null;
}
let iconsForecast = {};
for (let i = 0; i < data.length; i++) {
if (iconsForecast[data[i]] == undefined) {
iconsForecast[data[i]] = 1;
} else {
iconsForecast[data[i]] += 1;
}
}
return iconsForecast;
},
// show error when there is an issue with geolocation service
showError: error => {
notificationElement.style.display = "block";
notificationElement.innerHTML = `<p>${error.message} </p>`;
},
getDataNextDay: item => {
try {
return new Date(item)
.toDateString("MMMM")
.split(" ")
.slice(0, 3)
.join(" ");
}
catch (error) {
return 'Invalid Date';
}
},
updateTime: timeDate => {
try {
return timeDate
.toString()
.split(" ")
.slice(0, 5)
.join(" ");
}
catch (error) {
return 'Time format is invalid';
}
},
updateDate: updateTime => {
try {
return updateTime
.split(" ")
.slice(0, 3)
.join(" ");
}
catch (error) {
return 'Invalid time';
}
},
updateHour: updateTime => {
try {
return updateTime
.split(" ")
.slice(4)
.join(" ");
}
catch (error) {
return 'Invalid time';
}
},
updateWeatherTime: (weather, data) => {
var timeDate = new Date(0);
timeDate.setUTCSeconds(data);
const updateTime = helpers.updateTime(timeDate);
weather.date = helpers.updateDate(updateTime);
weather.hour = helpers.updateHour(updateTime);
},
updateWeatherData: (weather, data, Kelvin) => {
weather.temperature.value = Math.floor(data.main.temp - Kelvin);
weather.description = data.weather[0].description;
weather.iconId = data.weather[0].icon;
},
updateWeatherGeolocation: (weather, data) => {
weather.city = data.name;
weather.country = data.sys.country;
},
updateWeather: (weather, data, Kelvin) => {
helpers.updateWeatherData(weather, data, Kelvin);
helpers.updateWeatherGeolocation(weather, data);
helpers.updateWeatherTime(weather, data.dt);
},
getDataForecast: forecastList => {
let container = {};
for (let i = 0; i < forecastList.length; i++) {
const dateOfForecast = forecastList[i].dt_txt.split(' ').slice(0, 1);
if (!container.hasOwnProperty(dateOfForecast)) {
container[dateOfForecast] = [];
}
container[dateOfForecast].push(
forecastList[i].main.temp_min,
forecastList[i].main.temp_max
);
}
return Object.entries(container).slice(0, 4);
},
updateTreeDayForecast: (temperaturesData, Kelvin) => {
for (const key in temperaturesData) {
const minTemp = Math.min(...temperaturesData[key][1]);
const maxTemp = Math.max(...temperaturesData[key][1]);
temperaturesData[key][1] = Math.floor(minTemp - Kelvin);
temperaturesData[key][2] = Math.floor(maxTemp - Kelvin);
}
},
getWeatherForecast: (data, Kelvin) => {
const temperaturesData = helpers.getDataForecast(data.list);
helpers.updateTreeDayForecast(temperaturesData, Kelvin);
return {
'date1': helpers.getDataNextDay(temperaturesData[1][0]),
'date2': helpers.getDataNextDay(temperaturesData[2][0]),
'date3': helpers.getDataNextDay(temperaturesData[3][0]),
'minTemp1': JSON.stringify(temperaturesData[1][1]),
'maxTemp1': JSON.stringify(temperaturesData[1][2]),
'minTemp2': JSON.stringify(temperaturesData[2][1]),
'maxTemp2': JSON.stringify(temperaturesData[2][2]),
'minTemp3': JSON.stringify(temperaturesData[3][1]),
'maxTemp3': JSON.stringify(temperaturesData[3][2]),
}
}
}