You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use cron-parser in a UI environment where the user can toggle days of the week on and off. The result of the UI is a cron string that can then be sent and stored in the API.
I am parsing a cron string (parseExpression), breaking it into its fields then adjusting fields and recreating the cron string (fieldsToExpression).
There seems to be an issue when doing this with dayOfWeek (maybe dayOfMonth as well, but I'm not working with that yet). See my code for reproduction and the corresponding output below:
Code
constexpression1=parseExpression("* * * * *");console.log("expression1.stringify(): ",expression1.stringify());console.log("expression1.fields.dayOfWeek: ",expression1.fields.dayOfWeek);constdayOfWeek1=[...expression1.fields.dayOfWeek];dayOfWeek1.splice(dayOfWeek1.indexOf(5),1);// remove 5 from the arrayconstfields1={
...expression1.fields,dayOfWeek: dayOfWeek1,};constexpression2=fieldsToExpression(fields1);console.log("expression2.stringify(): ",expression2.stringify());constexpression3=parseExpression(expression2.stringify());console.log("expression3.fields.dayOfWeek: ",expression3.fields.dayOfWeek);console.log("expression3.stringify(): ",expression3.stringify());constdayOfWeek2=[...expression3.fields.dayOfWeek,5];// add 5 back into the arrayconstfields2={
...expression1.fields,dayOfWeek: dayOfWeek2,};console.log("fields2: ",fields2);constexpression4=fieldsToExpression(fields2);// fails here!console.log("expression4.fields.dayOfWeek: ",expression4.fields.dayOfWeek);console.log("expression4.stringify(): ",expression4.stringify());
Error: Validation error, Field dayOfWeek contains duplicate values
at Function.fieldsToExpression (/Users/me/workspace/my-project/app/node_modules/cron-parser/lib/expression.js:975:13)
at fieldsToExpression (/Users/me/workspace/my-project/app/node_modules/cron-parser/lib/parser.js:53:25)
Analysis
From what I can tell, the original expression's fields is where the issue begins. There are only 7 days in the week, yet the fields.dayOfWeek array contains 8 items. This is likely to support cron strings that use 7 for Sunday.
After removing 5 from the dayOfWeek array the stringified expression contains both 0 and 7. This is likely where the fix should occur. I don't know which situation where we would want both 0 and 7 in a stringified cron expression.
Let me know how I can help get this resolved. If you can point me towards the problem code, I'm happy to get a PR out to fix it.
Thanks in advance,
Jack
The text was updated successfully, but these errors were encountered:
Thinking back, it makes my job a bit more difficult receiving both 0 and 7 back from the fields.dayOfWeek array. I have to check if either of these are here to make a decision on whether a job should be run on Sunday. Would be easier if it was always 0-6 in the output of cron-parser APIs but it supported 7 as an input for the scenarios where that's relevant.
Hello,
I am trying to use
cron-parser
in a UI environment where the user can toggle days of the week on and off. The result of the UI is a cron string that can then be sent and stored in the API.I am parsing a cron string (
parseExpression
), breaking it into its fields then adjusting fields and recreating the cron string (fieldsToExpression
).There seems to be an issue when doing this with
dayOfWeek
(maybedayOfMonth
as well, but I'm not working with that yet). See my code for reproduction and the corresponding output below:Code
Output
Error
Analysis
From what I can tell, the original expression's fields is where the issue begins. There are only 7 days in the week, yet the
fields.dayOfWeek
array contains 8 items. This is likely to support cron strings that use7
for Sunday.After removing 5 from the
dayOfWeek
array the stringified expression contains both 0 and 7. This is likely where the fix should occur. I don't know which situation where we would want both 0 and 7 in a stringified cron expression.Let me know how I can help get this resolved. If you can point me towards the problem code, I'm happy to get a PR out to fix it.
Thanks in advance,
Jack
The text was updated successfully, but these errors were encountered: