Skip to content

Commit

Permalink
fix: correctly parse weekday ranges that include sunday (#283)
Browse files Browse the repository at this point in the history
* Allow 7 as weekday range and use modulo to remove it from the result.

* Prettier

* Use 7 as alias for sun to support ranges
  • Loading branch information
sky0hunter authored Mar 30, 2023
1 parent aa3ebd7 commit 2fb78b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/cron-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ const monthConstraint: IConstraint = {

const weekdayConstraint: IConstraint = {
min: 0,
max: 6,
max: 7,
aliases: {
'7': '0',
'sun': '0',
'mon': '1',
'tue': '2',
'wed': '3',
'thu': '4',
'fri': '5',
'sat': '6',
'sun': '7'
},
}

Expand Down Expand Up @@ -194,6 +193,8 @@ export function parseCronExpression(cronExpression: string): Cron {
months: new Set(
Array.from(parseElement(rawMonths, monthConstraint)).map((x) => x - 1)
),
weekdays: parseElement(rawWeekdays, weekdayConstraint),
weekdays: new Set(
Array.from(parseElement(rawWeekdays, weekdayConstraint)).map((x) => x % 7)
),
})
}
26 changes: 23 additions & 3 deletions test/cron-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ describe('parseCronExpression', () => {
})
})

test('Should correctly parse weekday range with 7', () => {
expect(parseCronExpression('1 2 3 4 5 6-7')).toMatchObject({
seconds: [1],
minutes: [2],
hours: [3],
days: [4],
months: [4],
weekdays: [0, 6],
})
})

test('Should correctly parse single numbers', () => {
expect(parseCronExpression('1 2 3 4 5 6')).toMatchObject({
seconds: [1],
Expand Down Expand Up @@ -209,7 +220,16 @@ describe('parseCronExpression', () => {
})
})

test('Should correctly parse weekday names', () => {
test('Should correctly parse weekday names with range', () => {
expect(parseCronExpression('0 0 0 1 1 sat-sun')).toMatchObject({
seconds: [0],
minutes: [0],
hours: [0],
days: [1],
months: [0],
weekdays: [0,6],
})

expect(parseCronExpression('0 0 0 1 1 sun')).toMatchObject({
seconds: [0],
minutes: [0],
Expand Down Expand Up @@ -408,11 +428,11 @@ describe('parseCronExpression', () => {
)
)
expect(() => parseCronExpression('* * * * * 8')).toThrow(
new Error('Failed to parse 8: 8 is outside of constraint range of 0 - 6.')
new Error('Failed to parse 8: 8 is outside of constraint range of 0 - 7.')
)
expect(() => parseCronExpression('* * * * * -1')).toThrow(
new Error(
'Failed to parse -1: -1 is outside of constraint range of 0 - 6.'
'Failed to parse -1: -1 is outside of constraint range of 0 - 7.'
)
)
expect(() => parseCronExpression('** * * * * *')).toThrow(
Expand Down
2 changes: 1 addition & 1 deletion test/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe('constructor', () => {
hours: new Set([1]),
days: new Set([1]),
months: new Set([1]),
weekdays: new Set([7]),
weekdays: new Set([8]),
})
).toThrow()
})
Expand Down

0 comments on commit 2fb78b5

Please sign in to comment.