Skip to content

Commit d96746f

Browse files
authored
fix: fix range parsing when upper limit = 0 (#687)
1 parent 97f9241 commit d96746f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/time.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -768,22 +768,22 @@ function CronTime(luxon) {
768768
if (allRanges[i].match(RE_RANGE)) {
769769
allRanges[i].replace(RE_RANGE, ($0, lower, upper, step) => {
770770
lower = parseInt(lower, 10);
771-
upper = parseInt(upper, 10) || undefined;
771+
upper = upper !== undefined ? parseInt(upper, 10) : undefined;
772772

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

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

783783
const outOfRangeError =
784784
lower < low ||
785-
(upper && upper > high) ||
786-
(!upper && lower > high);
785+
(upper !== undefined && upper > high) ||
786+
(upper === undefined && lower > high);
787787

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

795795
// Positive integer lower than constraints[1]
796-
if (upper) {
796+
if (upper !== undefined) {
797797
upper = Math.min(high, ~~Math.abs(upper));
798798
} else {
799799
// If step is provided, the default upper range is the highest value

tests/crontime.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ describe('crontime', () => {
7070
}).not.toThrow();
7171
});
7272

73+
it('should accept all valid ranges (0-59 0-59 1-23 1-31 0-12 0-6)', () => {
74+
expect(() => {
75+
new cron.CronTime('0-59 0-59 1-23 1-31 0-11 0-6');
76+
}).not.toThrow();
77+
});
78+
7379
it('should test comma (0,10 * * * * *)', () => {
7480
expect(() => {
7581
new cron.CronTime('0,10 * * * * *');
@@ -152,6 +158,14 @@ describe('crontime', () => {
152158
expect(() => {
153159
new cron.CronTime('* 2-1 * * *');
154160
}).toThrow();
161+
162+
expect(() => {
163+
new cron.CronTime('* 2-0 * * *');
164+
}).toThrow();
165+
166+
expect(() => {
167+
new cron.CronTime('* 2- * * *');
168+
}).toThrow();
155169
});
156170

157171
it('should test Date', () => {

0 commit comments

Comments
 (0)