cron-toolkit-ts
Version:
Type-safe Toolkit to get cron expressions for JavaScript and TypeScript
104 lines (103 loc) • 3.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const WeekDaysMap = new Map([
["Sunday", 0],
["Monday", 1],
["Tuesday", 2],
["Wednesday", 3],
["Thursday", 4],
["Friday", 5],
["Saturday", 6],
]);
class Cron {
constructor() {
this.everyMinute = () => "* * * * *";
this.everyCustomMinute = (minute) => `*/${minute} * * * *`;
this.everyFiveMinutes = () => this.everyCustomMinute(5);
this.every5Minutes = () => this.everyCustomMinute(5); // alias
this.everyTenMinutes = () => this.everyCustomMinute(10);
this.every10Minutes = () => this.everyCustomMinute(10); // alias
this.every15Minutes = () => this.everyCustomMinute(15);
this.every30Minutes = () => this.everyCustomMinute(30);
this.every45Minutes = () => this.everyCustomMinute(45);
this.everyHour = () => "0 * * * *";
this.everyCustomHour = (hour) => `0 */${hour} * * *`;
this.every2Hours = () => this.everyCustomHour(2);
this.every3Hours = () => this.everyCustomHour(3);
this.every6Hours = () => this.everyCustomHour(6);
this.every12Hours = () => this.everyCustomHour(12);
this.everyDay = () => {
return {
/** Everyday at 00:00 */
get: () => "0 0 * * *",
atHour: (hour) => {
return {
get: () => this.everyDayAt(hour),
atMinute: (min) => `${min} ${hour} * * *`
};
}
};
};
this.everyDayAt = (hour) => `0 ${hour} * * *`;
this.everyWeekDay = () => this.fromDay("Monday").toDayAt("Friday", 0);
// everyMonth = () => "0 0 1 * *" as EveryMonths<Months>;
this.everyMonth = () => {
return {
/** At 00:00 on day 1 of month */
get: () => "0 0 1 * *",
onDay: (day) => {
return {
get: () => `0 0 ${day} * *`,
atHour: (hour) => {
return {
get: () => `0 ${hour} ${day} * *`,
atMinute: (min) => `${min} ${hour} ${day} * *`
};
}
};
}
};
};
this.atMinute = (minute) => `${minute} * * * *`;
this.atHour = (hour) => `0 ${hour} * * *`;
this.atDay = (day) => `0 0 ${day} * *`;
this.atMonth = (month) => `0 0 1 ${month} *`;
this.atStartOfTheDay = () => this.atHour(0);
this.atMorning = () => this.atHour(9);
this.atNoon = () => this.atHour(12);
this.atAfternoon = () => this.atHour(15);
this.atEvening = () => this.atHour(18);
this.atNight = () => this.atHour(21);
}
fromDay(from) {
return {
toDay: (to) => `0 0 * * ${WeekDaysMap.get(from)}-${WeekDaysMap.get(to)}`,
toDayAt: (to, hour) => `0 ${hour} * * ${WeekDaysMap.get(from)}-${WeekDaysMap.get(to)}`,
};
}
betweenMinutes(start, end) {
return `${start}-${end} * * * *`;
}
betweenHours(start, end) {
return `0 ${start}-${end} * * *`;
}
betweenDays(start, end) {
return `0 0 ${start}-${end} * *`;
}
betweenMonths(start, end) {
return `0 0 1 ${start}-${end} *`;
}
useNonStandard() {
const everyCustomSecond = (second) => `*/${second} * * * * *`;
return {
everySecond: () => "* * * * * *",
every5Seconds: () => everyCustomSecond(5),
every10Seconds: () => everyCustomSecond(10),
every15Seconds: () => everyCustomSecond(15),
every30Seconds: () => everyCustomSecond(30),
every45Seconds: () => everyCustomSecond(45),
everyCustomSecond,
};
}
}
exports.default = Cron;