UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

85 lines (84 loc) 3.29 kB
import { startOfWeek, getDay, addMonths, startOfMonth, addDays, getDate, endOfMonth, subMonths, getMonth, isEqual } from "date-fns"; import { WEEK_START } from "./datepicker_constants.js"; const _parsedGetDate = (value) => value ? new Date(value) : /* @__PURE__ */ new Date(); const getWeekDays = (startDay, month, selectedDay) => { const startDate = _parsedGetDate(JSON.parse(JSON.stringify(startDay))); const dates = []; for (let i = 0; i < 7; i++) { const next = addDays(startDate, i); const isNext = getMonth(next) !== month; dates.push({ text: next.getDate(), value: next, currentMonth: !isNext, isFirstDayOfMonth: next.getDate() === 1 && !isNext, // will be selected if the date is the same as the selected day and is from the current month selected: selectedDay ? next.getDate() === selectedDay && !isNext : false }); } return dates; }; const isDateEqual = (date, dateToCompare) => { if (!date || !dateToCompare) { return false; } return isEqual(date, dateToCompare); }; const getCalendarDays = (month, year, selectedDay) => { const weeks = []; const firstDate = _parsedGetDate(new Date(year, month)); const lastDate = _parsedGetDate(new Date(year, month + 1, 0)); const weekStartsOn = WEEK_START; const firstDateInCalendar = startOfWeek(firstDate, { weekStartsOn }); const addDaysToWeek = (date) => { const days = getWeekDays(date, month, selectedDay); weeks.push({ days }); if (!weeks[weeks.length - 1].days.some( (day) => isDateEqual(day.value, lastDate) )) { const nextDate = addDays(date, 7); addDaysToWeek(nextDate); } }; addDaysToWeek(firstDateInCalendar); return weeks; }; const getWeekDayNames = (locale, weekStart) => { const days = [1, 2, 3, 4, 5, 6, 7].map((day) => { return new Intl.DateTimeFormat(locale, { weekday: "short", timeZone: "UTC" }).format(/* @__PURE__ */ new Date(`2017-01-0${day}T00:00:00+00:00`)).slice(0, 2); }); const beforeWeekStart = days.slice(0, weekStart); const afterWeekStart = days.slice(weekStart + 1, days.length); return [days[weekStart]].concat(...afterWeekStart).concat(...beforeWeekStart); }; const formatMonth = (month, monthFormat, locale) => { return new Intl.DateTimeFormat(locale, { month: monthFormat }).format(new Date(2e3, month, 1)); }; const calculateNextFocusDate = (currentDate) => { const date = new Date(currentDate); const currentWeekday = getDay(date); const nextMonthDate = addMonths(date, 1); const nextMonthStart = startOfMonth(nextMonthDate); const nextMonthStartWeekday = getDay(nextMonthStart); const dayDifference = (currentWeekday - nextMonthStartWeekday + 7) % 7; const focusDate = addDays(nextMonthStart, dayDifference); return getDate(focusDate); }; const calculatePrevFocusDate = (currentDate) => { const date = new Date(currentDate); const currentWeekday = getDay(date); const lastDayOfPrevMonth = endOfMonth(subMonths(date, 1)); let focusDate = lastDayOfPrevMonth; while (getDay(focusDate) !== currentWeekday) { focusDate = addDays(focusDate, -1); } return getDate(focusDate); }; export { calculateNextFocusDate, calculatePrevFocusDate, formatMonth, getCalendarDays, getWeekDayNames }; //# sourceMappingURL=utils.js.map