@flla/cron-time-generator
Version:
Cron Time Expression Generator
74 lines (73 loc) • 2.36 kB
JavaScript
import Helpers from "./helpers";
/**
* Every Time Class
*/
class EveryTime {
/**
*
* @param {number[]|string|number} every
* @param {{}} config
*/
constructor(every, config = {}) {
this.interval = 1;
this.config = {};
if (every === "even")
every = 2;
this.interval = every;
this.config = Object.assign(this.config, config);
return this;
}
/**
* Every nth Minute
*/
minutes() {
if (this.config["between"] && Array.isArray(this.interval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(0, this.interval.join("-"), Helpers.minute());
}
if (typeof this.interval === "number" && this.interval > 1) {
return Helpers.spliceIntoPosition(0, "*/" + this.interval);
}
else if (this.interval === "uneven") {
return Helpers.spliceIntoPosition(0, "1-59/2");
}
return Helpers.minute();
}
/**
* Every nth Hour
*/
hours() {
const hour = Helpers.hour();
if (this.config["between"] && Array.isArray(this.interval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(1, this.interval.join("-"), hour);
}
if (typeof this.interval === "number" && this.interval > 1) {
return Helpers.spliceIntoPosition(1, "*/" + this.interval, hour);
}
else if (this.interval === "uneven") {
return Helpers.spliceIntoPosition(1, "1-23/2", hour);
}
return hour;
}
/**
* Every nth Days after
* @param hoursOfDay
* @param $minutesOfDay
*/
days(hoursOfDay = 0, $minutesOfDay = 0) {
const day = Helpers.day(hoursOfDay, $minutesOfDay);
if (this.config["between"] && Array.isArray(this.interval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(2, this.interval.join("-"), day);
}
if (typeof this.interval === "number" && this.interval > 1) {
return Helpers.spliceIntoPosition(2, "*/" + this.interval, day);
}
else if (this.interval === "uneven") {
return Helpers.spliceIntoPosition(2, "1-31/2", day);
}
return day;
}
}
export default EveryTime;