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

.stringify() and .fields do not support nthDayOfWeek expressions (e.g. 0 9 * * 1#2) #349

Open
Jackman3005 opened this issue Apr 22, 2024 · 1 comment

Comments

@Jackman3005
Copy link

Note: The library does correctly parse the expression and provide correct next() dates for nthDayOfWeek expressions.

Issue

Given the expression 0 9 * * 1#2 which represent "Once a month on the first Monday of the Month at 9am". When the expression's fields are extracted via .fields and then the expression is rebuilt and stringified, it loses the nthDayOfWeek configuration and results in 0 9 * * 1, which is incorrect.

See the following failing test I wrote and relevant console output:

test("parsing `nthDayOfWeek` expressions to fields and rebuilding from fields does not change the expression", () => {
  // Every month on the second Tuesday at 9am.
  const expression = "0 9 * * 2#2";
  const fields = parseExpression(expression).fields;
  console.log("Fields: ", fields);
  // Fields: {
  //   second: [ 0 ],
  //   minute: [ 0 ],
  //   hour: [ 9 ],
  //   dayOfMonth: [
  //      1,  2,  3,  4,  5,  6,  7,  8,  9,
  //     10, 11, 12, 13, 14, 15, 16, 17, 18,
  //     19, 20, 21, 22, 23, 24, 25, 26, 27,
  //     28, 29, 30, 31
  //   ],
  //   month: [
  //      1,  2, 3, 4,  5,
  //      6,  7, 8, 9, 10,
  //     11, 12
  //   ],
  //   dayOfWeek: [ 2 ]
  // }

  const expressionFromFields = fieldsToExpression(fields).stringify(false);

  expect(expressionFromFields).toEqual(expression); // <--- Expectation fails
  // Expected: "0 9 * * 2#2"
  // Received: "0 9 * * 2"
});

Possible solution

The nthDayOfWeek configuration is stored in the private _options field of the expression. I'm not sure of other consequences that might arise, but it seems to me this should be moved from _options to fields so it can be passed through and used in fieldsToExpression to correctly rebuild the expression state.

@Jackman3005
Copy link
Author

Upon further testing I realized that even if you do not convert to fields, the original expression is unable to stringify(..) nthDayOfWeek expressions correctly.

See the following failing test I've written:

test("stringify expressions with `nthDayOfWeek` configuration", async () => {
  // Every month on the second Monday at 9am.
  const expression = "0 9 * * 1#2";

  const parsedExpression = parseExpression(expression);

  expect(parsedExpression.stringify(false)).toEqual(expression);
  // Expected: "0 9 * * 1#2"
  // Received: "0 9 * * 1"
});

@Jackman3005 Jackman3005 changed the title Expressions with nthDayOfWeek (0 9 * * 1#2) are incorrect when rebuilt from fields .stringify() and .fields do not support nthDayOfWeek expressions (0 9 * * 1#2) May 9, 2024
@Jackman3005 Jackman3005 changed the title .stringify() and .fields do not support nthDayOfWeek expressions (0 9 * * 1#2) .stringify() and .fields do not support nthDayOfWeek expressions (e.g. 0 9 * * 1#2) May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant