Skip to content

Commit

Permalink
Improve code readability. Use non minified version of minitz.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Oct 26, 2022
1 parent da90d50 commit 304fd05
Show file tree
Hide file tree
Showing 18 changed files with 1,492 additions and 230 deletions.
412 changes: 352 additions & 60 deletions dist/croner.cjs

Large diffs are not rendered by default.

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.

481 changes: 477 additions & 4 deletions docs/CronDate.html

Large diffs are not rendered by default.

129 changes: 86 additions & 43 deletions docs/date.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ <h1 class="page-title">
* [
* First item is which member to process,
* Second item is which member to increment if we didn't find a mathch in current item,
* Third item is an offset. if months is handled 0-11 in js date object, and we get 1-12
* Third item is an offset. if months is handled 0-11 in js date object, and we get 1-12 from `this.minute`
* from pattern. Offset should be -1
* ]
*
*/
const RecursionSteps = [
["m", "y", 0],
["d", "m", -1],
["h", "d", 0],
["i", "h", 0],
["s", "i", 0],
["month", "year", 0],
["day", "month", -1],
["hour", "day", 0],
["minute", "hour", 0],
["second", "minute", 0],
];

/**
Expand All @@ -170,10 +170,12 @@ <h1 class="page-title">
function CronDate (d, tz) {

/**
* TimeZone
* @type {string|undefined}
*/
this.tz = tz;

// Populate object using input date, or throw
if (d &amp;&amp; d instanceof Date) {
if (!isNaN(d)) {
this.fromDate(d);
Expand All @@ -200,23 +202,29 @@ <h1 class="page-title">
*/
CronDate.prototype.fromDate = function (inDate) {

/* If this instance of CronDate has a target timezone set,
* use minitz to convert input date object to target timezone
* before extracting hours, minutes, seconds etc.
*
* If not, extract all parts from inDate as-is.
*/
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;
this.second = d.s;
this.minute = d.i;
this.hour = d.h;
this.day = d.d;
this.month = d.m - 1;
this.year = d.y;
} else {
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();
this.second = inDate.getSeconds();
this.minute = inDate.getMinutes();
this.hour = inDate.getHours();
this.day = inDate.getDate();
this.month = inDate.getMonth();
this.year = inDate.getFullYear();
}

};
Expand All @@ -229,13 +237,48 @@ <h1 class="page-title">
*/
CronDate.prototype.fromCronDate = function (d) {
this.tz = d.tz;

/**
* Current full year, in local time or target timezone specified by `this.tz`
* @type {number}
*/
this.year = d.year;

/**
* Current month (1-12), in local time or target timezone specified by `this.tz`
* @type {number}
*/
this.month = d.month;

/**
* Current day (1-31), in local time or target timezone specified by `this.tz`
* @type {number}
*/
this.day = d.day;

/**
* Current hour (0-23), in local time or target timezone specified by `this.tz`
* @type {number}
*/
this.hour = d.hour;

/**
* Current minute (0-59), in local time or target timezone specified by `this.tz`
* @type {number}
*/
this.minute = d.minute;

/**
* Current second (0-59), in local time or target timezone specified by `this.tz`
* @type {number}
*/
this.second = d.second;

/**
* Current milliseconds
* @type {number}
*/
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;
};

/**
Expand All @@ -244,15 +287,15 @@ <h1 class="page-title">
*/
CronDate.prototype.apply = function () {
// If any value could be out of bounds, apply
if (this.m>11||this.d>DaysOfMonth[this.m]||this.h>59||this.i>59||this.s>59) {
const d = new Date(Date.UTC(this.y, this.m, this.d, this.h, this.i, this.s, this.ms));
if (this.month>11||this.day>DaysOfMonth[this.month]||this.hour>59||this.minute>59||this.second>59) {
const d = new Date(Date.UTC(this.year, this.month, this.day, this.hour, this.minute, this.second, this.ms));
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();
this.second = d.getUTCSeconds();
this.minute = d.getUTCMinutes();
this.hour = d.getUTCHours();
this.day = d.getUTCDate();
this.month = d.getUTCMonth();
this.year = d.getUTCFullYear();
return true;
} else {
return false;
Expand Down Expand Up @@ -290,29 +333,29 @@ <h1 class="page-title">
// Pre-calculate last day of month if needed
let lastDayOfMonth;
if (pattern.lastDayOfMonth) {
if (this.m !== 1) {
lastDayOfMonth = DaysOfMonth[this.m]; // About 20% performance increase when using L
if (this.month !== 1) {
lastDayOfMonth = DaysOfMonth[this.month]; // About 20% performance increase when using L
} else {
lastDayOfMonth = new Date(Date.UTC(this.y, this.m+1, 0,0,0,0,0)).getUTCDate();
lastDayOfMonth = new Date(Date.UTC(this.year, this.month+1, 0,0,0,0,0)).getUTCDate();
}
}

// Pre-calculate weekday if needed
// Calculate offset weekday by ((fDomWeekDay + (targetDate - 1)) % 7)
const fDomWeekDay = (!pattern.starDOW &amp;&amp; target == "d") ? new Date(Date.UTC(this.y, this.m, 1,0,0,0,0)).getUTCDay() : undefined;
const fDomWeekDay = (!pattern.starDOW &amp;&amp; target == "day") ? new Date(Date.UTC(this.year, this.month, 1,0,0,0,0)).getUTCDay() : undefined;

for( let i = this[target] + offset; i &lt; pattern[target].length; i++ ) {

// this applies to all "levels"
let match = pattern[target][i];

// Special case for last day of month
if (target === "d" &amp;&amp; pattern.lastDayOfMonth &amp;&amp; i-offset == lastDayOfMonth) {
if (target === "day" &amp;&amp; pattern.lastDayOfMonth &amp;&amp; i-offset == lastDayOfMonth) {
match = true;
}

// Special case for day of week
if (target === "d" &amp;&amp; !pattern.starDOW) {
if (target === "day" &amp;&amp; !pattern.starDOW) {
const dowMatch = pattern.dow[(fDomWeekDay + ((i-offset) - 1)) % 7];
// If we use legacyMode, and dayOfMonth is specified - use "OR" to combine day of week with day of month
// In all other cases use "AND"
Expand Down Expand Up @@ -380,7 +423,7 @@ <h1 class="page-title">
return this;

// ... or out of bounds ?
} else if (this.y >= 3000) {
} else if (this.year >= 3000) {
return null;

// ... oh, go to next part then
Expand All @@ -404,9 +447,9 @@ <h1 class="page-title">

// Always add one second, or minimum interval, then clear milliseconds and apply changes if seconds has gotten out of bounds
if (options.interval > 1 &amp;&amp; hasPreviousRun) {
this.s += options.interval;
this.second += options.interval;
} else {
this.s += 1;
this.second += 1;
}
this.ms = 0;
this.apply();
Expand All @@ -425,9 +468,9 @@ <h1 class="page-title">
*/
CronDate.prototype.getDate = function (internal) {
if (internal || !this.tz) {
return new Date(this.y, this.m, this.d, this.h, this.i, this.s, this.ms);
return new Date(this.year, this.month, this.day, this.hour, this.minute, this.second, this.ms);
} else {
return minitz(this.y, this.m+1, this.d, this.h, this.i, this.s, this.tz);
return minitz(this.year, this.month+1, this.day, this.hour, this.minute, this.second, this.tz);
}
};

Expand Down
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ <h5 class="subsection-title">Properties:</h5>
</svg>
</a>
<h4 class="name" id="CronPatternPart">
<span class="type-signature"></span>.CronPatternPart<span class="type-signature"> :"s"|"i"|"h"|"d"|"m"|"dow"</span>
<span class="type-signature"></span>.CronPatternPart<span class="type-signature"> :"second"|"minute"|"hour"|"day"|"month"|"dow"</span>
</h4>
</span>

Expand Down
26 changes: 13 additions & 13 deletions docs/pattern.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ <h1 class="page-title">

/**
* Name for each part of the cron pattern
* @typedef {("s" | "i" | "h" | "d" | "m" | "dow")} CronPatternPart
* @typedef {("second" | "minute" | "hour" | "day" | "month" | "dow")} CronPatternPart
*/

/**
Expand All @@ -149,12 +149,12 @@ <h1 class="page-title">
this.pattern = pattern;
this.timezone = timezone;

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.second = Array(60).fill(0); // 0-59
this.minute = Array(60).fill(0); // 0-59
this.hour = Array(24).fill(0); // 0-23
this.day = Array(31).fill(0); // 0-30 in array, 1-31 in config
this.month = 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.starDOM = false;
Expand Down Expand Up @@ -226,12 +226,12 @@ <h1 class="page-title">
this.throwAtIllegalCharacters(parts);

// Parse parts into arrays, validates as we go
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);
this.partToArray("second", parts[0], 0);
this.partToArray("minute", parts[1], 0);
this.partToArray("hour", parts[2], 0);
this.partToArray("day", parts[3], -1);
this.partToArray("month", parts[4], -1);
this.partToArray("dow", parts[5], 0);

// 0 = Sunday, 7 = Sunday
if( this.dow[7] ) {
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 304fd05

Please sign in to comment.