refactor: perfect cron rule string
This commit is contained in:
@@ -22,7 +22,7 @@ export function parseCron(cron: string | null): {
|
|||||||
value: 0,
|
value: 0,
|
||||||
date: null,
|
date: null,
|
||||||
};
|
};
|
||||||
} else if (cron.startsWith("RRULE:")) {
|
} else if (cron.startsWith("RRULE:") || cron.startsWith("DTSTART:")) {
|
||||||
return parseRrule(cron);
|
return parseRrule(cron);
|
||||||
} else if ((value = /^0 (\d+) \* \* \*$/i.exec(cron)) !== null) {
|
} else if ((value = /^0 (\d+) \* \* \*$/i.exec(cron)) !== null) {
|
||||||
return {
|
return {
|
||||||
@@ -145,11 +145,23 @@ export function getCron(
|
|||||||
case CronType.EVERY_YEAR:
|
case CronType.EVERY_YEAR:
|
||||||
return `0 0 1 ${value.toFixed(0)} *`;
|
return `0 0 1 ${value.toFixed(0)} *`;
|
||||||
case CronType.EVERY_N_MONTHS:
|
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:
|
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:
|
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:
|
case CronType.CUSTOM:
|
||||||
if (validCron(fallback)) {
|
if (validCron(fallback)) {
|
||||||
return fallback;
|
return fallback;
|
||||||
@@ -160,7 +172,7 @@ export function getCron(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function dtStartFormat(date: Date): string {
|
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 {
|
export function isOneTime(cron: string | null): boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user