natural-cron
Version:
Natural Cron is a easy-to-use Node.js library for creating and validating cron expressions with natural, human-readable APIs.
1 lines • 7.22 kB
JavaScript
var NaturalCron;!function(){"use strict";var e,t={d:function(e,_){for(var s in _)t.o(_,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:_[s]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},_={};t.r(_),t.d(_,{CronExpressionBuilder:function(){return i},CronValidators:function(){return s},SCHEDULES:function(){return r}}),function(e){e.Minute="minute",e.Hour="hour",e.DayOfMonth="dayOfMonth",e.Month="month",e.DayOfWeek="dayOfWeek"}(e||(e={}));class s{static validateMinute(e){if(!Number.isInteger(e)||e<0||e>59)throw new Error(`Invalid minute: ${e}. Minute should be between 0 and 59.`)}static validateHour(e){if(!Number.isInteger(e)||e<0||e>23)throw new Error(`Invalid hour: ${e}. Hour should be between 0 and 23.`)}static validateDayOfMonth(e){if(!Number.isInteger(e)||e<1||e>31)throw new Error(`Invalid day of month: ${e}. Day should be between 1 and 31.`)}static validateMonth(e){if(!Number.isInteger(e)||e<1||e>12)throw new Error(`Invalid month: ${e}. Month should be between 1 and 12.`)}static validateDayOfWeek(e){if(!Number.isInteger(e)||e<0||e>6)throw new Error(`Invalid day of week: ${e}. Day should be between 0 (Sunday) and 6 (Saturday).`)}static validateTime(e){if(!/^([01]?[0-9]|2[0-3]):([0-5]?[0-9])$/.test(e))throw new Error(`Invalid time format for 'at': ${e}`)}static validateTimeUnit(e){if(!["minute","hour","day","month","week"].includes(e))throw new Error(`Invalid time unit for cron: ${e}`)}}class h{static isContiguous(e){let t=[...e].sort(((e,t)=>e-t));for(let e=1;e<t.length;e++)if(t[e]!==t[e-1]+1)return!1;return!0}static formatCronPart(e){const t=Array.from(new Set(e)).sort(((e,t)=>e-t));return h.isContiguous(t)&&t.length>2?`${t[0]}-${t[t.length-1]}`:t.join(",")}static setDefault(e,t,_){void 0===e[t]&&(e[t]=_)}}class i{constructor(){this.schedule={}}ensureDefaultValues(){h.setDefault(this.schedule,"minute","*"),h.setDefault(this.schedule,"hour","*"),h.setDefault(this.schedule,"dayOfMonth","*"),h.setDefault(this.schedule,"month","*"),h.setDefault(this.schedule,"dayOfWeek","*")}atMinutes(e){return e.forEach((e=>s.validateMinute(e))),this.schedule.minute=h.formatCronPart(e),this}atHours(e){return e.forEach((e=>s.validateHour(e))),this.schedule.hour=h.formatCronPart(e),void 0===this.schedule.minute&&(this.schedule.minute="0"),this}atTime(e){s.validateTime(e);const[t,_]=e.split(":"),h=parseInt(t,10),i=parseInt(_,10);return this.schedule.minute=i.toString(),this.schedule.hour=h.toString(),this}every(e){switch(s.validateTimeUnit(e),e){case"minute":this.schedule.minute="*";break;case"hour":this.schedule.minute="0",this.schedule.hour="*";break;case"day":this.schedule.minute="0",this.schedule.hour="0",this.schedule.dayOfMonth="*";break;case"month":this.schedule.minute="0",this.schedule.hour="0",this.schedule.dayOfMonth="1",this.schedule.month="*";break;case"week":this.schedule.minute="0",this.schedule.hour="0",this.schedule.dayOfWeek="0",this.schedule.dayOfMonth="*",this.schedule.month="*"}return this}everyX(t,_){switch(_){case e.Minute:s.validateMinute(t),this.schedule.minute=`*/${t}`,this.schedule.hour="*";break;case e.Hour:s.validateHour(t),this.schedule.minute="0",this.schedule.hour=`*/${t}`;break;case e.DayOfMonth:s.validateDayOfMonth(t),this.schedule.minute="0",this.schedule.hour="0",this.schedule.dayOfMonth=`*/${t}`;break;case e.Month:s.validateMonth(t),this.schedule.minute="0",this.schedule.hour="0",this.schedule.dayOfMonth="1",this.schedule.month=`*/${t}`;break;case e.DayOfWeek:s.validateDayOfWeek(t),this.schedule.minute="0",this.schedule.hour="0",this.schedule.dayOfWeek=`*/${t}`}return this}onWeekDays(e){return e.forEach((e=>s.validateDayOfWeek(e))),this.schedule.dayOfWeek=h.formatCronPart(e),this}onDaysOfMonth(e){return e.forEach((e=>s.validateDayOfMonth(e))),this.schedule.dayOfMonth=h.formatCronPart(e),this}duringMonths(e){return e.forEach((e=>s.validateMonth(e))),this.schedule.month=h.formatCronPart(e),this}compile(){return this.ensureDefaultValues(),`${this.schedule.minute} ${this.schedule.hour} ${this.schedule.dayOfMonth} ${this.schedule.month} ${this.schedule.dayOfWeek}`}}const r={EVERY_SECOND:"* * * * * *",EVERY_5_SECONDS:"*/5 * * * * *",EVERY_10_SECONDS:"*/10 * * * * *",EVERY_30_SECONDS:"*/30 * * * * *",EVERY_MINUTE:"*/1 * * * *",EVERY_5_MINUTES:"0 */5 * * * *",EVERY_10_MINUTES:"0 */10 * * * *",EVERY_30_MINUTES:"0 */30 * * * *",EVERY_HOUR:"0 0-23/1 * * *",EVERY_2_HOURS:"0 0-23/2 * * *",EVERY_3_HOURS:"0 0-23/3 * * *",EVERY_4_HOURS:"0 0-23/4 * * *",EVERY_5_HOURS:"0 0-23/5 * * *",EVERY_6_HOURS:"0 0-23/6 * * *",EVERY_7_HOURS:"0 0-23/7 * * *",EVERY_8_HOURS:"0 0-23/8 * * *",EVERY_9_HOURS:"0 0-23/9 * * *",EVERY_10_HOURS:"0 0-23/10 * * *",EVERY_11_HOURS:"0 0-23/11 * * *",EVERY_12_HOURS:"0 0-23/12 * * *",EVERY_DAY_AT_1AM:"0 01 * * *",EVERY_DAY_AT_2AM:"0 02 * * *",EVERY_DAY_AT_3AM:"0 03 * * *",EVERY_DAY_AT_4AM:"0 04 * * *",EVERY_DAY_AT_5AM:"0 05 * * *",EVERY_DAY_AT_6AM:"0 06 * * *",EVERY_DAY_AT_7AM:"0 07 * * *",EVERY_DAY_AT_8AM:"0 08 * * *",EVERY_DAY_AT_9AM:"0 09 * * *",EVERY_DAY_AT_10AM:"0 10 * * *",EVERY_DAY_AT_11AM:"0 11 * * *",EVERY_DAY_AT_NOON:"0 12 * * *",EVERY_DAY_AT_1PM:"0 13 * * *",EVERY_DAY_AT_2PM:"0 14 * * *",EVERY_DAY_AT_3PM:"0 15 * * *",EVERY_DAY_AT_4PM:"0 16 * * *",EVERY_DAY_AT_5PM:"0 17 * * *",EVERY_DAY_AT_6PM:"0 18 * * *",EVERY_DAY_AT_7PM:"0 19 * * *",EVERY_DAY_AT_8PM:"0 20 * * *",EVERY_DAY_AT_9PM:"0 21 * * *",EVERY_DAY_AT_10PM:"0 22 * * *",EVERY_DAY_AT_11PM:"0 23 * * *",EVERY_DAY_AT_MIDNIGHT:"0 0 * * *",EVERY_WEEK:"0 0 * * 0",EVERY_WEEKDAY:"0 0 * * 1-5",EVERY_WEEKEND:"0 0 * * 6,0",EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT:"0 0 1 * *",EVERY_1ST_DAY_OF_MONTH_AT_NOON:"0 12 1 * *",EVERY_2ND_HOUR:"0 */2 * * *",EVERY_2ND_HOUR_FROM_1AM_THROUGH_11PM:"0 1-23/2 * * *",EVERY_2ND_MONTH:"0 0 1 */2 *",EVERY_QUARTER:"0 0 1 */3 *",EVERY_6_MONTHS:"0 0 1 */6 *",EVERY_YEAR:"0 0 1 1 *",EVERY_30_MINUTES_BETWEEN_9AM_AND_5PM:"0 */30 9-17 * * *",EVERY_30_MINUTES_BETWEEN_9AM_AND_6PM:"0 */30 9-18 * * *",EVERY_30_MINUTES_BETWEEN_10AM_AND_7PM:"0 */30 10-19 * * *",MONDAY_TO_FRIDAY_AT_1AM:"0 0 01 * * 1-5",MONDAY_TO_FRIDAY_AT_2AM:"0 0 02 * * 1-5",MONDAY_TO_FRIDAY_AT_3AM:"0 0 03 * * 1-5",MONDAY_TO_FRIDAY_AT_4AM:"0 0 04 * * 1-5",MONDAY_TO_FRIDAY_AT_5AM:"0 0 05 * * 1-5",MONDAY_TO_FRIDAY_AT_6AM:"0 0 06 * * 1-5",MONDAY_TO_FRIDAY_AT_7AM:"0 0 07 * * 1-5",MONDAY_TO_FRIDAY_AT_8AM:"0 0 08 * * 1-5",MONDAY_TO_FRIDAY_AT_9AM:"0 0 09 * * 1-5",MONDAY_TO_FRIDAY_AT_09_30AM:"0 30 09 * * 1-5",MONDAY_TO_FRIDAY_AT_10AM:"0 0 10 * * 1-5",MONDAY_TO_FRIDAY_AT_11AM:"0 0 11 * * 1-5",MONDAY_TO_FRIDAY_AT_11_30AM:"0 30 11 * * 1-5",MONDAY_TO_FRIDAY_AT_12PM:"0 0 12 * * 1-5",MONDAY_TO_FRIDAY_AT_1PM:"0 0 13 * * 1-5",MONDAY_TO_FRIDAY_AT_2PM:"0 0 14 * * 1-5",MONDAY_TO_FRIDAY_AT_3PM:"0 0 15 * * 1-5",MONDAY_TO_FRIDAY_AT_4PM:"0 0 16 * * 1-5",MONDAY_TO_FRIDAY_AT_5PM:"0 0 17 * * 1-5",MONDAY_TO_FRIDAY_AT_6PM:"0 0 18 * * 1-5",MONDAY_TO_FRIDAY_AT_7PM:"0 0 19 * * 1-5",MONDAY_TO_FRIDAY_AT_8PM:"0 0 20 * * 1-5",MONDAY_TO_FRIDAY_AT_9PM:"0 0 21 * * 1-5",MONDAY_TO_FRIDAY_AT_10PM:"0 0 22 * * 1-5",MONDAY_TO_FRIDAY_AT_11PM:"0 0 23 * * 1-5"};NaturalCron=_}();