UNPKG

@zohodesk/hooks

Version:

Unified Component Library - Hooks

62 lines (57 loc) 1.61 kB
import getAllMonthDaysCount from "./getAllMonthDaysCount"; import getCurrentValue from "./getCurrentValue"; export default function getMonthData(_ref) { let { day, date, month, year } = _ref; const { currentDate, currentMonth, currentYear } = getCurrentValue(); const datesCountOfMonths = getAllMonthDaysCount(year); let weeksList = []; let week = []; let monthStartDay = day; let monthEndDay = datesCountOfMonths[month - 1]; // Finding month start if (date !== 1) { monthStartDay = Math.abs(7 - (date - 1) % 7 + day) % 7; } // Creating Dates for (let i = 1; i <= 42; i++) { if (i <= monthStartDay) { week.push({ year: month == 1 ? year - 1 : year, month: month == 1 ? 12 : month - 1, date: datesCountOfMonths[month == 1 ? 11 : month - 2] - monthStartDay + i, day: (i - 1) % 7, isInactive: true }); } else if (i > monthEndDay + monthStartDay) { week.push({ year: month == 12 ? year + 1 : year, month: month == 12 ? 1 : month + 1, date: i - (monthEndDay + monthStartDay), day: (i - 1) % 7, isInactive: true }); } else { week.push({ year: year, month: month, date: i - monthStartDay, day: (i - 1) % 7, isToday: currentDate == i - monthStartDay && currentMonth == month - 1 && currentYear == year, isActive: date === i - monthStartDay, isInactive: false }); } if (week.length == 7) { weeksList.push(week); week = []; } } return weeksList; }