Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. function alignNumber(num) {
  2. return num < 10 ? `0${num}` : `${num}`;
  3. }
  4.  
  5. function dateToString(date) {
  6. const years = date.getFullYear();
  7. const months = alignNumber(date.getMonth() + 1);
  8. const days = alignNumber(date.getDate());
  9.  
  10. return `${years}-${months}-${days}`;
  11. }
  12.  
  13. function groupWeeks(data) {
  14. const dateCounts = data.reduce((acc, item) => {
  15. const date = new Date(item.date);
  16.  
  17. const monday = dateToString(
  18. new Date(date.setDate(date.getDate() - date.getDay() + 1))
  19. );
  20.  
  21. if (!acc[monday]) {
  22. acc[monday] = 0;
  23. }
  24.  
  25. acc[monday] += item.count;
  26. return acc;
  27. }, {});
  28.  
  29. return Object.keys(dateCounts).reduce((acc, date) => {
  30. const item = {
  31. weekStart: date,
  32. count: dateCounts[date]
  33. };
  34.  
  35. acc.push(item);
  36. return acc;
  37. }, []);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement