Skip to content

Commit

Permalink
Fix bugs with mot reseting properly on some date/day ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
rautio committed Oct 8, 2023
1 parent 7c32a3d commit 42e3f0c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crontext",
"version": "0.1.2",
"version": "0.1.3",
"description": "Simple utility for parsing human text into a cron schedule.",
"files": [
"lib"
Expand Down
20 changes: 20 additions & 0 deletions src/next/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,32 @@ describe('nextDate() should', () => {
nextDate('25 8 31 9 *', new Date('March 28 11:00:00 2023')).toString(),
).toEqual(new Date('October 31 8:25:00 2023').toString());
});
test('return complex datetime', () => {
expect(
nextDate(
'*/27 */2 * 6-8 1-5',
new Date('March 28 11:00:00 2023'),
).toString(),
).toEqual(new Date('July 3 0:00:00 2023').toString());
});
test('return same date on full range as *', () => {
const start = new Date('March 28 11:00:00 2023');
expect(nextDate('27 2 * 0-11 0-6', start).toString()).toEqual(
nextDate('27 2 0-31 * *', start).toString(),
);
});
});

describe('getNums() should', () => {
test('Return empty array on *', () => {
expect(getNums('*', 60)).toEqual([]);
});
test('Return empty array on full range', () => {
expect(getNums('0-6', 7)).toEqual([]);
expect(getNums('0-59', 60)).toEqual([]);
expect(getNums('0-23', 24)).toEqual([]);
expect(getNums('0-11', 12)).toEqual([]);
});
test('Return a single number', () => {
expect(getNums('2', 24)).toEqual([2]);
expect(getNums('0', 24)).toEqual([0]);
Expand Down
11 changes: 7 additions & 4 deletions src/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const getNums = (str: string, units: number): Array<number> => {
}
}
}
// If the range is the full range its the same as '*' and should return empty array
if (res.length === units) return [];
// '*' returns empty array.
return res.sort((a, b) => a - b);
};
Expand Down Expand Up @@ -183,7 +185,7 @@ export const iterDate = (date: Date, cron: string): Date => {
const splits = cron.split(' ');
const [minute, hour, dayMonth, month, dayWeek] = splits;
// Validate largest items first (smaller ones will spill over and re-run checks)
const monthSchedule = getNums(month, 11);
const monthSchedule = getNums(month, 12);
const dayWeekSchedule = getNums(dayWeek, 7);
const dayMonthSchedule = getNums(dayMonth, 31); // How do we know? Depends on the month.
const hourSchedule = getNums(hour, 24);
Expand All @@ -208,7 +210,8 @@ export const iterDate = (date: Date, cron: string): Date => {
const curDate = cur.getDate();
cur = iterDayOfMonth(cur, dayMonthSchedule);
// Validate day of week
cur = iterDayOfWeek(cur, getNums(dayWeek, 7));
cur = iterDayOfWeek(cur, dayWeekSchedule);
// Reset hour and minute if needed
if (cur.getDate() !== curDate && shouldReset(hour)) {
cur.setHours(0);
if (shouldReset(minute)) {
Expand All @@ -217,9 +220,9 @@ export const iterDate = (date: Date, cron: string): Date => {
}
// Validate month
const curMonth = cur.getMonth();
cur = iterMonth(cur, getNums(month, 12));
cur = iterMonth(cur, monthSchedule);
if (cur.getMonth() !== curMonth) {
if (shouldReset(dayMonth) && shouldReset(dayWeek)) {
if (shouldReset(dayMonth) || shouldReset(dayWeek)) {
cur.setDate(1);
if (shouldReset(hour)) {
cur.setHours(0);
Expand Down

0 comments on commit 42e3f0c

Please sign in to comment.