Skip to content

Commit

Permalink
refactor: update calendar dates and minor reafactor (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
aj3sh authored Dec 25, 2024
1 parent 982d80d commit 6689d01
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 196 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default tseslint.config({
eslintPluginPrettierRecommended,
],
files: ['**/*.{ts,tsx}'],
ignores: ['dist'],
ignores: ['dist/**'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
Expand Down
12 changes: 6 additions & 6 deletions src/components/NepaliCalendar/NepaliCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ const NepaliCalendar: React.FC<INepaliCalendarProps> = ({

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) => {
Expand All @@ -64,21 +64,21 @@ const NepaliCalendar: React.FC<INepaliCalendarProps> = ({
return
}

const [prevMonthYear, prevMonth0] =
const [prevMonthYear, prevMonth] =
selectedMonth === 0 ? [selectedYear - 1, 11] : [selectedYear, selectedMonth - 1]
setSelectedYear(prevMonthYear)
setSelectedMonth(prevMonth0)
setSelectedMonth(prevMonth)
}

const setNextMonth = () => {
if (selectedYear === MAX_NEPALI_YEAR && selectedMonth === 11) {
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)
Expand Down
4 changes: 2 additions & 2 deletions src/components/core/MonthSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const MonthSelector: React.FC<IMonthSelectionProps> = ({
value={selectedMonth}
onChange={e => onMonthChange && onMonthChange(Number(e.target.value))}
>
{getNepaliMonths(locale).map((month, month0) => (
<option key={month} value={month0}>
{getNepaliMonths(locale).map((month, monthIndex) => (
<option key={month} value={monthIndex}>
{month}
</option>
))}
Expand Down
111 changes: 3 additions & 108 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,13 @@
import dateConverter from 'nepali-datetime/dateConverter'
import NepaliDate from 'nepali-datetime'

export const NEPALI_NUMBERS = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']

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',
Expand Down
48 changes: 0 additions & 48 deletions src/utils/calendar.js

This file was deleted.

57 changes: 30 additions & 27 deletions src/utils/calendar.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/utils/classNames.js

This file was deleted.

0 comments on commit 6689d01

Please sign in to comment.