Skip to content

Commit

Permalink
fix: make parser more resilient to extra whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
P4sca1 committed Jul 29, 2024
1 parent d6628e8 commit f879354
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cron-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ export function parseCronExpression(cronExpression: string): Cron {
// biome-ignore lint/style/noParameterAssign: adding another variable with a new name is more confusing
cronExpression = timeNicknames[cronExpression.toLowerCase()] ?? cronExpression

const elements = cronExpression.split(' ')
// Split the cron expression into its elements, removing empty elements (extra whitespaces).
const elements = cronExpression.split(' ').filter((elem) => elem.length > 0)
if (elements.length < 5 || elements.length > 6) {
throw new Error('Invalid cron expression: expected 5 or 6 elements.')
}
Expand Down
13 changes: 13 additions & 0 deletions test/cron-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,17 @@ describe('parseCronExpression', () => {
new Error('Failed to parse step: Expected 0 to be greater than 0.'),
)
})

test('it should be resilient to extra whitespaces', () => {
expect(
parseCronExpression(' * * * * * * '),
).toMatchObject({
seconds: Array.from({ length: 60 }, (_, i) => i),
minutes: Array.from({ length: 60 }, (_, i) => i),
hours: Array.from({ length: 24 }, (_, i) => i),
days: Array.from({ length: 31 }, (_, i) => i + 1),
months: Array.from({ length: 12 }, (_, i) => i),
weekdays: Array.from({ length: 7 }, (_, i) => i),
})
})
})

0 comments on commit f879354

Please sign in to comment.