refactor: perfect cron rule string
This commit is contained in:
@@ -7,7 +7,7 @@ const ONE_TIME_VALUE = "@one-time";
|
||||
export function parseCron(cron: string | null): {
|
||||
type: CronType;
|
||||
value: number;
|
||||
date: Date|null;
|
||||
date: Date | null;
|
||||
} {
|
||||
let value: RegExpMatchArray | null;
|
||||
if (cron === null) {
|
||||
@@ -22,7 +22,7 @@ export function parseCron(cron: string | null): {
|
||||
value: 0,
|
||||
date: null,
|
||||
};
|
||||
} else if (cron.startsWith("RRULE:")) {
|
||||
} else if (cron.startsWith("RRULE:") || cron.startsWith("DTSTART:")) {
|
||||
return parseRrule(cron);
|
||||
} else if ((value = /^0 (\d+) \* \* \*$/i.exec(cron)) !== null) {
|
||||
return {
|
||||
@@ -145,11 +145,23 @@ export function getCron(
|
||||
case CronType.EVERY_YEAR:
|
||||
return `0 0 1 ${value.toFixed(0)} *`;
|
||||
case CronType.EVERY_N_MONTHS:
|
||||
return `RRULE:FREQ=MONTHLY;INTERVAL=${value.toFixed(0)}\nDTSTART:${dtStartFormat(date)}`;
|
||||
return new RRule({
|
||||
freq: RRule.MONTHLY,
|
||||
interval: value,
|
||||
dtstart: date,
|
||||
}).toString();
|
||||
case CronType.EVERY_N_WEEKS:
|
||||
return `RRULE:FREQ=WEEKLY;INTERVAL=${value.toFixed(0)}\nDTSTART:${dtStartFormat(date)}`;
|
||||
return new RRule({
|
||||
freq: RRule.WEEKLY,
|
||||
interval: value,
|
||||
dtstart: date,
|
||||
}).toString();
|
||||
case CronType.EVERY_N_DAYS:
|
||||
return `RRULE:FREQ=DAILY;INTERVAL=${value.toFixed(0)}\nDTSTART:${dtStartFormat(date)}`;
|
||||
return new RRule({
|
||||
freq: RRule.DAILY,
|
||||
interval: value,
|
||||
dtstart: date,
|
||||
}).toString();
|
||||
case CronType.CUSTOM:
|
||||
if (validCron(fallback)) {
|
||||
return fallback;
|
||||
@@ -160,7 +172,7 @@ export function getCron(
|
||||
}
|
||||
|
||||
function dtStartFormat(date: Date): string {
|
||||
return `${date.getFullYear().toFixed(0)}${(date.getMonth() + 1).toFixed(0).padStart(2, '0')}${date.getDate().toFixed(0).padStart(2, '0')}T000000Z`
|
||||
return `${date.getFullYear().toFixed(0)}${(date.getMonth() + 1).toFixed(0).padStart(2, "0")}${date.getDate().toFixed(0).padStart(2, "0")}T000000Z`;
|
||||
}
|
||||
|
||||
export function isOneTime(cron: string | null): boolean {
|
||||
|
||||
Reference in New Issue
Block a user