croner 9.0.0-dev.1 → 9.0.0-dev.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,21 +26,21 @@ Quick examples:
26
26
 
27
27
  ```javascript
28
28
  // Basic: Run a function at the interval defined by a cron expression
29
- const job = Cron('*/5 * * * * *', () => {
29
+ const job = new Cron('*/5 * * * * *', () => {
30
30
  console.log('This will run every fifth second');
31
31
  });
32
32
 
33
33
  // Enumeration: What dates do the next 100 sundays occur on?
34
- const nextSundays = Cron('0 0 0 * * 7').nextRuns(100);
34
+ const nextSundays = new Cron('0 0 0 * * 7').nextRuns(100);
35
35
  console.log(nextSundays);
36
36
 
37
37
  // Days left to a specific date
38
- const msLeft = Cron('59 59 23 24 DEC *').nextRun() - new Date();
38
+ const msLeft = new Cron('59 59 23 24 DEC *').nextRun() - new Date();
39
39
  console.log(Math.floor(msLeft/1000/3600/24) + " days left to next christmas eve");
40
40
 
41
41
  // Run a function at a specific date/time using a non-local timezone (time is ISO 8601 local time)
42
42
  // This will run 2024-01-23 00:00:00 according to the time in Asia/Kolkata
43
- Cron('2024-01-23T00:00:00', { timezone: 'Asia/Kolkata' }, () => { console.log('Yay!') });
43
+ new Cron('2024-01-23T00:00:00', { timezone: 'Asia/Kolkata' }, () => { console.log('Yay!') });
44
44
 
45
45
  ```
46
46
 
@@ -96,13 +96,13 @@ Cron takes three arguments
96
96
  // - First: Cron pattern, js date object (fire once), or ISO 8601 time string (fire once)
97
97
  // - Second: Options (optional)
98
98
  // - Third: Function run trigger (optional)
99
- const job = Cron("* * * * * *", { maxRuns: 1 }, () => {} );
99
+ const job = new Cron("* * * * * *", { maxRuns: 1 }, () => {} );
100
100
 
101
101
  // If function is omitted in constructor, it can be scheduled later
102
102
  job.schedule(job, /* optional */ context) => {});
103
103
  ```
104
104
 
105
- The job will be sceduled to run at next matching time unless you supply option `{ paused: true }`. The `Cron(...)` constructor will return a Cron instance, later called `job`, which have a couple of methods and properties listed below.
105
+ The job will be sceduled to run at next matching time unless you supply option `{ paused: true }`. The `new Cron(...)` constructor will return a Cron instance, later called `job`, which have a couple of methods and properties listed below.
106
106
 
107
107
  #### Status
108
108
 
package/dist/croner.cjs CHANGED
@@ -145,7 +145,7 @@ var CronPattern = class {
145
145
  * @private
146
146
  */
147
147
  parse() {
148
- if (!(typeof this.pattern === "string")) {
148
+ if (!(typeof this.pattern === "string" || this.pattern instanceof String)) {
149
149
  throw new TypeError("CronPattern: Pattern has to be of type string.");
150
150
  }
151
151
  if (this.pattern.indexOf("@") >= 0) this.pattern = this.handleNicknames(this.pattern).trim();
@@ -371,6 +371,9 @@ var CronPattern = class {
371
371
  if (split.length !== 2) {
372
372
  throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + conf + "'");
373
373
  }
374
+ if (split[0] === "") {
375
+ split[0] = "*";
376
+ }
374
377
  let start = 0;
375
378
  if (split[0] !== "*") {
376
379
  start = parseInt(split[0], 10) + valueIndexOffset;
@@ -1092,6 +1095,12 @@ var Cron = class {
1092
1095
  async trigger() {
1093
1096
  await this._trigger();
1094
1097
  }
1098
+ /**
1099
+ * Returns number of runs left, undefined = unlimited
1100
+ */
1101
+ runsLeft() {
1102
+ return this._states.maxRuns;
1103
+ }
1095
1104
  /**
1096
1105
  * Called when it's time to trigger.
1097
1106
  * Checks if all conditions are currently met,
package/dist/croner.d.cts CHANGED
@@ -240,7 +240,7 @@ declare module "date" {
240
240
  * Current full year, in local time or target timezone specified by `this.tz`
241
241
  */
242
242
  year: number;
243
- constructor(d?: CronDate | Date | string, tz?: string | number);
243
+ constructor(d?: CronDate | Date | string | null, tz?: string | number);
244
244
  /**
245
245
  * Check if the given date is the nth occurrence of a weekday in its month.
246
246
  *
@@ -334,33 +334,6 @@ declare module "croner" {
334
334
  * An array containing all named cron jobs.
335
335
  */
336
336
  const scheduledJobs: Cron[];
337
- /**
338
- * Encapsulate all internal states in the Cron instance.
339
- * Duplicate all options that can change to internal states, for example maxRuns and paused.
340
- */
341
- type CronState = {
342
- kill: boolean;
343
- blocking: boolean;
344
- /**
345
- * Start time of previous trigger, updated after each trigger
346
- *
347
- * Stored to use as the actual previous run, even while a new trigger
348
- * is started. Used by the public funtion `.previousRun()`
349
- */
350
- previousRun: CronDate | undefined;
351
- /**
352
- * Start time of current trigger, this is updated just before triggering
353
- *
354
- * This is used internally as "previous run", as we mostly want to know
355
- * when the previous run _started_
356
- */
357
- currentRun: CronDate | undefined;
358
- once: CronDate | undefined;
359
- currentTimeout: NodeJS.Timer | number | undefined;
360
- maxRuns: number | undefined;
361
- paused: boolean | undefined;
362
- pattern: CronPattern;
363
- };
364
337
  /**
365
338
  * Cron entrypoint
366
339
  *
@@ -372,8 +345,8 @@ declare module "croner" {
372
345
  class Cron {
373
346
  name: string | undefined;
374
347
  options: CronOptions;
375
- _states: CronState;
376
- fn?: Function;
348
+ private _states;
349
+ private fn?;
377
350
  constructor(pattern: string | Date, fnOrOptions1?: CronOptions | Function, fnOrOptions2?: CronOptions | Function);
378
351
  /**
379
352
  * Find next runtime, based on supplied date. Strips milliseconds.
@@ -381,7 +354,7 @@ declare module "croner" {
381
354
  * @param prev - Date to start from
382
355
  * @returns Next run time
383
356
  */
384
- nextRun(prev?: CronDate | Date | string): Date | null;
357
+ nextRun(prev?: CronDate | Date | string | null): Date | null;
385
358
  /**
386
359
  * Find next n runs, based on supplied date. Strips milliseconds.
387
360
  *
@@ -389,7 +362,7 @@ declare module "croner" {
389
362
  * @param previous - Date to start from
390
363
  * @returns - Next n run times
391
364
  */
392
- nextRuns(n: number, previous: Date | string): Date[];
365
+ nextRuns(n: number, previous?: Date | string): Date[];
393
366
  /**
394
367
  * Return the original pattern, if there was one
395
368
  *
@@ -465,6 +438,10 @@ declare module "croner" {
465
438
  * Trigger a run manually
466
439
  */
467
440
  trigger(): Promise<void>;
441
+ /**
442
+ * Returns number of runs left, undefined = unlimited
443
+ */
444
+ runsLeft(): number | undefined;
468
445
  /**
469
446
  * Called when it's time to trigger.
470
447
  * Checks if all conditions are currently met,
@@ -483,5 +460,5 @@ declare module "croner" {
483
460
  private _calculatePreviousRun;
484
461
  }
485
462
  export default Cron;
486
- export { Cron, CronOptions, CronDate, CronPattern, scheduledJobs };
463
+ export { Cron, CronDate, CronOptions, CronPattern, scheduledJobs };
487
464
  }
package/dist/croner.d.ts CHANGED
@@ -240,7 +240,7 @@ declare module "date" {
240
240
  * Current full year, in local time or target timezone specified by `this.tz`
241
241
  */
242
242
  year: number;
243
- constructor(d?: CronDate | Date | string, tz?: string | number);
243
+ constructor(d?: CronDate | Date | string | null, tz?: string | number);
244
244
  /**
245
245
  * Check if the given date is the nth occurrence of a weekday in its month.
246
246
  *
@@ -334,33 +334,6 @@ declare module "croner" {
334
334
  * An array containing all named cron jobs.
335
335
  */
336
336
  const scheduledJobs: Cron[];
337
- /**
338
- * Encapsulate all internal states in the Cron instance.
339
- * Duplicate all options that can change to internal states, for example maxRuns and paused.
340
- */
341
- type CronState = {
342
- kill: boolean;
343
- blocking: boolean;
344
- /**
345
- * Start time of previous trigger, updated after each trigger
346
- *
347
- * Stored to use as the actual previous run, even while a new trigger
348
- * is started. Used by the public funtion `.previousRun()`
349
- */
350
- previousRun: CronDate | undefined;
351
- /**
352
- * Start time of current trigger, this is updated just before triggering
353
- *
354
- * This is used internally as "previous run", as we mostly want to know
355
- * when the previous run _started_
356
- */
357
- currentRun: CronDate | undefined;
358
- once: CronDate | undefined;
359
- currentTimeout: NodeJS.Timer | number | undefined;
360
- maxRuns: number | undefined;
361
- paused: boolean | undefined;
362
- pattern: CronPattern;
363
- };
364
337
  /**
365
338
  * Cron entrypoint
366
339
  *
@@ -372,8 +345,8 @@ declare module "croner" {
372
345
  class Cron {
373
346
  name: string | undefined;
374
347
  options: CronOptions;
375
- _states: CronState;
376
- fn?: Function;
348
+ private _states;
349
+ private fn?;
377
350
  constructor(pattern: string | Date, fnOrOptions1?: CronOptions | Function, fnOrOptions2?: CronOptions | Function);
378
351
  /**
379
352
  * Find next runtime, based on supplied date. Strips milliseconds.
@@ -381,7 +354,7 @@ declare module "croner" {
381
354
  * @param prev - Date to start from
382
355
  * @returns Next run time
383
356
  */
384
- nextRun(prev?: CronDate | Date | string): Date | null;
357
+ nextRun(prev?: CronDate | Date | string | null): Date | null;
385
358
  /**
386
359
  * Find next n runs, based on supplied date. Strips milliseconds.
387
360
  *
@@ -389,7 +362,7 @@ declare module "croner" {
389
362
  * @param previous - Date to start from
390
363
  * @returns - Next n run times
391
364
  */
392
- nextRuns(n: number, previous: Date | string): Date[];
365
+ nextRuns(n: number, previous?: Date | string): Date[];
393
366
  /**
394
367
  * Return the original pattern, if there was one
395
368
  *
@@ -465,6 +438,10 @@ declare module "croner" {
465
438
  * Trigger a run manually
466
439
  */
467
440
  trigger(): Promise<void>;
441
+ /**
442
+ * Returns number of runs left, undefined = unlimited
443
+ */
444
+ runsLeft(): number | undefined;
468
445
  /**
469
446
  * Called when it's time to trigger.
470
447
  * Checks if all conditions are currently met,
@@ -483,5 +460,5 @@ declare module "croner" {
483
460
  private _calculatePreviousRun;
484
461
  }
485
462
  export default Cron;
486
- export { Cron, CronOptions, CronDate, CronPattern, scheduledJobs };
463
+ export { Cron, CronDate, CronOptions, CronPattern, scheduledJobs };
487
464
  }
package/dist/croner.js CHANGED
@@ -115,7 +115,7 @@ var CronPattern = class {
115
115
  * @private
116
116
  */
117
117
  parse() {
118
- if (!(typeof this.pattern === "string")) {
118
+ if (!(typeof this.pattern === "string" || this.pattern instanceof String)) {
119
119
  throw new TypeError("CronPattern: Pattern has to be of type string.");
120
120
  }
121
121
  if (this.pattern.indexOf("@") >= 0) this.pattern = this.handleNicknames(this.pattern).trim();
@@ -341,6 +341,9 @@ var CronPattern = class {
341
341
  if (split.length !== 2) {
342
342
  throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + conf + "'");
343
343
  }
344
+ if (split[0] === "") {
345
+ split[0] = "*";
346
+ }
344
347
  let start = 0;
345
348
  if (split[0] !== "*") {
346
349
  start = parseInt(split[0], 10) + valueIndexOffset;
@@ -1062,6 +1065,12 @@ var Cron = class {
1062
1065
  async trigger() {
1063
1066
  await this._trigger();
1064
1067
  }
1068
+ /**
1069
+ * Returns number of runs left, undefined = unlimited
1070
+ */
1071
+ runsLeft() {
1072
+ return this._states.maxRuns;
1073
+ }
1065
1074
  /**
1066
1075
  * Called when it's time to trigger.
1067
1076
  * Checks if all conditions are currently met,
@@ -145,7 +145,7 @@ var base64 = (() => {
145
145
  * @private
146
146
  */
147
147
  parse() {
148
- if (!(typeof this.pattern === "string")) {
148
+ if (!(typeof this.pattern === "string" || this.pattern instanceof String)) {
149
149
  throw new TypeError("CronPattern: Pattern has to be of type string.");
150
150
  }
151
151
  if (this.pattern.indexOf("@") >= 0) this.pattern = this.handleNicknames(this.pattern).trim();
@@ -371,6 +371,9 @@ var base64 = (() => {
371
371
  if (split.length !== 2) {
372
372
  throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + conf + "'");
373
373
  }
374
+ if (split[0] === "") {
375
+ split[0] = "*";
376
+ }
374
377
  let start = 0;
375
378
  if (split[0] !== "*") {
376
379
  start = parseInt(split[0], 10) + valueIndexOffset;
@@ -1092,6 +1095,12 @@ var base64 = (() => {
1092
1095
  async trigger() {
1093
1096
  await this._trigger();
1094
1097
  }
1098
+ /**
1099
+ * Returns number of runs left, undefined = unlimited
1100
+ */
1101
+ runsLeft() {
1102
+ return this._states.maxRuns;
1103
+ }
1095
1104
  /**
1096
1105
  * Called when it's time to trigger.
1097
1106
  * Checks if all conditions are currently met,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "croner",
3
- "version": "9.0.0-dev.1",
3
+ "version": "9.0.0-dev.3",
4
4
  "description": "Trigger functions and/or evaluate cron expressions in JavaScript. No dependencies. Most features. All environments.",
5
5
  "author": "Hexagon <github.com/hexagon>",
6
6
  "homepage": "https://croner.56k.guru",