cron-time-generator2
Version:
Cron Time Expression Generator
86 lines (85 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const DaysDictionary = {
sun: 0,
mon: 1,
tue: 2,
wed: 3,
thu: 4,
fri: 5,
sat: 6,
sunday: 0,
monday: 1,
tuesday: 2,
wednesday: 3,
thursday: 4,
friday: 5,
saturday: 6,
};
exports.default = {
/**
* @param {number} position
* @param {*} value
* @param {string} str
*/
spliceIntoPosition(position, value, str = undefined) {
if (str === undefined) {
str = this.minute();
}
let def = str.split(" ");
def.splice(position, 1, value);
return def.join(" ");
},
minute() {
return "* * * * *";
},
hour() {
return "0 * * * *";
},
day(hourOfTheDay = 0, minuteOfTheHour = 0) {
return `${minuteOfTheHour} ${hourOfTheDay} * * *`;
},
/**
* Get the integer representation of day string.
* @param {string|number} day
* @return {number}
* @example
* Helpers.dayToInt('sunday') // 0
* Helpers.dayToInt('monday') // 1
*/
dayToInt(day) {
if (typeof day === "number")
return day;
day = day.trim().toLowerCase();
const value = DaysDictionary[day];
if (!value)
throw Error(`Day: "${day}" is not a valid day.`);
return value;
},
/**
* Get the integer representation of multiple day strings.
* @param days
*/
daysToIntegers(days) {
const newDays = [];
for (const day of days) {
if (typeof day === "string") {
newDays.push(this.dayToInt(day));
}
else {
newDays.push(day);
}
}
return newDays;
},
/**
* Checks of startDay and endDay follows calendar sequence.
* else throws error.
* @param startDay
* @param endDay
*/
validateStartToEndDay(startDay, endDay) {
if (startDay > endDay)
throw Error("startDay must come before endDay following normal calendar sequence.");
},
};