Skip to content

Commit

Permalink
fix: fix range parsing when upper limit = 0 (kelektiv#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Aug 26, 2023
1 parent 7b4a7fc commit b016dde
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,22 +774,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 @@ -799,7 +799,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 @@ -207,6 +213,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

0 comments on commit b016dde

Please sign in to comment.