Guest User

Untitled

a guest
May 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. // Javascript, ES6
  2.  
  3. const checkIfCanGetOpinion = timeStamps => {
  4. var statistics = {
  5. threeDaysInRow: false,
  6. sessions: 0
  7. };
  8. // Create array of dates
  9. var datesArray = [];
  10. timeStamps.forEach(stamp => {
  11. const stampToUTC = stamp.replace(" ", "T") + "Z";
  12. const date = new Date(stampToUTC);
  13. datesArray.push(date);
  14. });
  15. // Transform dates into number of days
  16. const dayInMiliseconds = 24 * 60 * 60 * 1000;
  17. var datesToDays = datesArray.map(key => {
  18. return Date.parse(key) / dayInMiliseconds;
  19. });
  20. // above datesToDays array looks more or less like this: [17235.123123, 17235.123123, 17236.123123, 17236.123123, 17237.123123, 17237.123123]
  21. const firstDay = datesToDays[0];
  22. var daysInRow = datesToDays.map(day => {
  23. return Math.floor(day) - Math.floor(firstDay);
  24. });
  25. // remove duplicates
  26. daysInRow = daysInRow.filter(function(elem, pos, arr) {
  27. return arr.indexOf(elem) == pos;
  28. });
  29. // daysInRow array should be [0, 1, 2]
  30. if (daysInRow.join(",") === "0,1,2") {
  31. statistics.threeDaysInRow = true;
  32. } else {
  33. return false;
  34. }
  35. var sessionTimesInMiliseconds = datesArray.map(date => {
  36. return Date.parse(date);
  37. });
  38. // Substract the time of the previous session from this session to get time gaps between sessions
  39. var timeGapsBetweenSessions = [];
  40. const minGapLengthInMiliseconds = 30 * 60 * 1000;
  41. for (let i = 0; i < sessionTimesInMiliseconds.length - 1; i++) {
  42. var gap = sessionTimesInMiliseconds[i + 1] - sessionTimesInMiliseconds[i];
  43. timeGapsBetweenSessions.push(gap);
  44. }
  45. statistics.sessions += 1; // first session is already checked
  46. timeGapsBetweenSessions.forEach(gap => {
  47. if (gap >= minGapLengthInMiliseconds) statistics.sessions += 1;
  48. });
  49. if (statistics.threeDaysInRow === true && statistics.sessions >= 6) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. };
Add Comment
Please, Sign In to add comment