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

Support for '@' expressions #318

Merged
merged 6 commits into from
May 6, 2024
Merged
Changes from 2 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
30 changes: 29 additions & 1 deletion src/cronParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,41 @@ export class CronParser {
* @returns {string[]}
*/
parse(): string[] {
let parsed = this.extractParts(this.expression);
let parsed: string[];

if (this.expression.startsWith('@')) {
var special = this.parseSpecial(this.expression);
parsed = this.extractParts(special);

} else {
parsed = this.extractParts(this.expression);
}

this.normalize(parsed);
this.validate(parsed);

return parsed;
}

parseSpecial(expression: string): string {
const specialExpressions: { [key: string]: string } = {
'@yearly': '0 0 1 1 *',
'@annually': '0 0 1 1 *',
'@monthly': '0 0 1 * *',
'@weekly': '0 0 * * 0',
'@daily': '0 0 * * *',
'@midnight': '0 0 * * *',
Copy link
Owner

Choose a reason for hiding this comment

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

@midnight - I'm not aware of a cron implementation that supports this. Is there one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

'@hourly': '0 * * * *'
};

const special = specialExpressions[expression];
if (!special) {
throw new Error('Unknown special expression.');
}

return special;
}

protected extractParts(expression: string) {
if (!this.expression) {
throw new Error("cron expression is empty");
Expand Down