From c85edfa4d40f93061674f6143f6343b23a653bc0 Mon Sep 17 00:00:00 2001 From: Ajesh Sen Thapa <35629644+aj3sh@users.noreply.github.com> Date: Wed, 18 Dec 2024 13:13:40 +0545 Subject: [PATCH] feat: add static method `getDaysOfMonth` on NepaliDate (#96) --- README.md | 4 ++++ src/NepaliDate.ts | 21 ++++++++++++++++++ tests/NepaliDate.test.ts | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/README.md b/README.md index 86117b8..b815b5b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/NepaliDate.ts b/src/NepaliDate.ts index 33174ea..2f9d605 100644 --- a/src/NepaliDate.ts +++ b/src/NepaliDate.ts @@ -1,4 +1,5 @@ import dateConverter from './dateConverter' +import { NP_MONTHS_DATA } from './dateConverter/constants' import { format, formatEnglishDate, @@ -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 = () => diff --git a/tests/NepaliDate.test.ts b/tests/NepaliDate.test.ts index ed802c5..f22dd1c 100644 --- a/tests/NepaliDate.test.ts +++ b/tests/NepaliDate.test.ts @@ -1,3 +1,4 @@ +import dateConverter from '../src/dateConverter' import NepaliDate from '../src/NepaliDate' import { ValidationError } from '../src/validators' @@ -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' + ) + }) +})