Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function alignNumber(num) {
- return num < 10 ? `0${num}` : `${num}`;
- }
- function dateToString(date) {
- const years = date.getFullYear();
- const months = alignNumber(date.getMonth() + 1);
- const days = alignNumber(date.getDate());
- return `${years}-${months}-${days}`;
- }
- function groupWeeks(data) {
- const dateCounts = data.reduce((acc, item) => {
- const date = new Date(item.date);
- const monday = dateToString(
- new Date(date.setDate(date.getDate() - date.getDay() + 1))
- );
- if (!acc[monday]) {
- acc[monday] = 0;
- }
- acc[monday] += item.count;
- return acc;
- }, {});
- return Object.keys(dateCounts).reduce((acc, date) => {
- const item = {
- weekStart: date,
- count: dateCounts[date]
- };
- acc.push(item);
- return acc;
- }, []);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement