Skip to content

Commit

Permalink
order days on the parse level (#305)
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
yoav-melamed authored Nov 13, 2023
1 parent af8b27f commit d6d5340
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
21 changes: 20 additions & 1 deletion src/cronParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
Expand Down Expand Up @@ -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";
Expand Down
48 changes: 26 additions & 22 deletions test/cronstrue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit d6d5340

Please sign in to comment.