@dialpad/dialtone-vue
Version:
Vue component library for Dialpad's design system Dialtone
1 lines • 7.35 kB
Source Map (JSON)
{"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 formatDate = (date, dateFormat, locale) => {\n return new Intl.DateTimeFormat(locale, { dateStyle: dateFormat }).format(new Date(date));\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":["_parsedGetDate","value","getWeekDays","startDay","month","selectedDay","startDate","dates","i","next","addDays","isNext","getMonth","isDateEqual","date","dateToCompare","isEqual","getCalendarDays","year","weeks","firstDate","lastDate","weekStartsOn","WEEK_START","firstDateInCalendar","startOfWeek","addDaysToWeek","days","day","nextDate","getWeekDayNames","locale","weekStart","beforeWeekStart","afterWeekStart","formatMonth","monthFormat","formatDate","dateFormat","calculateNextFocusDate","currentDate","currentWeekday","getDay","nextMonthDate","addMonths","nextMonthStart","startOfMonth","nextMonthStartWeekday","dayDifference","focusDate","getDate","calculatePrevFocusDate","endOfMonth","subMonths"],"mappings":"oJAOMA,EAAkBC,GAAWA,EAAQ,IAAI,KAAKA,CAAK,EAAI,IAAI,KAM3DC,EAAc,CAACC,EAAUC,EAAOC,IAAgB,CACpD,MAAMC,EAAYN,EAAe,KAAK,MAAM,KAAK,UAAUG,CAAQ,CAAC,CAAC,EAC/DI,EAAQ,CAAA,EACd,QAASC,EAAI,EAAGA,EAAI,EAAGA,IAAK,CAC1B,MAAMC,EAAOC,EAAAA,QAAQJ,EAAWE,CAAC,EAC3BG,EAASC,EAAAA,SAASH,CAAI,IAAML,EAClCG,EAAM,KAAK,CACT,KAAME,EAAK,QAAO,EAClB,MAAOA,EACP,aAAc,CAACE,EACf,kBAAmBF,EAAK,YAAc,GAAK,CAACE,EAE5C,SAAUN,EAAeI,EAAK,QAAO,IAAOJ,GAAe,CAACM,EAAU,EAC5E,CAAK,CACH,CACA,OAAOJ,CACT,EAEMM,EAAc,CAACC,EAAMC,IACrB,CAACD,GAAQ,CAACC,EACL,GAEFC,EAAAA,QAAQF,EAAMC,CAAa,EAMvBE,EAAkB,CAACb,EAAOc,EAAMb,IAAgB,CAC3D,MAAMc,EAAQ,CAAA,EACRC,EAAYpB,EAAe,IAAI,KAAKkB,EAAMd,CAAK,CAAC,EAChDiB,EAAWrB,EAAe,IAAI,KAAKkB,EAAMd,EAAQ,EAAG,CAAC,CAAC,EAEtDkB,EAAeC,EAAAA,WAEfC,EAAsBC,EAAAA,YAAYL,EAAW,CAAE,aAAAE,CAAY,CAAE,EAE7DI,EAAiBZ,GAAS,CAC9B,MAAMa,EAAOzB,EAAYY,EAAMV,EAAOC,CAAW,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