crontimex
Version:
None-dependencies Cron Time Expression Generator/Builder written in Typescript
75 lines (74 loc) • 2.45 kB
JavaScript
import Helpers from "./helpers.js";
/**
* Every Time Class
*/
class EveryTime {
timeInterval;
config = {};
/**
*
* @param {number[]|string|number} every
* @param {{}} config
*/
constructor(every, config = {}) {
this.timeInterval = 1;
if (every === "even")
every = 2;
this.timeInterval = every;
this.config = Object.assign(this.config, config);
return this;
}
/**
* Every nth Minute
*/
minutes() {
if (this.config["between"] && Array.isArray(this.timeInterval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(0, this.timeInterval.join("-"), Helpers.minute());
}
if (typeof this.timeInterval === "number" && this.timeInterval > 1) {
return Helpers.spliceIntoPosition(0, "*/" + this.timeInterval);
}
else if (this.timeInterval === "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.timeInterval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(1, this.timeInterval.join("-"), hour);
}
if (typeof this.timeInterval === "number" && this.timeInterval > 1) {
return Helpers.spliceIntoPosition(1, "*/" + this.timeInterval, hour);
}
else if (this.timeInterval === "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.timeInterval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(2, this.timeInterval.join("-"), day);
}
if (typeof this.timeInterval === "number" && this.timeInterval > 1) {
return Helpers.spliceIntoPosition(2, "*/" + this.timeInterval, day);
}
else if (this.timeInterval === "uneven") {
return Helpers.spliceIntoPosition(2, "1-31/2", day);
}
return day;
}
}
export default EveryTime;