From d6d534077390ab4dab8125fc4fdcabf317bc12bc Mon Sep 17 00:00:00 2001 From: Yoav Melamed Date: Mon, 13 Nov 2023 17:29:16 +0200 Subject: [PATCH] order days on the parse level (#305) add test --- src/cronParser.ts | 21 ++++++++++++++++++++- test/cronstrue.ts | 48 +++++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/cronParser.ts b/src/cronParser.ts index 1f00135..0dbe6ea 100644 --- a/src/cronParser.ts +++ b/src/cronParser.ts @@ -40,6 +40,25 @@ export class CronParser { // split on one or more spaces let parsed: string[] = expression.trim().split(/[ ]+/); + // sort elements with array of numbers + for (let i = 0; i < parsed.length; i++) { + if (parsed[i].includes(",")) { + const arrayElement = parsed[i] + .split(",") + .map((item) => item.trim()) + .filter((item) => item !== "") + .map((item) => (!isNaN(Number(item)) ? Number(item) : item)) + .filter((item) => item !== null && item !== ""); + + if (arrayElement.length === 0) { + arrayElement.push("*"); + } + + arrayElement.sort((a, b) => (a !== null && b !== null ? (a as number) - (b as number) : 0)); + parsed[i] = arrayElement.map((item) => (item !== null ? item.toString() : "")).join(","); + } + } + if (parsed.length < 5) { throw new Error( `Expression has only ${parsed.length} part${parsed.length == 1 ? "" : "s"}. At least 5 parts are required.` @@ -242,7 +261,7 @@ export class CronParser { */ if (expressionParts[i].indexOf("/") > -1 && !/^\*|\-|\,/.test(expressionParts[i])) { - let stepRangeThrough: string|null = null; + let stepRangeThrough: string | null = null; switch (i) { case 4: stepRangeThrough = "12"; diff --git a/test/cronstrue.ts b/test/cronstrue.ts index 50ea6ef..ba949bb 100644 --- a/test/cronstrue.ts +++ b/test/cronstrue.ts @@ -154,84 +154,84 @@ describe("Cronstrue", function () { describe("at", function () { it("30 11 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ - tzOffset:3 + assert.equal(cronstrue.toString(this.test?.title as string, { + tzOffset: 3 }), "At 02:30 PM"); }); it("31 10 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: 5.5 }), "At 04:01 PM"); }); it("29 10 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: 5.5 }), "At 03:59 PM"); }); it("30 10 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: 5.5 }), "At 04:00 PM"); }); it("31 10 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: -1.5 }), "At 09:01 AM"); }); it("29 10 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: -1.5 }), "At 08:59 AM"); }); it("30 10 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: -1.5 }), "At 09:00 AM"); }); it("30 11 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ - tzOffset:3, - use24HourTimeFormat:true + assert.equal(cronstrue.toString(this.test?.title as string, { + tzOffset: 3, + use24HourTimeFormat: true }), "At 14:30"); }); it("30 3 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ - tzOffset:-5, - use24HourTimeFormat:true + assert.equal(cronstrue.toString(this.test?.title as string, { + tzOffset: -5, + use24HourTimeFormat: true }), "At 22:30"); }); it("10 11 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ + assert.equal(cronstrue.toString(this.test?.title as string, { tzOffset: -4, verbose: true }), "At 07:10 AM, every day"); }); it("30 22 * * *", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ - tzOffset:5, - use24HourTimeFormat:true + assert.equal(cronstrue.toString(this.test?.title as string, { + tzOffset: 5, + use24HourTimeFormat: true }), "At 03:30"); }); it("5 1 * * 1", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ - tzOffset:-2, + assert.equal(cronstrue.toString(this.test?.title as string, { + tzOffset: -2, }), "At 11:05 PM, only on Sunday"); }); it("5 23 * * 1", function () { - assert.equal(cronstrue.toString(this.test?.title as string,{ - tzOffset:2, + assert.equal(cronstrue.toString(this.test?.title as string, { + tzOffset: 2, }), "At 01:05 AM, only on Tuesday"); }); @@ -290,6 +290,10 @@ describe("Cronstrue", function () { it("0 * 31 * 1", function () { assert.equal(cronstrue.toString(this.test?.title as string), "Every hour, on day 31 of the month, and on Monday"); }); + + it("0 45 12 22,17,6,30,26 * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string), "At 12:45 PM, on day 6, 17, 22, 26, and 30 of the month"); + }); }); describe("weekday", function () {