cron-time-generator
Advanced tools
Comparing version 1.0.0 to 1.0.3
@@ -47,2 +47,4 @@ // @ts-check | ||
hours() { | ||
const hour = Helpers.hour(); | ||
if (this.config["between"] && Array.isArray(this.interval)) { | ||
@@ -53,3 +55,3 @@ this.config["between"] = false; | ||
this.interval.join("-"), | ||
Helpers.hour() | ||
hour | ||
); | ||
@@ -59,8 +61,8 @@ } | ||
if (typeof this.interval === "number" && this.interval > 1) { | ||
return Helpers.spliceIntoPosition(1, "*/" + this.interval); | ||
return Helpers.spliceIntoPosition(1, "*/" + this.interval, hour); | ||
} else if (this.interval === "uneven") { | ||
return Helpers.spliceIntoPosition(1, "1-23/2"); | ||
return Helpers.spliceIntoPosition(1, "1-23/2", hour); | ||
} | ||
return Helpers.hour(); | ||
return hour; | ||
} | ||
@@ -72,5 +74,7 @@ | ||
days() { | ||
const day = Helpers.day(); | ||
if (this.config["at"]) { | ||
this.config["at"] = false; | ||
return Helpers.spliceIntoPosition(2, this.config.at, Helpers.day()); | ||
return Helpers.spliceIntoPosition(2, this.config.at, day); | ||
} | ||
@@ -83,3 +87,3 @@ | ||
this.interval.join("-"), | ||
Helpers.day() | ||
day | ||
); | ||
@@ -89,8 +93,8 @@ } | ||
if (typeof this.interval === "number" && this.interval > 1) { | ||
return Helpers.spliceIntoPosition(2, "*/" + this.interval, Helpers.day()); | ||
return Helpers.spliceIntoPosition(2, "*/" + this.interval, day); | ||
} else if (this.interval === "uneven") { | ||
return Helpers.spliceIntoPosition(2, "1-31/2", Helpers.day()); | ||
return Helpers.spliceIntoPosition(2, "1-31/2", day); | ||
} | ||
return Helpers.day(); | ||
return day; | ||
} | ||
@@ -97,0 +101,0 @@ } |
@@ -31,4 +31,48 @@ // @ts-check | ||
}, | ||
/** | ||
* 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 !== "string") return day; | ||
day = day.toLowerCase(); | ||
switch (day) { | ||
case "sunday": | ||
return 0; | ||
case "monday": | ||
return 1; | ||
case "tuesday": | ||
return 2; | ||
case "wednesday": | ||
return 3; | ||
case "thursday": | ||
return 4; | ||
case "friday": | ||
return 5; | ||
case "saturday": | ||
return 6; | ||
default: | ||
throw Error(`Day: "${day}" is not a valid day.`); | ||
} | ||
}, | ||
/** | ||
* 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 calendar sequence."); | ||
} | ||
}; | ||
module.exports = Helpers; |
74
index.js
@@ -87,3 +87,3 @@ let Helpers = require('./helpers'); | ||
static everyMondayAt($hourOfTheDay, $minuteOfTheHour = 0) { | ||
return `${$minuteOfTheHour} ${$hourOfTheDay} * * SUN`; | ||
return `${$minuteOfTheHour} ${$hourOfTheDay} * * MON`; | ||
} | ||
@@ -189,2 +189,74 @@ | ||
/** | ||
* Every WeekDay | ||
* @param {number|string} $startDay - Starting day (Monday=1, Sunday=0) | ||
* @param {number|string} $endDay - Starting day (Monday=1, Sunday=0) | ||
* @returns {string} | ||
* | ||
* @example | ||
* cronTime.everyWeekDay() | ||
* // Monday to Friday | ||
* cronTime.everyWeekDay('sunday', 'thursday') | ||
* // Sunday to Thursday | ||
*/ | ||
static everyWeekDay($startDay = 'monday', $endDay = 'friday') { | ||
$startDay = Helpers.dayToInt($startDay); | ||
$endDay = Helpers.dayToInt($endDay); | ||
Helpers.validateStartToEndDay($startDay, $endDay); | ||
return `0 0 * * ${$startDay}-${$endDay}` | ||
} | ||
/** | ||
* Every WeekDay At | ||
* @param {number} $hourOfTheDay - Hour of the day | ||
* @param {number} $minuteOfTheHour - Minute of the hour | ||
* @param {number|string} $startDay - Starting day | ||
* @param {number|string} $endDay - Ending day | ||
* @returns {string} | ||
*/ | ||
static everyWeekDayAt($hourOfTheDay, $minuteOfTheHour = 0, $startDay = 'monday', $endDay = 'friday') { | ||
$startDay = Helpers.dayToInt($startDay); | ||
$endDay = Helpers.dayToInt($endDay); | ||
Helpers.validateStartToEndDay($startDay, $endDay); | ||
return `${$minuteOfTheHour} ${$hourOfTheDay} * * ${$startDay}-${$endDay}` | ||
} | ||
/** | ||
* Every Weekend | ||
* @param {number|string} $startDay - Starting day (Monday=1, Sunday=0) | ||
* @param {number|string} $endDay - Starting day (Monday=1, Sunday=0) | ||
* @returns {string} | ||
* | ||
* @example | ||
* cronTime.everyWeekend() | ||
* // Saturday and Sunday | ||
* cronTime.everyWeekend('friday', 'saturday') | ||
* // Friday and Saturday | ||
*/ | ||
static everyWeekend($startDay = 'saturday', $endDay = 'sunday') { | ||
$startDay = Helpers.dayToInt($startDay); | ||
$endDay = Helpers.dayToInt($endDay); | ||
return `0 0 * * ${$startDay},${$endDay}` | ||
} | ||
/** | ||
* Every Weekend At | ||
* @param {number} $hourOfTheDay - Hour of the day | ||
* @param {number} $minuteOfTheHour - Minute of the hour | ||
* @param {number|string} $startDay - Starting day | ||
* @param {number|string} $endDay - Ending day | ||
* @returns {string} | ||
*/ | ||
static everyWeekendAt($hourOfTheDay, $minuteOfTheHour = 0, $startDay = 'saturday', $endDay = 'sunday') { | ||
$startDay = Helpers.dayToInt($startDay); | ||
$endDay = Helpers.dayToInt($endDay); | ||
return `${$minuteOfTheHour} ${$hourOfTheDay} * * ${$startDay},${$endDay}` | ||
} | ||
/** | ||
* Every Month | ||
@@ -191,0 +263,0 @@ */ |
{ | ||
"name": "cron-time-generator", | ||
"version": "1.0.0", | ||
"version": "1.0.3", | ||
"description": "Cron Time Expression Generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -43,5 +43,46 @@ # Cron-Time | ||
cronTime.everyWeekDay(); | ||
// 0 0 * * 1-5 | ||
// from Monday to Friday | ||
cronTime.everyWeekDayAt(1, 30); | ||
// 30 1 * * 1-5 | ||
// 1:30 AM from Monday to Friday | ||
cronTime.everyWeekend(); | ||
// 0 0 * * 6,0 | ||
// on Saturday and Sunday | ||
cronTime.everyWeekendAt(1, 30); | ||
// 30 1 * * 6,0 | ||
// 1:30 AM on Saturday and Sunday | ||
// E.T.C | ||
``` | ||
For `everyWeekDay` and `everyWeekend` there is also an option to change the starting day. | ||
By default week days is from **Monday** to **Friday** while weekend days are **Saturdays** and **Sundays** | ||
This can be changed like so: | ||
```javascript | ||
cronTime.everyWeekDay("sunday", "thursday"); | ||
// 0 0 * * 0-4 | ||
// from Sunday to Thursday | ||
cronTime.everyWeekDayAt(1, 30, "sunday", "thursday"); | ||
// 30 1 * * 0-4 | ||
// 1:30 AM from Sunday to Thursday | ||
cronTime.everyWeekend("friday", "saturday"); | ||
// 0 0 * * 5,6 | ||
// on Friday and Saturday | ||
cronTime.everyWeekendAt(1, 30, "friday", "saturday"); | ||
// 30 1 * * 5,6 | ||
// 1:30 AM on Friday and Saturday | ||
``` | ||
Note: if a `$startDay` is specified then an `$endDay` must be specified also, else it will use the default values which may not tally with your new `$startDay` | ||
Every method of `CronTime` returns exactly what its name says. | ||
@@ -128,2 +169,10 @@ | ||
`everyWeekDay` | ||
`everyWeekDayAt($hour, $minute, $startDay, $endDay)` | ||
`everyWeekend` | ||
`everyWeekendAt($hour, $minute, $startDay, $endDay)` | ||
`everyMonth` | ||
@@ -130,0 +179,0 @@ |
15968
416
183