From 6689d014fc6f19e5f0fc7de1649f12bd28c5f8c9 Mon Sep 17 00:00:00 2001 From: Ajesh Sen Thapa <35629644+aj3sh@users.noreply.github.com> Date: Wed, 25 Dec 2024 18:59:14 +0545 Subject: [PATCH] refactor: update calendar dates and minor reafactor (#13) --- eslint.config.js | 2 +- .../NepaliCalendar/NepaliCalendar.tsx | 12 +- src/components/core/MonthSelector.tsx | 4 +- src/constants.ts | 111 +----------------- src/utils/calendar.js | 48 -------- src/utils/calendar.ts | 57 ++++----- src/utils/classNames.js | 4 - 7 files changed, 42 insertions(+), 196 deletions(-) delete mode 100644 src/utils/calendar.js delete mode 100644 src/utils/classNames.js diff --git a/eslint.config.js b/eslint.config.js index 603c8df..4fdb327 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -13,7 +13,7 @@ export default tseslint.config({ eslintPluginPrettierRecommended, ], files: ['**/*.{ts,tsx}'], - ignores: ['dist'], + ignores: ['dist/**'], languageOptions: { ecmaVersion: 2020, globals: globals.browser, diff --git a/src/components/NepaliCalendar/NepaliCalendar.tsx b/src/components/NepaliCalendar/NepaliCalendar.tsx index 15f4b75..dc1769a 100644 --- a/src/components/NepaliCalendar/NepaliCalendar.tsx +++ b/src/components/NepaliCalendar/NepaliCalendar.tsx @@ -42,13 +42,13 @@ const NepaliCalendar: React.FC = ({ const isCurrentDate = (calendarDate: ICalendarDate) => calendarDate.year === now.getYear() && - calendarDate.month0 === now.getMonth() && + calendarDate.month === now.getMonth() && calendarDate.date === now.getDate() const isSelectedDate = (calendarDate: ICalendarDate) => selectedNepaliDate && calendarDate.year === selectedNepaliDate.getYear() && - calendarDate.month0 === selectedNepaliDate.getMonth() && + calendarDate.month === selectedNepaliDate.getMonth() && calendarDate.date === selectedNepaliDate.getDate() const handleDateSelect = (calendarDate: ICalendarDate) => { @@ -64,10 +64,10 @@ const NepaliCalendar: React.FC = ({ return } - const [prevMonthYear, prevMonth0] = + const [prevMonthYear, prevMonth] = selectedMonth === 0 ? [selectedYear - 1, 11] : [selectedYear, selectedMonth - 1] setSelectedYear(prevMonthYear) - setSelectedMonth(prevMonth0) + setSelectedMonth(prevMonth) } const setNextMonth = () => { @@ -75,10 +75,10 @@ const NepaliCalendar: React.FC = ({ return } - const [prevMonthYear, prevMonth0] = + const [prevMonthYear, prevMonth] = selectedMonth === 11 ? [selectedYear + 1, 0] : [selectedYear, selectedMonth + 1] setSelectedYear(prevMonthYear) - setSelectedMonth(prevMonth0) + setSelectedMonth(prevMonth) } const calendarArray = getCalendarTableArray(selectedYear, selectedMonth) diff --git a/src/components/core/MonthSelector.tsx b/src/components/core/MonthSelector.tsx index fb94ea1..e5ec98e 100644 --- a/src/components/core/MonthSelector.tsx +++ b/src/components/core/MonthSelector.tsx @@ -24,8 +24,8 @@ const MonthSelector: React.FC = ({ value={selectedMonth} onChange={e => onMonthChange && onMonthChange(Number(e.target.value))} > - {getNepaliMonths(locale).map((month, month0) => ( - ))} diff --git a/src/constants.ts b/src/constants.ts index 237f53b..db48db7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,4 @@ -import dateConverter from 'nepali-datetime/dateConverter' +import NepaliDate from 'nepali-datetime' export const NEPALI_NUMBERS = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'] @@ -6,113 +6,8 @@ export const LOCALE_EN = 'en' export const LOCALE_NE = 'ne' export const DEFAULT_LOCALE = LOCALE_EN -export const MIN_NEPALI_YEAR = dateConverter.npMinYear() -export const MAX_NEPALI_YEAR = dateConverter.npMaxYear() - -// TODO: use export `NP_MONTHS_DATA` from nepali-datetime for single source of truth -// Nepali months data -export const NP_MONTHS_DATA: Array<[number[], number]> = [ - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 365], // 2000 BS - 1943/1944 AD - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], // 2001 BS - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 366], - [[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 366], - [[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 366], - [[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 366], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 366], - [[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 366], - [[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 365], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 365], // 2080 - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 366], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], 366], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], 366], - [[30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], 365], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], 366], - [[30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30], 366], - [[30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 364], - [[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 366], - [[31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31], 365], - [[31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30], 365], // 2099 BS - 2042/2043 AD -] +export const MIN_NEPALI_YEAR = NepaliDate.minSupportedNepaliDate().getYear() +export const MAX_NEPALI_YEAR = NepaliDate.maxSupportedNepaliDate().getYear() export const NEPALI_MONTHS_EN = [ 'Baisakh', diff --git a/src/utils/calendar.js b/src/utils/calendar.js deleted file mode 100644 index 8a62232..0000000 --- a/src/utils/calendar.js +++ /dev/null @@ -1,48 +0,0 @@ -import NepaliDate from 'nepali-datetime'; -import { MAX_NEPALI_YEAR, MIN_NEPALI_YEAR, NP_MONTHS_DATA } from '../constants'; -const WEEK_COUNT = 7; -const getMonthData = (year, month0) => { - if (year < MIN_NEPALI_YEAR || year > MAX_NEPALI_YEAR) { - return null; - } - return NP_MONTHS_DATA[year - MIN_NEPALI_YEAR][0][month0]; -}; -export const getCalendarTableArray = (year, month0) => { - const currentMonthDays = getMonthData(year, month0); - if (!currentMonthDays) { - return []; - } - const startWeekDay = new NepaliDate(year, month0, 1).getDay(); - const [prevMonthYear, prevMonth0] = month0 === 0 ? [year - 1, 11] : [year, month0 - 1]; - const prevMonthDays = getMonthData(prevMonthYear, prevMonth0); - const [nextMonthYear, nextMonth0] = month0 === 11 ? [year + 1, 0] : [year, month0 + 1]; - const totalCalendarDays = Math.ceil((startWeekDay + currentMonthDays) / WEEK_COUNT) * WEEK_COUNT; - const calendar = Array(totalCalendarDays) - .fill(null) - .map((_, index) => { - let dt, mon0, yr, active; - if (index < startWeekDay) { - // Previous month - dt = prevMonthDays ? prevMonthDays - (startWeekDay - index - 1) : null; - mon0 = prevMonth0; - yr = prevMonthYear; - active = false; - } - else if (index < startWeekDay + currentMonthDays) { - // Current month - dt = index - startWeekDay + 1; - mon0 = month0; - yr = year; - active = true; - } - else { - // Next month - dt = index - (startWeekDay + currentMonthDays) + 1; - mon0 = nextMonth0; - yr = nextMonthYear; - active = false; - } - return { index, active, year: yr, month0: mon0, date: dt }; - }); - return calendar; -}; diff --git a/src/utils/calendar.ts b/src/utils/calendar.ts index effdce5..b9873bb 100644 --- a/src/utils/calendar.ts +++ b/src/utils/calendar.ts @@ -1,68 +1,71 @@ import NepaliDate from 'nepali-datetime' -import { MAX_NEPALI_YEAR, MIN_NEPALI_YEAR, NP_MONTHS_DATA } from '../constants' -const WEEK_COUNT = 7 +const DAYS_IN_WEEK = 7 export interface ICalendarDate { index: number active: boolean year: number - month0: number + month: number date: number | null } -const getMonthData = (year: number, month0: number) => { - if (year < MIN_NEPALI_YEAR || year > MAX_NEPALI_YEAR) { +const getMonthData = (year: number, month: number) => { + try { + return NepaliDate.getDaysOfMonth(year, month) + } catch { return null } - return NP_MONTHS_DATA[year - MIN_NEPALI_YEAR][0][month0] } -export const getCalendarTableArray = ( - year: number, - month0: number -): ICalendarDate[] => { - const currentMonthDays = getMonthData(year, month0) +export const getCalendarTableArray = (year: number, month: number): ICalendarDate[] => { + const currentMonthDays = getMonthData(year, month) if (!currentMonthDays) { return [] } - const startWeekDay = new NepaliDate(year, month0, 1).getDay() + const startWeekDay = new NepaliDate(year, month, 1).getDay() - const [prevMonthYear, prevMonth0] = month0 === 0 ? [year - 1, 11] : [year, month0 - 1] - const prevMonthDays = getMonthData(prevMonthYear, prevMonth0) + const [prevMonthYear, prevMonth] = month === 0 ? [year - 1, 11] : [year, month - 1] + const prevMonthDays = getMonthData(prevMonthYear, prevMonth) - const [nextMonthYear, nextMonth0] = month0 === 11 ? [year + 1, 0] : [year, month0 + 1] + const [nextMonthYear, nextMonth] = month === 11 ? [year + 1, 0] : [year, month + 1] const totalCalendarDays = - Math.ceil((startWeekDay + currentMonthDays) / WEEK_COUNT) * WEEK_COUNT + Math.ceil((startWeekDay + currentMonthDays) / DAYS_IN_WEEK) * DAYS_IN_WEEK const calendar: ICalendarDate[] = Array(totalCalendarDays) .fill(null) .map((_, index) => { - let dt, mon0, yr, active + let calendarDate, calendarMonth, calendarYear, active if (index < startWeekDay) { // Previous month - dt = prevMonthDays ? prevMonthDays - (startWeekDay - index - 1) : null - mon0 = prevMonth0 - yr = prevMonthYear + calendarDate = prevMonthDays ? prevMonthDays - (startWeekDay - index - 1) : null + calendarMonth = prevMonth + calendarYear = prevMonthYear active = false } else if (index < startWeekDay + currentMonthDays) { // Current month - dt = index - startWeekDay + 1 - mon0 = month0 - yr = year + calendarDate = index - startWeekDay + 1 + calendarMonth = month + calendarYear = year active = true } else { // Next month - dt = index - (startWeekDay + currentMonthDays) + 1 - mon0 = nextMonth0 - yr = nextMonthYear + calendarDate = index - (startWeekDay + currentMonthDays) + 1 + calendarMonth = nextMonth + calendarYear = nextMonthYear active = false } - return { index, active, year: yr, month0: mon0, date: dt } + return { + index, + active, + year: calendarYear, + month: calendarMonth, + date: calendarDate, + } }) return calendar diff --git a/src/utils/classNames.js b/src/utils/classNames.js deleted file mode 100644 index 54ed031..0000000 --- a/src/utils/classNames.js +++ /dev/null @@ -1,4 +0,0 @@ -const classNames = (...classes) => { - return classes.filter(Boolean).join(' '); -}; -export default classNames;