Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix range parsing when upper limit = 0 #687

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,22 +768,22 @@ function CronTime(luxon) {
if (allRanges[i].match(RE_RANGE)) {
allRanges[i].replace(RE_RANGE, ($0, lower, upper, step) => {
lower = parseInt(lower, 10);
upper = parseInt(upper, 10) || undefined;
upper = upper !== undefined ? parseInt(upper, 10) : undefined;

const wasStepDefined = !isNaN(parseInt(step, 10));
if (step === '0') {
throw new Error(`Field (${type}) has a step of zero`);
}
step = parseInt(step, 10) || 1;

if (upper && lower > upper) {
if (upper !== undefined && lower > upper) {
throw new Error(`Field (${type}) has an invalid range`);
}

const outOfRangeError =
lower < low ||
(upper && upper > high) ||
(!upper && lower > high);
(upper !== undefined && upper > high) ||
(upper === undefined && lower > high);

if (outOfRangeError) {
throw new Error(`Field value (${value}) is out of range`);
Expand All @@ -793,7 +793,7 @@ function CronTime(luxon) {
lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);

// Positive integer lower than constraints[1]
if (upper) {
if (upper !== undefined) {
upper = Math.min(high, ~~Math.abs(upper));
} else {
// If step is provided, the default upper range is the highest value
Expand Down
14 changes: 14 additions & 0 deletions tests/crontime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ describe('crontime', () => {
}).not.toThrow();
});

it('should accept all valid ranges (0-59 0-59 1-23 1-31 0-12 0-6)', () => {
expect(() => {
new cron.CronTime('0-59 0-59 1-23 1-31 0-11 0-6');
}).not.toThrow();
});

it('should test comma (0,10 * * * * *)', () => {
expect(() => {
new cron.CronTime('0,10 * * * * *');
Expand Down Expand Up @@ -152,6 +158,14 @@ describe('crontime', () => {
expect(() => {
new cron.CronTime('* 2-1 * * *');
}).toThrow();

expect(() => {
new cron.CronTime('* 2-0 * * *');
}).toThrow();

expect(() => {
new cron.CronTime('* 2- * * *');
}).toThrow();
});

it('should test Date', () => {
Expand Down