From d96746f7b8f357e565d1fad48c9f70d3d646da64 Mon Sep 17 00:00:00 2001 From: Pierre Cavin Date: Sat, 26 Aug 2023 18:42:50 +0200 Subject: [PATCH] fix: fix range parsing when upper limit = 0 (#687) --- lib/time.js | 10 +++++----- tests/crontime.test.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/time.js b/lib/time.js index 3b9e54c8..5c4a98a5 100644 --- a/lib/time.js +++ b/lib/time.js @@ -768,7 +768,7 @@ 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') { @@ -776,14 +776,14 @@ function CronTime(luxon) { } 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`); @@ -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 diff --git a/tests/crontime.test.js b/tests/crontime.test.js index 0d962d06..d6721bd2 100644 --- a/tests/crontime.test.js +++ b/tests/crontime.test.js @@ -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 * * * * *'); @@ -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', () => {