-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function getNextOccurrence, Bug fix isValidDate
- Loading branch information
Showing
8 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## `getNextOccurrence` | ||
|
||
Find the next occurrence of the given day. | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { getNextOccurrence } from '@sgx4u/date-time-utils'; | ||
|
||
const Demo = () => { | ||
const nextSunday = getNextOccurrence('Sunday'); | ||
return <p>Next Sunday: {nextSunday}</p>; | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
getNextOccurrence(day, startDate): string; | ||
``` | ||
|
||
### Props | ||
|
||
- `day` The day to find the next occurrence of, `type`: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | ||
- `startDate` The date to start checking from, `type`: Date | string | number, `optional` | ||
|
||
### Example | ||
|
||
- `getNextOccurrence('Sunday')` - With no start date mentioned it will default to the current date, so lets say current date is 10th Jan 2025. Result - 'Sun, 12 Jan 2025 00:00:00 GMT' | ||
- `getNextOccurrence('Sunday', '2025-01-20T00:00:00.000Z')` - 'Sun, 26 Jan 2025 00:00:00 GMT' | ||
- `getNextOccurrence('Sunday', 'ABC')` - 'Invalid date' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
|
||
import { getNextOccurrence } from './get-next-occurrence'; | ||
|
||
describe('getNextOccurrence utility function', () => { | ||
// Test 1: Test for next occurrence of a weekday | ||
test('Should return the correct next Monday from today', () => { | ||
const today = new Date(); | ||
const result = getNextOccurrence('Monday'); | ||
expect(new Date(result).getTime()).toBeGreaterThan(today.getTime()); // Ensure it's a future date | ||
}); | ||
|
||
// Test 2: Test with a specific start date | ||
test('Should return the next occurrence of Sunday from a given date', () => { | ||
const result = getNextOccurrence('Sunday', new Date('2025-02-10')); // Monday | ||
expect(result).toBe('Sun, 16 Feb 2025 00:00:00 GMT'); | ||
}); | ||
|
||
// Test 3: Test if the function correctly rolls over to the next week | ||
test('Should return the correct next Friday when today is Saturday', () => { | ||
const result = getNextOccurrence('Monday', new Date('2025-02-28')); // Friday | ||
expect(result).toBe('Mon, 03 Mar 2025 00:00:00 GMT'); | ||
}); | ||
|
||
// Test 4: Test for invalid date | ||
test('Should return "Invalid date" for an invalid date', () => { | ||
const result = getNextOccurrence('Monday', 'ABC'); | ||
expect(result).toBe('Invalid date'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { daysFull } from '../data/date.data'; | ||
|
||
type DayType = 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'; | ||
|
||
/** | ||
* @function | ||
* @description - Find the next occurrence of the given day. | ||
* @param day - The day to find the next occurrence of; type: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | ||
* @param startDate - The date to start checking from; type: Date | string | number; default: current date; optional | ||
* @returns { string } Returns UTC value of the result date; type: string | ||
*/ | ||
export const getNextOccurrence = (day: DayType, startDate: Date | string | number = new Date()): string => { | ||
const date = new Date(startDate); | ||
if (!(date instanceof Date) || isNaN(date.getTime())) return 'Invalid date'; | ||
|
||
const requiredDayIndex = daysFull.indexOf(day); | ||
const currentDayIndex = date.getUTCDay(); | ||
|
||
const daysToAdd = requiredDayIndex - currentDayIndex + (requiredDayIndex < currentDayIndex ? 7 : 0); | ||
date.setUTCDate(date.getUTCDate() + daysToAdd); | ||
return date.toUTCString(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters