Skip to content

Commit

Permalink
Feature: Combine range with steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Nov 19, 2021
1 parent f1150ff commit 227e975
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 84 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ job.stop();
#### Expressions
```javascript
// Run a function according to pattern
Cron('0-4 */5 1,2,3 * JAN-MAR SAT', function () {
console.log('This will run the first five seconds every fifth minute');
Cron('15-45/15 */5 1,2,3 * JAN-MAR SAT', function () {
console.log('This will run every fifteenth second between second 15-45');
console.log('of the first five seconds every fifth minute');
console.log('of hour 1,2 and 3 every saturday in January to March.');
});
```
Expand Down
Binary file modified croner.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 23 additions & 16 deletions dist/croner.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,23 @@
for( i = 0; i < split.length; i++ ) {
this.partToArray(type, split[i], valueIndexOffset);
}
}

// Handle range (-)
} else if( conf.indexOf("-") !== -1 ) {
let handled = false;
if( conf.indexOf("-") !== -1 ) {
this.handleRange(conf, type, valueIndexOffset);
handled = true;
}

// Handle stepping (/)
} else if( conf.indexOf("/") !== -1 ) {
this.handleStepping(conf, type, valueIndexOffset);
if( conf.indexOf("/") !== -1 ) {
this.handleStepping(conf, type, valueIndexOffset, handled);
handled = true;
}

// Handle pure number
} else {
if (!handled) {
this.handleNumber(conf, type, valueIndexOffset);
}

Expand All @@ -437,7 +443,7 @@
* @param {string[]} parts - Each part split as strings
*/
CronPattern.prototype.throwAtIllegalCharacters = function (parts) {
let reValidCron = /[^/*0-9,-]+/;
const reValidCron = /[^/*0-9,-]+/;
for(let i = 0; i < parts.length; i++) {
if( reValidCron.test(parts[i]) ) {
throw new TypeError("CronPattern: configuration entry " + i + " (" + parts[i] + ") contains illegal characters.");
Expand Down Expand Up @@ -473,13 +479,13 @@
* @param {number} valueIndexOffset - -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
*/
CronPattern.prototype.handleRange = function (conf, type, valueIndexOffset) {
let split = conf.split("-");
const split = conf.split("-");

if( split.length !== 2 ) {
throw new TypeError("CronPattern: Syntax error, illegal range: '" + conf + "'");
}

let lower = parseInt(split[0], 10) + valueIndexOffset,
const lower = parseInt(split[0], 10) + valueIndexOffset,
upper = parseInt(split[1], 10) + valueIndexOffset;

if( isNaN(lower) ) {
Expand Down Expand Up @@ -510,27 +516,28 @@
* @param {string} conf - Current part, expected to be a string like * /20 (without the space)
* @param {string} type - One of "seconds", "minutes" etc
* @param {number} valueIndexOffset - -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
* @param {boolean} combine - Combine stepping with a previously run handler
*/
CronPattern.prototype.handleStepping = function (conf, type, valueIndexOffset) {
CronPattern.prototype.handleStepping = function (conf, type, valueIndexOffset, combine) {

let split = conf.split("/");
const split = conf.split("/");

if( split.length !== 2 ) {
throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + conf + "'");
}

if( split[0] !== "*" ) {
throw new TypeError("CronPattern: Syntax error, left part of / needs to be * : '" + conf + "'");
}

let steps = parseInt(split[1], 10);
const steps = parseInt(split[1], 10);

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+")");

for( let i = 0; i < this[type].length; i+= steps ) {
this[type][(i + valueIndexOffset)] = 1;
for( let i = 0; i < this[type].length; i++ ) {
if (i%steps===0) {
this[type][(i + valueIndexOffset)] = (combine === true) ? (this[type][(i + valueIndexOffset)] & 1) : 1;
} else {
this[type][(i + valueIndexOffset)] = 0;
}
}
};

Expand Down
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.

8 changes: 4 additions & 4 deletions docs/Cron.html
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ <h5>Parameters:</h5>
<code>prev</code>
<em>(
<span class="param-type">
date
Date
</span>


Expand Down Expand Up @@ -542,7 +542,7 @@ <h5>Parameters:</h5>
</svg>
</a>
<h4 class="name" id="next">
<span class="type-signature"></span>.next<span class="signature">(prev<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {date|null}</span>
<span class="type-signature"></span>.next<span class="signature">(prev<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Date|null}</span>
</h4>
</span>

Expand Down Expand Up @@ -570,7 +570,7 @@ <h5>Parameters:</h5>
<code>prev</code>
<em>(
<span class="param-type">
date
Date
</span>


Expand Down Expand Up @@ -762,7 +762,7 @@ <h4 class="name" id="pause">
</svg>
</a>
<h4 class="name" id="previous">
<span class="type-signature"></span>.previous<span class="signature">()</span><span class="type-signature"> &rarr; {date|null}</span>
<span class="type-signature"></span>.previous<span class="signature">()</span><span class="type-signature"> &rarr; {Date|null}</span>
</h4>
</span>

Expand Down
4 changes: 2 additions & 2 deletions docs/CronDate.html
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ <h3 class="subsection-title">Methods</h3>
</svg>
</a>
<h4 class="name" id="getDate">
<span class="type-signature"></span>.getDate<span class="signature">(internal)</span><span class="type-signature"> &rarr; {date}</span>
<span class="type-signature"></span>.getDate<span class="signature">(internal)</span><span class="type-signature"> &rarr; {Date}</span>
</h4>
</span>

Expand Down Expand Up @@ -443,7 +443,7 @@ <h5>Parameters:</h5>
</svg>
</a>
<h4 class="name" id="getTime">
<span class="type-signature"></span>.getTime<span class="signature">(internal)</span><span class="type-signature"> &rarr; {date}</span>
<span class="type-signature"></span>.getTime<span class="signature">(internal)</span><span class="type-signature"> &rarr; {Date}</span>
</h4>
</span>

Expand Down
12 changes: 6 additions & 6 deletions docs/croner.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ <h1 class="page-title">
* @property {boolean} [kill] - Job is about to be killed or killed
* @property {boolean} [catch] - Continue exection even if a unhandled error is thrown by triggered function
* @property {number} [maxRuns] - Maximum nuber of executions
* @property {string | date} [startAt] - When to start running
* @property {string | date} [stopAt] - When to stop running
* @property {string | Date} [startAt] - When to start running
* @property {string | Date} [stopAt] - When to stop running
* @property {string} [timezone] - Time zone in Europe/Stockholm format
*/

Expand Down Expand Up @@ -252,8 +252,8 @@ <h1 class="page-title">
/**
* Find next runtime, based on supplied date. Strips milliseconds.
*
* @param {date} [prev] - Input pattern
* @returns {date | null} - Next run time
* @param {Date} [prev] - Input pattern
* @returns {Date | null} - Next run time
*/
Cron.prototype.next = function (prev) {
prev = new CronDate(prev, this.options.timezone);
Expand All @@ -277,7 +277,7 @@ <h1 class="page-title">
* Return previous run time
* @public
*
* @returns {date | null} - Previous run time
* @returns {Date | null} - Previous run time
*/
Cron.prototype.previous = function () {
return this.previousrun ? this.previousrun.getDate() : null;
Expand Down Expand Up @@ -316,7 +316,7 @@ <h1 class="page-title">
* Returns number of milliseconds to next run
* @public
*
* @param {date} [prev] - Starting date, defaults to now
* @param {Date} [prev] - Starting date, defaults to now
* @returns {number | null}
*/
Cron.prototype.msToNext = function (prev) {
Expand Down
10 changes: 5 additions & 5 deletions docs/date.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ <h1 class="page-title">
* Sets internals using a Date
* @private
*
* @param {date} date - Input date
* @param {Date} date - Input date
* @param {boolean} fromLocal - Target already in local time
*/
CronDate.prototype.fromDate = function (date, fromLocal) {
Expand Down Expand Up @@ -191,7 +191,7 @@ <h1 class="page-title">
* Reset internal parameters (seconds, minutes, hours) that may have exceeded their ranges
* @private
*
* @param {date} date - Input date
* @param {Date} date - Input date
*/
CronDate.prototype.apply = function () {
let newDate = new Date(this.years, this.months, this.days, this.hours, this.minutes, this.seconds, this.milliseconds);
Expand All @@ -209,7 +209,7 @@ <h1 class="page-title">
* Sets internals by parsing a string
* @private
*
* @param {date} date - Input date
* @param {Date} date - Input date
*/
CronDate.prototype.fromString = function (str) {

Expand Down Expand Up @@ -350,7 +350,7 @@ <h1 class="page-title">
* @public
*
* @param {boolean} internal - If this is an internal call
* @returns {date}
* @returns {Date}
*/
CronDate.prototype.getDate = function (internal) {
let targetDate = new Date(this.years, this.months, this.days, this.hours, this.minutes, this.seconds, this.milliseconds);
Expand All @@ -367,7 +367,7 @@ <h1 class="page-title">
* @public
*
* @param {boolean} internal - If this is an internal call
* @returns {date}
* @returns {Date}
*/
CronDate.prototype.getTime = function (internal) {
return this.getDate(internal).getTime();
Expand Down
4 changes: 2 additions & 2 deletions docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ <h5 class="subsection-title">Properties:</h5>
|

<span class="param-type">
date
Date
</span>


Expand Down Expand Up @@ -761,7 +761,7 @@ <h5 class="subsection-title">Properties:</h5>
|

<span class="param-type">
date
Date
</span>


Expand Down
18 changes: 14 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ <h1>Croner</h1>
<li>Works in Deno &gt;=1.16</li>
<li>Works in browsers as standalone, UMD or ES-module.</li>
<li><strong>Experimental feature:</strong> Schedule in specific target timezones</li>
<li>Includes <a href="https://www.typescriptlang.org/">TypeScript</a> typings</li>
<li>Includes <a href="https://www.typescriptlang.org/">TypeScript</a> typings, see Usage/Typescript</li>
</ul>
<p>Quick examples:</p>
<pre class="prettyprint source lang-javascript"><code>// Run a function at the interval set by a cron expression
Expand All @@ -158,12 +158,21 @@ <h1>Croner</h1>
<h2>Installation</h2>
<h3>Node.js</h3>
<p><code>npm install croner --save</code></p>
<p>JavaScript</p>
<pre class="prettyprint source lang-javascript"><code>// ESM Import ...
import Cron from &quot;croner&quot;;

// ... or CommonJS Require
const Cron = require(&quot;croner&quot;);
</code></pre>
<p>TypeScript</p>
<p><em>Note that only default export is available in Node.js TypeScript, as the commonjs module is used internally.</em></p>
<pre class="prettyprint source lang-typescript"><code>import Cron from &quot;croner&quot;;

const scheduler : Cron = new Cron(&quot;* * * * * *&quot;, () => {
console.log(&quot;This will run every second.&quot;);
});
</code></pre>
<h3>Deno</h3>
<p>JavaScript</p>
<pre class="prettyprint source lang-javascript"><code>import Cron from &quot;https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js&quot;;
Expand All @@ -175,7 +184,7 @@ <h3>Deno</h3>
<p>TypeScript</p>
<pre class="prettyprint source lang-typescript"><code>import { Cron } from &quot;https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js&quot;;

let scheduler : Cron = new Cron(&quot;* * * * * *&quot;, () => {
const _scheduler : Cron = new Cron(&quot;* * * * * *&quot;, () => {
console.log(&quot;This will run every second.&quot;);
});
</code></pre>
Expand Down Expand Up @@ -343,8 +352,9 @@ <h4>Pattern</h4>
<h3>Examples</h3>
<h4>Expressions</h4>
<pre class="prettyprint source lang-javascript"><code>// Run a function according to pattern
Cron('0-4 */5 1,2,3 * JAN-MAR SAT', function () {
console.log('This will run the first five seconds every fifth minute');
Cron('15-45/15 */5 1,2,3 * JAN-MAR SAT', function () {
console.log('This will run every fifteenth second between second 15-45');
console.log('of the first five seconds every fifth minute');
console.log('of hour 1,2 and 3 every saturday in January to March.');
});
</code></pre>
Expand Down

0 comments on commit 227e975

Please sign in to comment.