Skip to content

Commit 6b13079

Browse files
authored
feat: add static methods for min and max supported dates (#102)
1 parent b19d135 commit 6b13079

File tree

3 files changed

+120
-15
lines changed

3 files changed

+120
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ console.log(date2.toString()) // 2080-03-23 10:15:00
218218
#### Others
219219

220220
- `NepaliDate.getDaysOfMonth(year, month)`: Returns the number of days in a specific month of a given year.
221+
- `NepaliDate.minSupportedDate()`: Returns the minimum supported JS Date object.
222+
- `NepaliDate.maxSupportedDate()`: Returns the maximum supported JS Date object.
223+
- `NepaliDate.minSupportedNepaliDate()`: Returns the minimum supported Nepali object.
224+
- `NepaliDate.maxSupportedNepaliDate()`: Returns the maximum supported Nepali object.
221225

222226
### dateConverter
223227

src/NepaliDate.ts

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class NepaliDate {
2626
private hour: number
2727
private minute: number
2828
private weekDay: number
29-
static minimum: () => Date
30-
static maximum: () => Date
3129

3230
/**
3331
* Creates a NepaliDate instance for the current date and time.
@@ -663,11 +661,65 @@ class NepaliDate {
663661

664662
return NP_MONTHS_DATA[year - dateConverter.npMinYear()][0][month]
665663
}
666-
}
667664

668-
NepaliDate.minimum = () =>
669-
NepaliDate.fromEnglishDate(dateConverter.enMinYear(), 0, 1).getDateObject()
670-
NepaliDate.maximum = () =>
671-
NepaliDate.fromEnglishDate(dateConverter.enMaxYear(), 11, 31).getDateObject()
665+
/**
666+
* Returns the minimum supported JS Date object.
667+
*
668+
* @returns {Date} The minimum supported JS Date object.
669+
*/
670+
static minSupportedDate(): Date {
671+
return NepaliDate.fromEnglishDate(
672+
dateConverter.enMinYear(),
673+
0,
674+
1
675+
).getDateObject()
676+
}
677+
678+
/**
679+
* Returns the maximum supported JS Date object.
680+
*
681+
* @returns {Date} The maximum supported JS Date object.
682+
*/
683+
static maxSupportedDate(): Date {
684+
return NepaliDate.fromEnglishDate(
685+
dateConverter.enMaxYear(),
686+
11,
687+
31
688+
).getDateObject()
689+
}
690+
691+
/**
692+
* Returns the minimum supported NepaliDate object.
693+
*
694+
* @returns {Date} The minimum supported NepaliDate object.
695+
*/
696+
static minSupportedNepaliDate(): NepaliDate {
697+
return new NepaliDate(dateConverter.npMinYear(), 0, 1)
698+
}
699+
700+
/**
701+
* Returns the maximum supported NepaliDate object.
702+
*
703+
* @returns {Date} The maximum supported NepaliDate object.
704+
*/
705+
static maxSupportedNepaliDate(): NepaliDate {
706+
const npMaxYear = dateConverter.npMaxYear()
707+
return new NepaliDate(npMaxYear, 11, this.getDaysOfMonth(npMaxYear, 11))
708+
}
709+
710+
static minimum(): Date {
711+
console.warn(
712+
'`NepaliDate.minimum()` is deprecated and will be removed in a future version. Please use `NepaliDate.minSupportedDate()` instead.'
713+
)
714+
return this.minSupportedDate()
715+
}
716+
717+
static maximum(): Date {
718+
console.warn(
719+
'`NepaliDate.maximum()` is deprecated and will be removed in a future version. Please use `NepaliDate.maxSupportedDate()` instead.'
720+
)
721+
return this.maxSupportedDate()
722+
}
723+
}
672724

673725
export default NepaliDate

tests/NepaliDate.test.ts

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ describe('NepaliDate', () => {
244244
expect(n.getEnglishDate()).toBe(11)
245245
expect(n.getDay()).toBe(6)
246246
})
247-
248-
it("should return minimum date 1944 of Nepal's time", () => {
249-
expect(NepaliDate.minimum().toISOString()).toEqual('1943-12-31T18:30:00.000Z')
250-
})
251-
252-
it("should return maximum date 2042 Dec last of Nepal's time", () => {
253-
expect(NepaliDate.maximum().toISOString()).toEqual('2042-12-30T18:15:00.000Z')
254-
})
255247
})
256248

257249
describe('NepaliDate parsing on initialization', () => {
@@ -787,3 +779,60 @@ describe('NepaliDate.getDaysOfMonth', () => {
787779
)
788780
})
789781
})
782+
783+
describe('NepaliDate min max supported dates', () => {
784+
it('minSupportedDate should return the minimum English Date converted to Nepali Date', () => {
785+
const result = NepaliDate.minSupportedDate()
786+
787+
expect(result).toBeInstanceOf(Date)
788+
expect(result.toISOString()).toEqual('1943-12-31T18:30:00.000Z') // comparing UTC time
789+
})
790+
791+
it('maxSupportedDate should return the maximum English Date converted to Nepali Date', () => {
792+
const result = NepaliDate.maxSupportedDate()
793+
794+
expect(result).toBeInstanceOf(Date)
795+
expect(result.toISOString()).toEqual('2042-12-30T18:15:00.000Z') // comparing UTC time
796+
})
797+
798+
it('minSupportedNepaliDate should return the minimum NepaliDate object', () => {
799+
const expectedYear = dateConverter.npMinYear()
800+
const result = NepaliDate.minSupportedNepaliDate()
801+
802+
expect(result).toBeInstanceOf(NepaliDate)
803+
expect(result.getYear()).toBe(expectedYear)
804+
expect(result.getMonth()).toBe(0)
805+
expect(result.getDate()).toBe(1)
806+
})
807+
808+
it('maxSupportedNepaliDate should return the maximum NepaliDate object', () => {
809+
const expectedYear = dateConverter.npMaxYear()
810+
const expectedMonth = 11
811+
const expectedDays = NepaliDate.getDaysOfMonth(expectedYear, expectedMonth)
812+
813+
const result = NepaliDate.maxSupportedNepaliDate()
814+
815+
expect(result).toBeInstanceOf(NepaliDate)
816+
expect(result.getYear()).toBe(expectedYear)
817+
expect(result.getMonth()).toBe(expectedMonth)
818+
expect(result.getDate()).toBe(expectedDays)
819+
})
820+
821+
it("should return minimum date 1944 of Nepal's time", () => {
822+
const originalWarn = console.warn
823+
console.warn = () => {} // hide deprecation warning
824+
expect(NepaliDate.minimum().toISOString()).toEqual(
825+
NepaliDate.minSupportedDate().toISOString()
826+
)
827+
console.warn = originalWarn
828+
})
829+
830+
it("should return maximum date 2042 Dec last of Nepal's time", () => {
831+
const originalWarn = console.warn
832+
console.warn = () => {} // hide deprecation warning
833+
expect(NepaliDate.maximum().toISOString()).toEqual(
834+
NepaliDate.maxSupportedDate().toISOString()
835+
)
836+
console.warn = originalWarn
837+
})
838+
})

0 commit comments

Comments
 (0)