Skip to content

Commit

Permalink
Prepare 5.2.0. Codebase cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Oct 7, 2022
1 parent f6c302e commit 390ebca
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 341 deletions.
186 changes: 92 additions & 94 deletions dist/croner.cjs
Expand Up @@ -6,12 +6,12 @@

/* ------------------------------------------------------------------------------------
minitz 3.0.1 - MIT License - Hexagon <hexagon@56k.guru>
minitz 4.0.0 - MIT License - Hexagon <hexagon@56k.guru>
Bundled manually, check https://github.com/Hexagon/minitz for updates
------------------------------------------------------------------------------------ */
function minitz(year,month,day,hour,minute,second,timezone,throwOnInvalidTime){return minitz.fromTZ(minitz.tp(year,month,day,hour,minute,second,timezone),throwOnInvalidTime)}minitz.fromTZISO=(localTimeString,timezone,throwOnInvalidTime)=>{return minitz.fromTZ(parseISOLocal(localTimeString,timezone),throwOnInvalidTime)};minitz.fromTZ=function(timePoint,throwOnInvalidTime){const inputDate=new Date(Date.UTC(timePoint.year,timePoint.month-1,timePoint.day,timePoint.hour,timePoint.minute,timePoint.second)),offset=getTimezoneOffset(timePoint.timezone,inputDate),guessedLocalDate=new Date(inputDate.getTime()-offset),guessedInputDateOffset=getTimezoneOffset(timePoint.timezone,guessedLocalDate);if(guessedInputDateOffset-offset===0){return guessedLocalDate}else {const guessedLocalDate2=new Date(inputDate.getTime()-guessedInputDateOffset),guessedInputDateOffset2=getTimezoneOffset(timePoint.timezone,guessedLocalDate2);if(guessedInputDateOffset2-guessedInputDateOffset===0){return guessedLocalDate2}else if(!throwOnInvalidTime){return guessedLocalDate}else {throw new Error("Invalid date passed to fromTZ()")}}};minitz.toTZ=function(date,tzString){const target=new Date(date.toLocaleString("sv-SE",{timeZone:tzString}));return {year:target.getFullYear(),month:target.getMonth()+1,day:target.getDate(),hour:target.getHours(),minute:target.getMinutes(),second:target.getSeconds(),timezone:tzString}};minitz.tp=(y,m,d,h,i,s,t)=>{return {year:y,month:m,day:d,hour:h,minute:i,second:s,timezone:t}};function getTimezoneOffset(timeZone,date=new Date){const tz=date.toLocaleString("en",{timeZone:timeZone,timeStyle:"long"}).split(" ").slice(-1)[0];const dateString=date.toString();return Date.parse(`${dateString} UTC`)-Date.parse(`${dateString} ${tz}`)}function parseISOLocal(dateTimeString,timezone){const parsed=new Date(Date.parse(dateTimeString));if(isNaN(parsed)){throw new Error("minitz: Invalid ISO8601 passed to parser.")}const stringEnd=dateTimeString.substring(9);if(dateTimeString.includes("Z")||stringEnd.includes("-")||stringEnd.includes("+")){return minitz.tp(parsed.getUTCFullYear(),parsed.getUTCMonth()+1,parsed.getUTCDate(),parsed.getUTCHours(),parsed.getUTCMinutes(),parsed.getUTCSeconds(),"Etc/UTC")}else {return minitz.tp(parsed.getFullYear(),parsed.getMonth()+1,parsed.getDate(),parsed.getHours(),parsed.getMinutes(),parsed.getSeconds(),timezone)}}minitz.minitz=minitz;
function minitz(y,m,d,h,i,s,tz,throwOnInvalid){return minitz.fromTZ(minitz.tp(y,m,d,h,i,s,tz),throwOnInvalid)}minitz.fromTZISO=(localTimeStr,tz,throwOnInvalid)=>{return minitz.fromTZ(parseISOLocal(localTimeStr,tz),throwOnInvalid)};minitz.fromTZ=function(tp,throwOnInvalid){const inDate=new Date(Date.UTC(tp.y,tp.m-1,tp.d,tp.h,tp.i,tp.s)),offset=getTimezoneOffset(tp.tz,inDate),dateGuess=new Date(inDate.getTime()-offset),dateOffsGuess=getTimezoneOffset(tp.tz,dateGuess);if(dateOffsGuess-offset===0){return dateGuess}else {const dateGuess2=new Date(inDate.getTime()-dateOffsGuess),dateOffsGuess2=getTimezoneOffset(tp.tz,dateGuess2);if(dateOffsGuess2-dateOffsGuess===0){return dateGuess2}else if(!throwOnInvalid){return dateGuess}else {throw new Error("Invalid date passed to fromTZ()")}}};minitz.toTZ=function(d,tzStr){const td=new Date(d.toLocaleString("sv-SE",{timeZone:tzStr}));return {y:td.getFullYear(),m:td.getMonth()+1,d:td.getDate(),h:td.getHours(),i:td.getMinutes(),s:td.getSeconds(),tz:tzStr}};minitz.tp=(y,m,d,h,i,s,tz)=>{return {y:y,m:m,d:d,h:h,i:i,s:s,tz:tz}};function getTimezoneOffset(timeZone,date=new Date){const tz=date.toLocaleString("en",{timeZone:timeZone,timeStyle:"long"}).split(" ").slice(-1)[0];const dateString=date.toString();return Date.parse(`${dateString} UTC`)-Date.parse(`${dateString} ${tz}`)}function parseISOLocal(dtStr,tz){const pd=new Date(Date.parse(dtStr));if(isNaN(pd)){throw new Error("minitz: Invalid ISO8601 passed to parser.")}const stringEnd=dtStr.substring(9);if(dtStr.includes("Z")||stringEnd.includes("-")||stringEnd.includes("+")){return minitz.tp(pd.getUTCFullYear(),pd.getUTCMonth()+1,pd.getUTCDate(),pd.getUTCHours(),pd.getUTCMinutes(),pd.getUTCSeconds(),"Etc/UTC")}else {return minitz.tp(pd.getFullYear(),pd.getMonth()+1,pd.getDate(),pd.getHours(),pd.getMinutes(),pd.getSeconds(),tz)}}minitz.minitz=minitz;

/**
* @typedef {Object} CronOptions - Cron scheduler options
Expand Down Expand Up @@ -74,27 +74,27 @@
* Converts date to CronDate
* @constructor
*
* @param {CronDate|date|string} [date] - Input date, if using string representation ISO 8001 (2015-11-24T19:40:00) local timezone is expected
* @param {string} [timezone] - String representation of target timezone in Europe/Stockholm format.
* @param {CronDate|Date|string} [d] - Input date, if using string representation ISO 8001 (2015-11-24T19:40:00) local timezone is expected
* @param {string} [tz] - String representation of target timezone in Europe/Stockholm format.
*/
function CronDate (date, timezone) {
function CronDate (d, tz) {

this.timezone = timezone;
this.tz = tz;

if (date && date instanceof Date) {
if (!isNaN(date)) {
this.fromDate(date);
if (d && d instanceof Date) {
if (!isNaN(d)) {
this.fromDate(d);
} else {
throw new TypeError("CronDate: Invalid date passed as parameter to CronDate constructor");
throw new TypeError("CronDate: Invalid date passed to CronDate constructor");
}
} else if (date === void 0) {
} else if (d === void 0) {
this.fromDate(new Date());
} else if (date && typeof date === "string") {
this.fromString(date);
} else if (date instanceof CronDate) {
this.fromCronDate(date);
} else if (d && typeof d === "string") {
this.fromString(d);
} else if (d instanceof CronDate) {
this.fromCronDate(d);
} else {
throw new TypeError("CronDate: Invalid type (" + typeof date + ") passed as parameter to CronDate constructor");
throw new TypeError("CronDate: Invalid type (" + typeof d + ") passed to CronDate constructor");
}

}
Expand All @@ -103,27 +103,27 @@
* Sets internals using a Date
* @private
*
* @param {Date} date - Input date in local time
* @param {Date} inDate - Input date in local time
*/
CronDate.prototype.fromDate = function (inputDate) {
CronDate.prototype.fromDate = function (inDate) {

if (this.timezone) {
const date = minitz.toTZ(inputDate, this.timezone);
this.milliseconds = inputDate.getMilliseconds();
this.seconds = date.second;
this.minutes = date.minute;
this.hours = date.hour;
this.days = date.day;
this.months = date.month - 1;
this.years = date.year;
if (this.tz) {
const d = minitz.toTZ(inDate, this.tz);
this.ms = inDate.getMilliseconds();
this.s = d.s;
this.i = d.i;
this.h = d.h;
this.d = d.d;
this.m = d.m - 1;
this.y = d.y;
} else {
this.milliseconds = inputDate.getMilliseconds();
this.seconds = inputDate.getSeconds();
this.minutes = inputDate.getMinutes();
this.hours = inputDate.getHours();
this.days = inputDate.getDate();
this.months = inputDate.getMonth();
this.years = inputDate.getFullYear();
this.ms = inDate.getMilliseconds();
this.s = inDate.getSeconds();
this.i = inDate.getMinutes();
this.h = inDate.getHours();
this.d = inDate.getDate();
this.m = inDate.getMonth();
this.y = inDate.getFullYear();
}

};
Expand All @@ -132,35 +132,33 @@
* Sets internals by deep copying another CronDate
* @private
*
* @param {CronDate} date - Input date
* @param {CronDate} d - Input date
*/
CronDate.prototype.fromCronDate = function (date) {
this.timezone = date.timezone;
this.milliseconds = date.milliseconds;
this.seconds = date.seconds;
this.minutes = date.minutes;
this.hours = date.hours;
this.days = date.days;
this.months = date.months;
this.years = date.years;
CronDate.prototype.fromCronDate = function (d) {
this.tz = d.tz;
this.ms = d.ms;
this.s = d.s;
this.i = d.i;
this.h = d.h;
this.d = d.d;
this.m = d.m;
this.y = d.y;
};

/**
* Reset internal parameters (seconds, minutes, hours) that may have exceeded their ranges
* @private
*
* @param {Date} date - Input date
*/
CronDate.prototype.apply = function () {
const newDate = new Date(Date.UTC(this.years, this.months, this.days, this.hours, this.minutes, this.seconds, this.milliseconds));
const d = new Date(Date.UTC(this.y, this.m, this.d, this.h, this.i, this.s, this.ms));

this.milliseconds = newDate.getUTCMilliseconds();
this.seconds = newDate.getUTCSeconds();
this.minutes = newDate.getUTCMinutes();
this.hours = newDate.getUTCHours();
this.days = newDate.getUTCDate();
this.months = newDate.getUTCMonth();
this.years = newDate.getUTCFullYear();
this.ms = d.getUTCMilliseconds();
this.s = d.getUTCSeconds();
this.i = d.getUTCMinutes();
this.h = d.getUTCHours();
this.d = d.getUTCDate();
this.m = d.getUTCMonth();
this.y = d.getUTCFullYear();
};

/**
Expand All @@ -170,7 +168,7 @@
* @param {Date} date - Input date
*/
CronDate.prototype.fromString = function (str) {
return this.fromDate(minitz.fromTZISO(str, this.timezone));
return this.fromDate(minitz.fromTZISO(str, this.tz));
};

/**
Expand All @@ -186,11 +184,11 @@

// Always add one second, or minimum interval, then clear milliseconds and apply changes if seconds has gotten out of bounds
if (options.interval > 1 && hasPreviousRun) {
this.seconds += options.interval;
this.s += options.interval;
} else {
this.seconds += 1;
this.s += 1;
}
this.milliseconds = 0;
this.ms = 0;
this.apply();

const
Expand All @@ -216,7 +214,7 @@
let match = pattern[target][i];

// Days has a couple of special cases
if (target === "days") {
if (target === "d") {

// Create a date object for the target date
const targetDate = this.getDate(true);
Expand All @@ -231,19 +229,19 @@
targetDateCopy.setDate(i-offset+1);

// Overwrite match if last day of month is matching
if (targetDateCopy.getMonth() !== this.months) {
if (targetDateCopy.getMonth() !== this.m) {
match = true;
}

}

// Weekdays must also match when incrementing days
// If running in legacy mode, it is sufficient that only weekday match.
const dowMatch = pattern.daysOfWeek[targetDate.getDay()];
const dowMatch = pattern.dow[targetDate.getDay()];
if (options.legacyMode) {
if (!pattern.starDayOfWeek && pattern.starDayOfMonth) {
if (!pattern.starDOW && pattern.starDOM) {
match = dowMatch;
} else if (!pattern.starDayOfWeek && !pattern.starDayOfMonth) {
} else if (!pattern.starDOW && !pattern.starDOM) {
match = match || dowMatch;
}
} else {
Expand Down Expand Up @@ -290,11 +288,11 @@
// from pattern. Offset should be -1
// ]
const toDo = [
["seconds", "minutes", 0],
["minutes", "hours", 0],
["hours", "days", 0],
["days", "months", -1],
["months", "years", 0]
["s", "i", 0],
["i", "h", 0],
["h", "d", 0],
["d", "m", -1],
["m", "y", 0]
];

// Ok, we're working our way trough the toDo array, top to bottom
Expand Down Expand Up @@ -329,7 +327,7 @@
}

// Bail out if an impossible pattern is used
if (this.years >= 4000) {
if (this.y >= 4000) {
return null;
}

Expand All @@ -350,10 +348,10 @@
* @returns {Date}
*/
CronDate.prototype.getDate = function (internal) {
if (internal || !this.timezone) {
return new Date(this.years, this.months, this.days, this.hours, this.minutes, this.seconds, this.milliseconds);
if (internal || !this.tz) {
return new Date(this.y, this.m, this.d, this.h, this.i, this.s, this.ms);
} else {
return minitz(this.years, this.months+1, this.days, this.hours, this.minutes, this.seconds, this.timezone);
return minitz(this.y, this.m+1, this.d, this.h, this.i, this.s, this.tz);
}
};

Expand All @@ -369,7 +367,7 @@

/**
* Name for each part of the cron pattern
* @typedef {("seconds" | "minutes" | "hours" | "days" | "months" | "daysOfWeek")} CronPatternPart
* @typedef {("s" | "i" | "h" | "d" | "m" | "dow")} CronPatternPart
*/

/**
Expand All @@ -392,16 +390,16 @@
this.pattern = pattern;
this.timezone = timezone;

this.seconds = Array(60).fill(0); // 0-59
this.minutes = Array(60).fill(0); // 0-59
this.hours = Array(24).fill(0); // 0-23
this.days = Array(31).fill(0); // 0-30 in array, 1-31 in config
this.months = Array(12).fill(0); // 0-11 in array, 1-12 in config
this.daysOfWeek = Array(8).fill(0); // 0-7 Where 0 = Sunday and 7=Sunday;
this.s = Array(60).fill(0); // 0-59
this.i = Array(60).fill(0); // 0-59
this.h = Array(24).fill(0); // 0-23
this.d = Array(31).fill(0); // 0-30 in array, 1-31 in config
this.m = Array(12).fill(0); // 0-11 in array, 1-12 in config
this.dow = Array(8).fill(0); // 0-7 Where 0 = Sunday and 7=Sunday;

this.lastDayOfMonth = false;
this.starDayOfMonth = false;
this.starDayOfWeek = false;
this.starDOM = false;
this.starDOW = false;

this.parse();

Expand Down Expand Up @@ -441,18 +439,18 @@
this.lastDayOfMonth = true;
}

// Check for starDayOfMonth
// Check for starDOM
if(parts[3].toUpperCase() == "*") {
this.starDayOfMonth = true;
this.starDOM = true;
}

// Replace alpha representations
parts[4] = this.replaceAlphaMonths(parts[4]);
parts[5] = this.replaceAlphaDays(parts[5]);

// Check for starDayOfWeek
// Check for starDOW
if(parts[5].toUpperCase() == "*") {
this.starDayOfWeek = true;
this.starDOW = true;
}

// Implement '?' in the simplest possible way - replace ? with current value, before further processing
Expand All @@ -469,16 +467,16 @@
this.throwAtIllegalCharacters(parts);

// Parse parts into arrays, validates as we go
this.partToArray("seconds", parts[0], 0);
this.partToArray("minutes", parts[1], 0);
this.partToArray("hours", parts[2], 0);
this.partToArray("days", parts[3], -1);
this.partToArray("months", parts[4], -1);
this.partToArray("daysOfWeek", parts[5], 0);
this.partToArray("s", parts[0], 0);
this.partToArray("i", parts[1], 0);
this.partToArray("h", parts[2], 0);
this.partToArray("d", parts[3], -1);
this.partToArray("m", parts[4], -1);
this.partToArray("dow", parts[5], 0);

// 0 = Sunday, 7 = Sunday
if( this.daysOfWeek[7] ) {
this.daysOfWeek[0] = 1;
if( this.dow[7] ) {
this.dow[0] = 1;
}

};
Expand Down Expand Up @@ -663,7 +661,7 @@

if( isNaN(steps) ) throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");
if( steps === 0 ) throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");
if( steps > this[type].length ) throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[type].length+")");
if( steps > this[type].length ) throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[type].length+")");

for( let i = start; i < this[type].length; i+= steps ) {
this[type][i] = 1;
Expand Down
2 changes: 1 addition & 1 deletion dist/croner.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.cjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.mjs.map

Large diffs are not rendered by default.

0 comments on commit 390ebca

Please sign in to comment.