pmtpenza

Untitled

Jul 5th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import * as moment from 'moment'; // add this 1 of 4
  2. import 'moment/locale/en-gb';
  3. import {
  4. SUN,
  5. THUNDER,
  6. CLOUD,
  7. DRIZZLE,
  8. RAIN,
  9. SNOW
  10. } from './../constants/weatherIconState';
  11.  
  12. export const getWeatherState = weatherState => {
  13. const { id } = weatherState[0];
  14. if (id < 300) {
  15. return THUNDER;
  16. } else if (id < 400) {
  17. return DRIZZLE;
  18. } else if (id < 600) {
  19. return RAIN;
  20. } else if (id < 700) {
  21. return SNOW;
  22. } else if (id === 800) {
  23. return SUN;
  24. } else {
  25. return CLOUD;
  26. }
  27. };
  28.  
  29. const transformWeather = weatherData => {
  30. const {
  31. humidity,
  32. temp
  33. } = weatherData.main;
  34. const {
  35. speed
  36. } = weatherData.wind;
  37. const weatherState = getWeatherState(weatherData.weather);
  38.  
  39. return {
  40. humidity,
  41. temperature: Math.floor(temp),
  42. weatherState,
  43. wind: speed,
  44. };
  45. };
  46. const getForecastAverage = collapsedForecast => {
  47. return Object.keys(collapsedForecast).map((day) => {
  48. const collapsedDay = {
  49. temperature: 0,
  50. humidity: 0,
  51. wind: 0,
  52. weatherState: '',
  53. description: 'string'
  54. };
  55. const len = collapsedForecast[day].length;
  56. collapsedForecast[day].forEach((val) => {
  57. collapsedDay.humidity += val.data.humidity;
  58. collapsedDay.temperature += val.data.temperature;
  59. collapsedDay.wind += val.data.wind;
  60. collapsedDay.weatherState = val.data.weatherState;
  61. });
  62.  
  63. return {
  64. temperature: Math.floor(collapsedDay.temperature / len),
  65. humidity: Math.floor(collapsedDay.humidity / len),
  66. wind: Math.floor(collapsedDay.wind / len),
  67. weatherState: collapsedDay.weatherState,
  68. weekDay: day
  69. };
  70. });
  71. };
  72.  
  73. const collapseForecastByDay = forecastData => {
  74. const collapsedForecast = {};
  75. forecastData.forEach(forecast => {
  76. if (!Array.isArray(collapsedForecast[forecast.weekDay])) {
  77. collapsedForecast[forecast.weekDay] = [];
  78. }
  79. collapsedForecast[forecast.weekDay].push(forecast);
  80. });
  81. return getForecastAverage(collapsedForecast);
  82. };
  83.  
  84.  
  85. export const transformForecast = data => {
  86. const mappedForecast = data.list.map(item => ({
  87. weekDay: moment.unix(item.dt).format('dddd'),
  88. data: transformWeather(item)
  89. }));
  90. return collapseForecastByDay(mappedForecast);
  91. };
Advertisement
Add Comment
Please, Sign In to add comment