Skip to content

Commit

Permalink
feat: add static methods for min and max supported dates (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
aj3sh authored Dec 25, 2024
1 parent b19d135 commit 6b13079
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ 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.
- `NepaliDate.minSupportedDate()`: Returns the minimum supported JS Date object.
- `NepaliDate.maxSupportedDate()`: Returns the maximum supported JS Date object.
- `NepaliDate.minSupportedNepaliDate()`: Returns the minimum supported Nepali object.
- `NepaliDate.maxSupportedNepaliDate()`: Returns the maximum supported Nepali object.

### dateConverter

Expand Down
66 changes: 59 additions & 7 deletions src/NepaliDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class NepaliDate {
private hour: number
private minute: number
private weekDay: number
static minimum: () => Date
static maximum: () => Date

/**
* Creates a NepaliDate instance for the current date and time.
Expand Down Expand Up @@ -663,11 +661,65 @@ class NepaliDate {

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

NepaliDate.minimum = () =>
NepaliDate.fromEnglishDate(dateConverter.enMinYear(), 0, 1).getDateObject()
NepaliDate.maximum = () =>
NepaliDate.fromEnglishDate(dateConverter.enMaxYear(), 11, 31).getDateObject()
/**
* Returns the minimum supported JS Date object.
*
* @returns {Date} The minimum supported JS Date object.
*/
static minSupportedDate(): Date {
return NepaliDate.fromEnglishDate(
dateConverter.enMinYear(),
0,
1
).getDateObject()
}

/**
* Returns the maximum supported JS Date object.
*
* @returns {Date} The maximum supported JS Date object.
*/
static maxSupportedDate(): Date {
return NepaliDate.fromEnglishDate(
dateConverter.enMaxYear(),
11,
31
).getDateObject()
}

/**
* Returns the minimum supported NepaliDate object.
*
* @returns {Date} The minimum supported NepaliDate object.
*/
static minSupportedNepaliDate(): NepaliDate {
return new NepaliDate(dateConverter.npMinYear(), 0, 1)
}

/**
* Returns the maximum supported NepaliDate object.
*
* @returns {Date} The maximum supported NepaliDate object.
*/
static maxSupportedNepaliDate(): NepaliDate {
const npMaxYear = dateConverter.npMaxYear()
return new NepaliDate(npMaxYear, 11, this.getDaysOfMonth(npMaxYear, 11))
}

static minimum(): Date {
console.warn(
'`NepaliDate.minimum()` is deprecated and will be removed in a future version. Please use `NepaliDate.minSupportedDate()` instead.'
)
return this.minSupportedDate()
}

static maximum(): Date {
console.warn(
'`NepaliDate.maximum()` is deprecated and will be removed in a future version. Please use `NepaliDate.maxSupportedDate()` instead.'
)
return this.maxSupportedDate()
}
}

export default NepaliDate
65 changes: 57 additions & 8 deletions tests/NepaliDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ describe('NepaliDate', () => {
expect(n.getEnglishDate()).toBe(11)
expect(n.getDay()).toBe(6)
})

it("should return minimum date 1944 of Nepal's time", () => {
expect(NepaliDate.minimum().toISOString()).toEqual('1943-12-31T18:30:00.000Z')
})

it("should return maximum date 2042 Dec last of Nepal's time", () => {
expect(NepaliDate.maximum().toISOString()).toEqual('2042-12-30T18:15:00.000Z')
})
})

describe('NepaliDate parsing on initialization', () => {
Expand Down Expand Up @@ -787,3 +779,60 @@ describe('NepaliDate.getDaysOfMonth', () => {
)
})
})

describe('NepaliDate min max supported dates', () => {
it('minSupportedDate should return the minimum English Date converted to Nepali Date', () => {
const result = NepaliDate.minSupportedDate()

expect(result).toBeInstanceOf(Date)
expect(result.toISOString()).toEqual('1943-12-31T18:30:00.000Z') // comparing UTC time
})

it('maxSupportedDate should return the maximum English Date converted to Nepali Date', () => {
const result = NepaliDate.maxSupportedDate()

expect(result).toBeInstanceOf(Date)
expect(result.toISOString()).toEqual('2042-12-30T18:15:00.000Z') // comparing UTC time
})

it('minSupportedNepaliDate should return the minimum NepaliDate object', () => {
const expectedYear = dateConverter.npMinYear()
const result = NepaliDate.minSupportedNepaliDate()

expect(result).toBeInstanceOf(NepaliDate)
expect(result.getYear()).toBe(expectedYear)
expect(result.getMonth()).toBe(0)
expect(result.getDate()).toBe(1)
})

it('maxSupportedNepaliDate should return the maximum NepaliDate object', () => {
const expectedYear = dateConverter.npMaxYear()
const expectedMonth = 11
const expectedDays = NepaliDate.getDaysOfMonth(expectedYear, expectedMonth)

const result = NepaliDate.maxSupportedNepaliDate()

expect(result).toBeInstanceOf(NepaliDate)
expect(result.getYear()).toBe(expectedYear)
expect(result.getMonth()).toBe(expectedMonth)
expect(result.getDate()).toBe(expectedDays)
})

it("should return minimum date 1944 of Nepal's time", () => {
const originalWarn = console.warn
console.warn = () => {} // hide deprecation warning
expect(NepaliDate.minimum().toISOString()).toEqual(
NepaliDate.minSupportedDate().toISOString()
)
console.warn = originalWarn
})

it("should return maximum date 2042 Dec last of Nepal's time", () => {
const originalWarn = console.warn
console.warn = () => {} // hide deprecation warning
expect(NepaliDate.maximum().toISOString()).toEqual(
NepaliDate.maxSupportedDate().toISOString()
)
console.warn = originalWarn
})
})

0 comments on commit 6b13079

Please # to comment.