UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

1 lines 6.67 kB
{"version":3,"file":"utils.cjs","sources":["../../../components/datepicker/utils.js"],"sourcesContent":["import {\n startOfWeek, addDays, getMonth, isEqual,\n addMonths, startOfMonth, getDay, getDate,\n subMonths, endOfMonth,\n} from 'date-fns';\nimport { WEEK_START } from '@/components/datepicker/datepicker_constants.js';\n\nconst _parsedGetDate = (value) => (value ? new Date(value) : new Date());\n\n/**\n * Get 7 days from the provided start date, month is used to check\n * whether the date is from the specified month or in the offset\n */\nconst getWeekDays = (startDay, month, selectedDay) => {\n const startDate = _parsedGetDate(JSON.parse(JSON.stringify(startDay)));\n const dates = [];\n for (let i = 0; i < 7; i++) {\n const next = addDays(startDate, i);\n const isNext = getMonth(next) !== month;\n dates.push({\n text: next.getDate(),\n value: next,\n currentMonth: !isNext,\n isFirstDayOfMonth: next.getDate() === 1 && !isNext,\n // will be selected if the date is the same as the selected day and is from the current month\n selected: selectedDay ? (next.getDate() === selectedDay && !isNext) : false,\n });\n }\n return dates;\n};\n\nconst isDateEqual = (date, dateToCompare) => {\n if (!date || !dateToCompare) {\n return false;\n }\n return isEqual(date, dateToCompare);\n};\n\n/**\n * Get days for the calendar to be displayed in a table grouped by weeks\n */\nexport const getCalendarDays = (month, year, selectedDay) => {\n const weeks = [];\n const firstDate = _parsedGetDate(new Date(year, month));\n const lastDate = _parsedGetDate(new Date(year, month + 1, 0));\n\n const weekStartsOn = WEEK_START;\n\n const firstDateInCalendar = startOfWeek(firstDate, { weekStartsOn });\n\n const addDaysToWeek = (date) => {\n const days = getWeekDays(date, month, selectedDay);\n\n weeks.push({ days });\n\n if (\n !weeks[weeks.length - 1].days.some((day) =>\n isDateEqual(day.value, lastDate),\n )\n ) {\n const nextDate = addDays(date, 7);\n addDaysToWeek(nextDate);\n }\n };\n\n addDaysToWeek(firstDateInCalendar);\n\n return weeks;\n};\n\n/**\n * Generate week day names based on locale and in order specified in week start\n */\nexport const getWeekDayNames = (locale, weekStart) => {\n // Get list in order from sun ... sat\n const days = [1, 2, 3, 4, 5, 6, 7].map((day) => {\n return new Intl.DateTimeFormat(locale, { weekday: 'short', timeZone: 'UTC' })\n .format(new Date(`2017-01-0${day}T00:00:00+00:00`))\n .slice(0, 2);\n });\n\n // Get days that are in order before specified week start\n const beforeWeekStart = days.slice(0, weekStart);\n // Get days that are in order after specified week start\n const afterWeekStart = days.slice(weekStart + 1, days.length);\n\n // return them in correct order\n return [days[weekStart]].concat(...afterWeekStart).concat(...beforeWeekStart);\n};\n\nexport const formatMonth = (month, monthFormat, locale) => {\n return new Intl.DateTimeFormat(locale, { month: monthFormat }).format(new Date(2000, month, 1));\n};\n\nexport const calculateNextFocusDate = (currentDate) => {\n const date = new Date(currentDate);\n const currentWeekday = getDay(date);\n const nextMonthDate = addMonths(date, 1);\n const nextMonthStart = startOfMonth(nextMonthDate);\n const nextMonthStartWeekday = getDay(nextMonthStart);\n\n const dayDifference = (currentWeekday - nextMonthStartWeekday + 7) % 7;\n\n // Add the difference in days to the first day of the next month\n const focusDate = addDays(nextMonthStart, dayDifference);\n\n // Returns only the day of the month\n return getDate(focusDate);\n};\n\nexport const calculatePrevFocusDate = (currentDate) => {\n const date = new Date(currentDate);\n const currentWeekday = getDay(date);\n\n // Move to the last day of the previous month\n const lastDayOfPrevMonth = endOfMonth(subMonths(date, 1));\n let focusDate = lastDayOfPrevMonth;\n\n // Adjust to the same weekday in the last week of the previous month\n while (getDay(focusDate) !== currentWeekday) {\n focusDate = addDays(focusDate, -1);\n }\n\n // Returns only the day of the month\n return getDate(focusDate);\n};\n"],"names":["addDays","getMonth","isEqual","WEEK_START","startOfWeek","getDay","addMonths","startOfMonth","getDate","endOfMonth","subMonths"],"mappings":";;;;AAOA,MAAM,iBAAiB,CAAC,UAAW,QAAQ,IAAI,KAAK,KAAK,IAAI,oBAAI,KAAI;AAMrE,MAAM,cAAc,CAAC,UAAU,OAAO,gBAAgB;AACpD,QAAM,YAAY,eAAe,KA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