Skip to content

Commit

Permalink
feat: add static method getDaysOfMonth on NepaliDate (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
aj3sh authored Dec 18, 2024
1 parent 577df7f commit c85edfa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ const date2 = NepaliDate.fromEnglishDate(2023, 6, 8, 10, 15)
console.log(date2.toString()) // 2080-03-23 10:15:00
```

#### Others

- `NepaliDate.getDaysOfMonth(year, month)`: Returns the number of days in a specific month of a given year.

### dateConverter

The `dateConverter` module provides core functions for converting dates between the Nepali and English calendars.
Expand Down
21 changes: 21 additions & 0 deletions src/NepaliDate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dateConverter from './dateConverter'
import { NP_MONTHS_DATA } from './dateConverter/constants'
import {
format,
formatEnglishDate,
Expand Down Expand Up @@ -642,6 +643,26 @@ class NepaliDate {
)
return NepaliDate.fromEnglishDate(year, month0, day, hour, minute, second, ms)
}

/**
* Returns the number of days in a specific month of a given year.
*
* @param year - The year to fetch the month from.
* @param month - The month to get the number of days for.
* @returns The number of days in the specified month.
* @throws {Error} If the year or month is out of range.
*/
static getDaysOfMonth(year: number, month: number): number {
if (year < dateConverter.npMinYear() || year > dateConverter.npMaxYear()) {
throw new Error('Year out of range')
}

if (month < 0 || month > 11) {
throw new Error('Month out of range')
}

return NP_MONTHS_DATA[year - dateConverter.npMinYear()][0][month]
}
}

NepaliDate.minimum = () =>
Expand Down
46 changes: 46 additions & 0 deletions tests/NepaliDate.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dateConverter from '../src/dateConverter'
import NepaliDate from '../src/NepaliDate'
import { ValidationError } from '../src/validators'

Expand Down Expand Up @@ -741,3 +742,48 @@ describe('NepaliDate with Time feature: set methods', () => {
})
})
})

describe('NepaliDate.getDaysOfMonth', () => {
it('should return the correct number of days for a valid year and month', () => {
const days = NepaliDate.getDaysOfMonth(2081, 8)
expect(days).toBe(29)
})

it('should return the correct number of days for a year 2000 and month 0', () => {
const days = NepaliDate.getDaysOfMonth(2000, 0)
expect(days).toBe(30)
})

it('should return the correct number of days for a year 2099 and month 11', () => {
const days = NepaliDate.getDaysOfMonth(2099, 11)
expect(days).toBe(30)
})

it('should throw an error if the year is below the valid range', () => {
const invalidYear = dateConverter.npMinYear() - 1
expect(() => NepaliDate.getDaysOfMonth(invalidYear, 0)).toThrow(
'Year out of range'
)
})

it('should throw an error if the year is above the valid range', () => {
const invalidYear = dateConverter.npMaxYear() + 1
expect(() => NepaliDate.getDaysOfMonth(invalidYear, 0)).toThrow(
'Year out of range'
)
})

it('should throw an error if the month is below 0', () => {
const validYear = dateConverter.npMinYear()
expect(() => NepaliDate.getDaysOfMonth(validYear, -1)).toThrow(
'Month out of range'
)
})

it('should throw an error if the month is greater than 11', () => {
const validYear = dateConverter.npMinYear()
expect(() => NepaliDate.getDaysOfMonth(validYear, 12)).toThrow(
'Month out of range'
)
})
})

0 comments on commit c85edfa

Please sign in to comment.