Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering days on parse() #305

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("*");
}
Comment on lines +53 to +55
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support the following:
it("dayOfWeek specified as comma", function () { assert.equal(new CronParser("*/5 * * * * ,").parse()[5], "*"); });

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a valid syntax for a cron?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/5 * * * * , is an invalid syntax, this is why it parsed to */5 * * * * * which is a valid syntax.
anyhow, it was an existing test that I didn't want to break.
My challenge was to add this fix while keeping all the 212 (+1 the one I added) running.


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