@sapphire/cron 1.1.4-next.e7fdc5db.0 → 1.1.4-next.ec73e32c
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/dist/cjs/index.cjs +10 -22
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.mjs +10 -22
- package/dist/esm/index.mjs.map +1 -1
- package/dist/iife/index.global.js +11 -23
- package/dist/iife/index.global.js.map +1 -1
- package/package.json +9 -8
package/dist/cjs/index.cjs
CHANGED
|
@@ -5,10 +5,7 @@ var utilities = require('@sapphire/utilities');
|
|
|
5
5
|
var __defProp = Object.defineProperty;
|
|
6
6
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
-
var __publicField = (obj, key, value) =>
|
|
9
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
10
|
-
return value;
|
|
11
|
-
};
|
|
8
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
12
9
|
|
|
13
10
|
// src/lib/constants.ts
|
|
14
11
|
var Time = /* @__PURE__ */ ((Time2) => {
|
|
@@ -88,15 +85,12 @@ var _Cron = class _Cron {
|
|
|
88
85
|
if (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {
|
|
89
86
|
return this.next(new Date(outset.getTime() + 864e5 /* Day */), false);
|
|
90
87
|
}
|
|
91
|
-
if (!origin)
|
|
92
|
-
return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));
|
|
88
|
+
if (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));
|
|
93
89
|
const now = new Date(outset.getTime() + 6e4);
|
|
94
90
|
for (const hour of this.hours) {
|
|
95
|
-
if (hour < now.getUTCHours())
|
|
96
|
-
continue;
|
|
91
|
+
if (hour < now.getUTCHours()) continue;
|
|
97
92
|
for (const minute of this.minutes) {
|
|
98
|
-
if (hour === now.getUTCHours() && minute < now.getUTCMinutes())
|
|
99
|
-
continue;
|
|
93
|
+
if (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;
|
|
100
94
|
return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));
|
|
101
95
|
}
|
|
102
96
|
}
|
|
@@ -107,13 +101,11 @@ var _Cron = class _Cron {
|
|
|
107
101
|
* @param cron The pattern to normalize
|
|
108
102
|
*/
|
|
109
103
|
static normalize(cron) {
|
|
110
|
-
if (Reflect.has(predefined, cron))
|
|
111
|
-
return Reflect.get(predefined, cron);
|
|
104
|
+
if (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);
|
|
112
105
|
const now = /* @__PURE__ */ new Date();
|
|
113
106
|
cron = cron.split(" ").map(
|
|
114
107
|
(val, i) => val.replace(wildcardRegex, (match) => {
|
|
115
|
-
if (match === "h")
|
|
116
|
-
return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();
|
|
108
|
+
if (match === "h") return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();
|
|
117
109
|
if (match === "?") {
|
|
118
110
|
switch (i) {
|
|
119
111
|
case 0:
|
|
@@ -139,8 +131,7 @@ var _Cron = class _Cron {
|
|
|
139
131
|
*/
|
|
140
132
|
static parseString(cron) {
|
|
141
133
|
const parts = cron.split(" ");
|
|
142
|
-
if (parts.length !== 5)
|
|
143
|
-
throw new Error("Invalid Cron Provided");
|
|
134
|
+
if (parts.length !== 5) throw new Error("Invalid Cron Provided");
|
|
144
135
|
return parts.map((part, i) => _Cron.parsePart(part, i));
|
|
145
136
|
}
|
|
146
137
|
/**
|
|
@@ -151,16 +142,13 @@ var _Cron = class _Cron {
|
|
|
151
142
|
static parsePart(cronPart, id) {
|
|
152
143
|
if (cronPart.includes(",")) {
|
|
153
144
|
const res = [];
|
|
154
|
-
for (const part of cronPart.split(","))
|
|
155
|
-
res.push(..._Cron.parsePart(part, id));
|
|
145
|
+
for (const part of cronPart.split(",")) res.push(..._Cron.parsePart(part, id));
|
|
156
146
|
return [...new Set(res)].sort((a, b) => a - b);
|
|
157
147
|
}
|
|
158
148
|
const [, wild, minStr, maxStr, step] = partRegex.exec(cronPart);
|
|
159
149
|
let [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];
|
|
160
|
-
if (wild)
|
|
161
|
-
|
|
162
|
-
else if (!max && !step)
|
|
163
|
-
return [min];
|
|
150
|
+
if (wild) [min, max] = allowedNum[id];
|
|
151
|
+
else if (!max && !step) return [min];
|
|
164
152
|
[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);
|
|
165
153
|
return utilities.range(min, max, parseInt(step, 10) || 1);
|
|
166
154
|
}
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Cron.ts"],"names":["Time"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Cron.ts"],"names":["Time"],"mappings":";;;;;;AAAO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,gBAAa,QAAb;AACA,EAAAA,YAAA,iBAAc,QAAd;AACA,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,UAAO,UAAP;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAVW,SAAAA;AAAA,GAAA;AAaL,IAAM,YAA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AAG,IAAI,WAAW,EAAE;AAAA,aAG3B,CAAC,OAAO,CAAC,KAAM,QAAO,CAAC,GAAG;AAQnC,KAAC,KAAK,GAAG,IAAI,CAAC,KAAK,OAAO,WAAW,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAGjE,WAAO,MAAM,KAAK,KAAK,SAAS,MAAM,EAAE,KAAK,CAAC;AAAA,EAC/C;AACD;AAvHkB;AAAX,IAAM,OAAN","sourcesContent":["export enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\nexport const partRegex = /^(?:(\\*)|(\\d+)(?:-(\\d+))?)(?:\\/(\\d+))?$/;\n\nexport const wildcardRegex = /\\bh\\b|\\B\\?\\B/g;\n\nexport const allowedNum = [\n\t[0, 59],\n\t[0, 23],\n\t[1, 31],\n\t[1, 12],\n\t[0, 6]\n];\n\nexport const predefined = {\n\t'@annually': '0 0 1 1 *',\n\t'@yearly': '0 0 1 1 *',\n\t'@monthly': '0 0 1 * *',\n\t'@weekly': '0 0 * * 0',\n\t'@daily': '0 0 * * *',\n\t'@hourly': '0 * * * *'\n} as const;\n\nexport const cronTokens = {\n\tjan: 1,\n\tfeb: 2,\n\tmar: 3,\n\tapr: 4,\n\tmay: 5,\n\tjun: 6,\n\tjul: 7,\n\taug: 8,\n\tsep: 9,\n\toct: 10,\n\tnov: 11,\n\tdec: 12,\n\tsun: 0,\n\tmon: 1,\n\ttue: 2,\n\twed: 3,\n\tthu: 4,\n\tfri: 5,\n\tsat: 6\n} as const;\n\nexport const tokensRegex = new RegExp(Object.keys(cronTokens).join('|'), 'g');\n","import { range } from '@sapphire/utilities';\nimport { allowedNum, cronTokens, partRegex, predefined, Time, tokensRegex, wildcardRegex } from './constants';\n\n/**\n * Handles Cron strings and generates dates based on the cron string provided.\n * @see https://en.wikipedia.org/wiki/Cron\n */\nexport class Cron {\n\tpublic cron: string;\n\tpublic normalized: string;\n\tpublic minutes: number[];\n\tpublic hours: number[];\n\tpublic days: number[];\n\tpublic months: number[];\n\tpublic dows: number[];\n\n\t/**\n\t * @param cron The cron pattern to use\n\t */\n\tpublic constructor(cron: string) {\n\t\tthis.cron = cron.toLowerCase();\n\t\tthis.normalized = Cron.normalize(this.cron);\n\t\t[this.minutes, this.hours, this.days, this.months, this.dows] = Cron.parseString(this.normalized);\n\t}\n\n\t/**\n\t * Get the next date that matches with the current pattern\n\t * @param outset The Date instance to compare with\n\t * @param origin Whether this next call is origin\n\t */\n\tpublic next(outset: Date = new Date(), origin = true): Date {\n\t\tif (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {\n\t\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t\t}\n\t\tif (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));\n\n\t\tconst now = new Date(outset.getTime() + 60000);\n\n\t\tfor (const hour of this.hours) {\n\t\t\tif (hour < now.getUTCHours()) continue;\n\t\t\tfor (const minute of this.minutes) {\n\t\t\t\tif (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;\n\t\t\t\treturn new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));\n\t\t\t}\n\t\t}\n\n\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t}\n\n\t/**\n\t * Normalize the pattern\n\t * @param cron The pattern to normalize\n\t */\n\tprivate static normalize(cron: string): string {\n\t\tif (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);\n\t\tconst now = new Date();\n\t\tcron = cron\n\t\t\t.split(' ')\n\t\t\t.map((val, i) =>\n\t\t\t\tval.replace(wildcardRegex, (match) => {\n\t\t\t\t\tif (match === 'h') return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();\n\n\t\t\t\t\tif (match === '?') {\n\t\t\t\t\t\tswitch (i) {\n\t\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\t\treturn now.getUTCMinutes().toString();\n\t\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\t\treturn now.getUTCHours().toString();\n\t\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\t\treturn now.getUTCDate().toString();\n\t\t\t\t\t\t\tcase 3:\n\t\t\t\t\t\t\t\treturn now.getUTCMonth().toString();\n\t\t\t\t\t\t\tcase 4:\n\t\t\t\t\t\t\t\treturn now.getUTCDay().toString();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn match;\n\t\t\t\t})\n\t\t\t)\n\t\t\t.join(' ');\n\t\treturn cron.replace(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));\n\t}\n\n\t/**\n\t * Parse the pattern\n\t * @param cron The pattern to parse\n\t */\n\tprivate static parseString(cron: string): Array<number[]> {\n\t\tconst parts = cron.split(' ');\n\t\tif (parts.length !== 5) throw new Error('Invalid Cron Provided');\n\t\treturn parts.map((part, i) => Cron.parsePart(part, i));\n\t}\n\n\t/**\n\t * Parse the current part\n\t * @param cronPart The part of the pattern to parse\n\t * @param id The id that identifies the current part\n\t */\n\tprivate static parsePart(cronPart: string, id: number): number[] {\n\t\tif (cronPart.includes(',')) {\n\t\t\tconst res: number[] = [];\n\t\t\tfor (const part of cronPart.split(',')) res.push(...Cron.parsePart(part, id));\n\t\t\treturn [...new Set(res)].sort((a, b) => a - b);\n\t\t}\n\n\t\tconst [, wild, minStr, maxStr, step] = partRegex.exec(cronPart)!;\n\t\tlet [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];\n\n\t\t// If '*', set min and max as the minimum and maximum allowed numbers:\n\t\tif (wild) [min, max] = allowedNum[id];\n\t\t// Else if a number was given, but not a maximum nor a step, return it\n\t\t// as only allowed value:\n\t\telse if (!max && !step) return [min];\n\n\t\t// Set min and max as the given numbers, defaulting max to the maximum\n\t\t// allowed, so min is never bigger than max:\n\t\t// This makes min and max be, in the following cases (considering minutes):\n\t\t// -> 1-2 | 1..2\n\t\t// -> 2-1 | 1..2\n\t\t// -> 1/7 | 1, 8, 15, 22, 29, 36, 43, 50, 57\n\t\t[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);\n\n\t\t// Generate a range\n\t\treturn range(min, max, parseInt(step, 10) || 1);\n\t}\n}\n"]}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -3,10 +3,7 @@ import { range } from '@sapphire/utilities';
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
var __publicField = (obj, key, value) =>
|
|
7
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
-
return value;
|
|
9
|
-
};
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
10
7
|
|
|
11
8
|
// src/lib/constants.ts
|
|
12
9
|
var Time = /* @__PURE__ */ ((Time2) => {
|
|
@@ -86,15 +83,12 @@ var _Cron = class _Cron {
|
|
|
86
83
|
if (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {
|
|
87
84
|
return this.next(new Date(outset.getTime() + 864e5 /* Day */), false);
|
|
88
85
|
}
|
|
89
|
-
if (!origin)
|
|
90
|
-
return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));
|
|
86
|
+
if (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));
|
|
91
87
|
const now = new Date(outset.getTime() + 6e4);
|
|
92
88
|
for (const hour of this.hours) {
|
|
93
|
-
if (hour < now.getUTCHours())
|
|
94
|
-
continue;
|
|
89
|
+
if (hour < now.getUTCHours()) continue;
|
|
95
90
|
for (const minute of this.minutes) {
|
|
96
|
-
if (hour === now.getUTCHours() && minute < now.getUTCMinutes())
|
|
97
|
-
continue;
|
|
91
|
+
if (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;
|
|
98
92
|
return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));
|
|
99
93
|
}
|
|
100
94
|
}
|
|
@@ -105,13 +99,11 @@ var _Cron = class _Cron {
|
|
|
105
99
|
* @param cron The pattern to normalize
|
|
106
100
|
*/
|
|
107
101
|
static normalize(cron) {
|
|
108
|
-
if (Reflect.has(predefined, cron))
|
|
109
|
-
return Reflect.get(predefined, cron);
|
|
102
|
+
if (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);
|
|
110
103
|
const now = /* @__PURE__ */ new Date();
|
|
111
104
|
cron = cron.split(" ").map(
|
|
112
105
|
(val, i) => val.replace(wildcardRegex, (match) => {
|
|
113
|
-
if (match === "h")
|
|
114
|
-
return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();
|
|
106
|
+
if (match === "h") return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();
|
|
115
107
|
if (match === "?") {
|
|
116
108
|
switch (i) {
|
|
117
109
|
case 0:
|
|
@@ -137,8 +129,7 @@ var _Cron = class _Cron {
|
|
|
137
129
|
*/
|
|
138
130
|
static parseString(cron) {
|
|
139
131
|
const parts = cron.split(" ");
|
|
140
|
-
if (parts.length !== 5)
|
|
141
|
-
throw new Error("Invalid Cron Provided");
|
|
132
|
+
if (parts.length !== 5) throw new Error("Invalid Cron Provided");
|
|
142
133
|
return parts.map((part, i) => _Cron.parsePart(part, i));
|
|
143
134
|
}
|
|
144
135
|
/**
|
|
@@ -149,16 +140,13 @@ var _Cron = class _Cron {
|
|
|
149
140
|
static parsePart(cronPart, id) {
|
|
150
141
|
if (cronPart.includes(",")) {
|
|
151
142
|
const res = [];
|
|
152
|
-
for (const part of cronPart.split(","))
|
|
153
|
-
res.push(..._Cron.parsePart(part, id));
|
|
143
|
+
for (const part of cronPart.split(",")) res.push(..._Cron.parsePart(part, id));
|
|
154
144
|
return [...new Set(res)].sort((a, b) => a - b);
|
|
155
145
|
}
|
|
156
146
|
const [, wild, minStr, maxStr, step] = partRegex.exec(cronPart);
|
|
157
147
|
let [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];
|
|
158
|
-
if (wild)
|
|
159
|
-
|
|
160
|
-
else if (!max && !step)
|
|
161
|
-
return [min];
|
|
148
|
+
if (wild) [min, max] = allowedNum[id];
|
|
149
|
+
else if (!max && !step) return [min];
|
|
162
150
|
[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);
|
|
163
151
|
return range(min, max, parseInt(step, 10) || 1);
|
|
164
152
|
}
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Cron.ts"],"names":["Time"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Cron.ts"],"names":["Time"],"mappings":";;;;;;AAAO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,gBAAa,QAAb;AACA,EAAAA,YAAA,iBAAc,QAAd;AACA,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,UAAO,UAAP;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EA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GAAG,SAAS,QAAQ,EAAE,CAAC;AAG5D,QAAI,KAAM,EAAC,KAAK,GAAG,IAAI,WAAW,EAAE;AAAA,aAG3B,CAAC,OAAO,CAAC,KAAM,QAAO,CAAC,GAAG;AAQnC,KAAC,KAAK,GAAG,IAAI,CAAC,KAAK,OAAO,WAAW,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAGjE,WAAO,MAAM,KAAK,KAAK,SAAS,MAAM,EAAE,KAAK,CAAC;AAAA,EAC/C;AACD;AAvHkB;AAAX,IAAM,OAAN","sourcesContent":["export enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\nexport const partRegex = /^(?:(\\*)|(\\d+)(?:-(\\d+))?)(?:\\/(\\d+))?$/;\n\nexport const wildcardRegex = /\\bh\\b|\\B\\?\\B/g;\n\nexport const allowedNum = [\n\t[0, 59],\n\t[0, 23],\n\t[1, 31],\n\t[1, 12],\n\t[0, 6]\n];\n\nexport const predefined = {\n\t'@annually': '0 0 1 1 *',\n\t'@yearly': '0 0 1 1 *',\n\t'@monthly': '0 0 1 * *',\n\t'@weekly': '0 0 * * 0',\n\t'@daily': '0 0 * * *',\n\t'@hourly': '0 * * * *'\n} as const;\n\nexport const cronTokens = {\n\tjan: 1,\n\tfeb: 2,\n\tmar: 3,\n\tapr: 4,\n\tmay: 5,\n\tjun: 6,\n\tjul: 7,\n\taug: 8,\n\tsep: 9,\n\toct: 10,\n\tnov: 11,\n\tdec: 12,\n\tsun: 0,\n\tmon: 1,\n\ttue: 2,\n\twed: 3,\n\tthu: 4,\n\tfri: 5,\n\tsat: 6\n} as const;\n\nexport const tokensRegex = new RegExp(Object.keys(cronTokens).join('|'), 'g');\n","import { range } from '@sapphire/utilities';\nimport { allowedNum, cronTokens, partRegex, predefined, Time, tokensRegex, wildcardRegex } from './constants';\n\n/**\n * Handles Cron strings and generates dates based on the cron string provided.\n * @see https://en.wikipedia.org/wiki/Cron\n */\nexport class Cron {\n\tpublic cron: string;\n\tpublic normalized: string;\n\tpublic minutes: number[];\n\tpublic hours: number[];\n\tpublic days: number[];\n\tpublic months: number[];\n\tpublic dows: number[];\n\n\t/**\n\t * @param cron The cron pattern to use\n\t */\n\tpublic constructor(cron: string) {\n\t\tthis.cron = cron.toLowerCase();\n\t\tthis.normalized = Cron.normalize(this.cron);\n\t\t[this.minutes, this.hours, this.days, this.months, this.dows] = Cron.parseString(this.normalized);\n\t}\n\n\t/**\n\t * Get the next date that matches with the current pattern\n\t * @param outset The Date instance to compare with\n\t * @param origin Whether this next call is origin\n\t */\n\tpublic next(outset: Date = new Date(), origin = true): Date {\n\t\tif (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {\n\t\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t\t}\n\t\tif (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));\n\n\t\tconst now = new Date(outset.getTime() + 60000);\n\n\t\tfor (const hour of this.hours) {\n\t\t\tif (hour < now.getUTCHours()) continue;\n\t\t\tfor (const minute of this.minutes) {\n\t\t\t\tif (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;\n\t\t\t\treturn new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));\n\t\t\t}\n\t\t}\n\n\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t}\n\n\t/**\n\t * Normalize the pattern\n\t * @param cron The pattern to normalize\n\t */\n\tprivate static normalize(cron: string): string {\n\t\tif (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);\n\t\tconst now = new Date();\n\t\tcron = cron\n\t\t\t.split(' ')\n\t\t\t.map((val, i) =>\n\t\t\t\tval.replace(wildcardRegex, (match) => {\n\t\t\t\t\tif (match === 'h') return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();\n\n\t\t\t\t\tif (match === '?') {\n\t\t\t\t\t\tswitch (i) {\n\t\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\t\treturn now.getUTCMinutes().toString();\n\t\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\t\treturn now.getUTCHours().toString();\n\t\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\t\treturn now.getUTCDate().toString();\n\t\t\t\t\t\t\tcase 3:\n\t\t\t\t\t\t\t\treturn now.getUTCMonth().toString();\n\t\t\t\t\t\t\tcase 4:\n\t\t\t\t\t\t\t\treturn now.getUTCDay().toString();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn match;\n\t\t\t\t})\n\t\t\t)\n\t\t\t.join(' ');\n\t\treturn cron.replace(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));\n\t}\n\n\t/**\n\t * Parse the pattern\n\t * @param cron The pattern to parse\n\t */\n\tprivate static parseString(cron: string): Array<number[]> {\n\t\tconst parts = cron.split(' ');\n\t\tif (parts.length !== 5) throw new Error('Invalid Cron Provided');\n\t\treturn parts.map((part, i) => Cron.parsePart(part, i));\n\t}\n\n\t/**\n\t * Parse the current part\n\t * @param cronPart The part of the pattern to parse\n\t * @param id The id that identifies the current part\n\t */\n\tprivate static parsePart(cronPart: string, id: number): number[] {\n\t\tif (cronPart.includes(',')) {\n\t\t\tconst res: number[] = [];\n\t\t\tfor (const part of cronPart.split(',')) res.push(...Cron.parsePart(part, id));\n\t\t\treturn [...new Set(res)].sort((a, b) => a - b);\n\t\t}\n\n\t\tconst [, wild, minStr, maxStr, step] = partRegex.exec(cronPart)!;\n\t\tlet [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];\n\n\t\t// If '*', set min and max as the minimum and maximum allowed numbers:\n\t\tif (wild) [min, max] = allowedNum[id];\n\t\t// Else if a number was given, but not a maximum nor a step, return it\n\t\t// as only allowed value:\n\t\telse if (!max && !step) return [min];\n\n\t\t// Set min and max as the given numbers, defaulting max to the maximum\n\t\t// allowed, so min is never bigger than max:\n\t\t// This makes min and max be, in the following cases (considering minutes):\n\t\t// -> 1-2 | 1..2\n\t\t// -> 2-1 | 1..2\n\t\t// -> 1/7 | 1, 8, 15, 22, 29, 36, 43, 50, 57\n\t\t[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);\n\n\t\t// Generate a range\n\t\treturn range(min, max, parseInt(step, 10) || 1);\n\t}\n}\n"]}
|
|
@@ -4,10 +4,7 @@ var SapphireCron = (function (exports) {
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
6
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
-
var __publicField = (obj, key, value) =>
|
|
8
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
-
return value;
|
|
10
|
-
};
|
|
7
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
11
8
|
|
|
12
9
|
// src/lib/constants.ts
|
|
13
10
|
var Time = /* @__PURE__ */ ((Time2) => {
|
|
@@ -63,7 +60,7 @@ var SapphireCron = (function (exports) {
|
|
|
63
60
|
};
|
|
64
61
|
var tokensRegex = new RegExp(Object.keys(cronTokens).join("|"), "g");
|
|
65
62
|
|
|
66
|
-
// ../utilities/dist/esm/chunk-
|
|
63
|
+
// ../utilities/dist/esm/chunk-PAWJFY3S.mjs
|
|
67
64
|
var __defProp2 = Object.defineProperty;
|
|
68
65
|
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
|
|
69
66
|
|
|
@@ -100,15 +97,12 @@ var SapphireCron = (function (exports) {
|
|
|
100
97
|
if (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {
|
|
101
98
|
return this.next(new Date(outset.getTime() + 864e5 /* Day */), false);
|
|
102
99
|
}
|
|
103
|
-
if (!origin)
|
|
104
|
-
return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));
|
|
100
|
+
if (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));
|
|
105
101
|
const now = new Date(outset.getTime() + 6e4);
|
|
106
102
|
for (const hour of this.hours) {
|
|
107
|
-
if (hour < now.getUTCHours())
|
|
108
|
-
continue;
|
|
103
|
+
if (hour < now.getUTCHours()) continue;
|
|
109
104
|
for (const minute of this.minutes) {
|
|
110
|
-
if (hour === now.getUTCHours() && minute < now.getUTCMinutes())
|
|
111
|
-
continue;
|
|
105
|
+
if (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;
|
|
112
106
|
return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));
|
|
113
107
|
}
|
|
114
108
|
}
|
|
@@ -119,13 +113,11 @@ var SapphireCron = (function (exports) {
|
|
|
119
113
|
* @param cron The pattern to normalize
|
|
120
114
|
*/
|
|
121
115
|
static normalize(cron) {
|
|
122
|
-
if (Reflect.has(predefined, cron))
|
|
123
|
-
return Reflect.get(predefined, cron);
|
|
116
|
+
if (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);
|
|
124
117
|
const now = /* @__PURE__ */ new Date();
|
|
125
118
|
cron = cron.split(" ").map(
|
|
126
119
|
(val, i) => val.replace(wildcardRegex, (match) => {
|
|
127
|
-
if (match === "h")
|
|
128
|
-
return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();
|
|
120
|
+
if (match === "h") return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();
|
|
129
121
|
if (match === "?") {
|
|
130
122
|
switch (i) {
|
|
131
123
|
case 0:
|
|
@@ -151,8 +143,7 @@ var SapphireCron = (function (exports) {
|
|
|
151
143
|
*/
|
|
152
144
|
static parseString(cron) {
|
|
153
145
|
const parts = cron.split(" ");
|
|
154
|
-
if (parts.length !== 5)
|
|
155
|
-
throw new Error("Invalid Cron Provided");
|
|
146
|
+
if (parts.length !== 5) throw new Error("Invalid Cron Provided");
|
|
156
147
|
return parts.map((part, i) => _Cron.parsePart(part, i));
|
|
157
148
|
}
|
|
158
149
|
/**
|
|
@@ -163,16 +154,13 @@ var SapphireCron = (function (exports) {
|
|
|
163
154
|
static parsePart(cronPart, id) {
|
|
164
155
|
if (cronPart.includes(",")) {
|
|
165
156
|
const res = [];
|
|
166
|
-
for (const part of cronPart.split(","))
|
|
167
|
-
res.push(..._Cron.parsePart(part, id));
|
|
157
|
+
for (const part of cronPart.split(",")) res.push(..._Cron.parsePart(part, id));
|
|
168
158
|
return [...new Set(res)].sort((a, b) => a - b);
|
|
169
159
|
}
|
|
170
160
|
const [, wild, minStr, maxStr, step] = partRegex.exec(cronPart);
|
|
171
161
|
let [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];
|
|
172
|
-
if (wild)
|
|
173
|
-
|
|
174
|
-
else if (!max && !step)
|
|
175
|
-
return [min];
|
|
162
|
+
if (wild) [min, max] = allowedNum[id];
|
|
163
|
+
else if (!max && !step) return [min];
|
|
176
164
|
[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);
|
|
177
165
|
return range(min, max, parseInt(step, 10) || 1);
|
|
178
166
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/constants.ts","../../../utilities/dist/esm/chunk-G5GHKT7C.mjs","../../../utilities/src/lib/range.ts","../../src/lib/Cron.ts"],"names":["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,CAAC,CAAC;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAe,UAAU,UAAkB,IAAsB;AAChE,QAAI,SAAS,SAAS,GAAG,GAAG;AAC3B,YAAM,MAAgB,CAAC;AACvB,iBAAW,QAAQ,SAAS,MAAM,GAAG;AAAG,YAAI,KAAK,GAAG,MAAK,UAAU,MAAM,EAAE,CAAC;AAC5E,aAAO,CAAC,GAAG,IAAI,IAAI,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAAA,IAC9C;AAEA,UAAM,CAAC,EAAE,MAAM,QAAQ,QAAQ,IAAI,IAAI,UAAU,KAAK,QAAQ;AAC9D,QAAI,CAAC,KAAK,GAAG,IAAI,CAAC,SAAS,QAAQ,EAAE,GAAG,SAAS,QAAQ,EAAE,CAAC;AAG5D,QAAI;AAAM,OAAC,KAAK,GAAG,IAAI,WAAW,EAAE;AAAA,aAG3B,CAAC,OAAO,CAAC;AAAM,aAAO,CAAC,GAAG;AAQnC,KAAC,KAAK,GAAG,IAAI,CAAC,KAAK,OAAO,WAAW,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAGjE,WAAO,MAAM,KAAK,KAAK,SAAS,MAAM,EAAE,KAAK,CAAC;AAAA,EAC/C;AACD;AAvHkB;AAAX,IAAM,OAAN","sourcesContent":["export enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\nexport const partRegex = /^(?:(\\*)|(\\d+)(?:-(\\d+))?)(?:\\/(\\d+))?$/;\n\nexport const wildcardRegex = /\\bh\\b|\\B\\?\\B/g;\n\nexport const allowedNum = [\n\t[0, 59],\n\t[0, 23],\n\t[1, 31],\n\t[1, 12],\n\t[0, 6]\n];\n\nexport const predefined = {\n\t'@annually': '0 0 1 1 *',\n\t'@yearly': '0 0 1 1 *',\n\t'@monthly': '0 0 1 * *',\n\t'@weekly': '0 0 * * 0',\n\t'@daily': '0 0 * * *',\n\t'@hourly': '0 * * * *'\n} as const;\n\nexport const cronTokens = {\n\tjan: 1,\n\tfeb: 2,\n\tmar: 3,\n\tapr: 4,\n\tmay: 5,\n\tjun: 6,\n\tjul: 7,\n\taug: 8,\n\tsep: 9,\n\toct: 10,\n\tnov: 11,\n\tdec: 12,\n\tsun: 0,\n\tmon: 1,\n\ttue: 2,\n\twed: 3,\n\tthu: 4,\n\tfri: 5,\n\tsat: 6\n} as const;\n\nexport const tokensRegex = new RegExp(Object.keys(cronTokens).join('|'), 'g');\n","var __defProp = Object.defineProperty;\nvar __name = (target, value) => __defProp(target, \"name\", { value, configurable: true });\n\nexport { __name };\n//# sourceMappingURL=out.js.map\n//# sourceMappingURL=chunk-G5GHKT7C.mjs.map","/**\n * Get an array of numbers with the selected range\n * @param min The minimum value\n * @param max The maximum value\n * @param step The step value\n */\nexport function range(min: number, max: number, step: number): number[] {\n\treturn new Array(Math.floor((max - min) / step) + 1).fill(0).map((_val, i) => min + i * step);\n}\n","import { range } from '@sapphire/utilities';\nimport { allowedNum, cronTokens, partRegex, predefined, Time, tokensRegex, wildcardRegex } from './constants';\n\n/**\n * Handles Cron strings and generates dates based on the cron string provided.\n * @see https://en.wikipedia.org/wiki/Cron\n */\nexport class Cron {\n\tpublic cron: string;\n\tpublic normalized: string;\n\tpublic minutes: number[];\n\tpublic hours: number[];\n\tpublic days: number[];\n\tpublic months: number[];\n\tpublic dows: number[];\n\n\t/**\n\t * @param cron The cron pattern to use\n\t */\n\tpublic constructor(cron: string) {\n\t\tthis.cron = cron.toLowerCase();\n\t\tthis.normalized = Cron.normalize(this.cron);\n\t\t[this.minutes, this.hours, this.days, this.months, this.dows] = Cron.parseString(this.normalized);\n\t}\n\n\t/**\n\t * Get the next date that matches with the current pattern\n\t * @param outset The Date instance to compare with\n\t * @param origin Whether this next call is origin\n\t */\n\tpublic next(outset: Date = new Date(), origin = true): Date {\n\t\tif (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {\n\t\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t\t}\n\t\tif (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));\n\n\t\tconst now = new Date(outset.getTime() + 60000);\n\n\t\tfor (const hour of this.hours) {\n\t\t\tif (hour < now.getUTCHours()) continue;\n\t\t\tfor (const minute of this.minutes) {\n\t\t\t\tif (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;\n\t\t\t\treturn new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));\n\t\t\t}\n\t\t}\n\n\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t}\n\n\t/**\n\t * Normalize the pattern\n\t * @param cron The pattern to normalize\n\t */\n\tprivate static normalize(cron: string): string {\n\t\tif (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);\n\t\tconst now = new Date();\n\t\tcron = cron\n\t\t\t.split(' ')\n\t\t\t.map((val, i) =>\n\t\t\t\tval.replace(wildcardRegex, (match) => {\n\t\t\t\t\tif (match === 'h') return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();\n\n\t\t\t\t\tif (match === '?') {\n\t\t\t\t\t\tswitch (i) {\n\t\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\t\treturn now.getUTCMinutes().toString();\n\t\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\t\treturn now.getUTCHours().toString();\n\t\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\t\treturn now.getUTCDate().toString();\n\t\t\t\t\t\t\tcase 3:\n\t\t\t\t\t\t\t\treturn now.getUTCMonth().toString();\n\t\t\t\t\t\t\tcase 4:\n\t\t\t\t\t\t\t\treturn now.getUTCDay().toString();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn match;\n\t\t\t\t})\n\t\t\t)\n\t\t\t.join(' ');\n\t\treturn cron.replace(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));\n\t}\n\n\t/**\n\t * Parse the pattern\n\t * @param cron The pattern to parse\n\t */\n\tprivate static parseString(cron: string): Array<number[]> {\n\t\tconst parts = cron.split(' ');\n\t\tif (parts.length !== 5) throw new Error('Invalid Cron Provided');\n\t\treturn parts.map((part, i) => Cron.parsePart(part, i));\n\t}\n\n\t/**\n\t * Parse the current part\n\t * @param cronPart The part of the pattern to parse\n\t * @param id The id that identifies the current part\n\t */\n\tprivate static parsePart(cronPart: string, id: number): number[] {\n\t\tif (cronPart.includes(',')) {\n\t\t\tconst res: number[] = [];\n\t\t\tfor (const part of cronPart.split(',')) res.push(...Cron.parsePart(part, id));\n\t\t\treturn [...new Set(res)].sort((a, b) => a - b);\n\t\t}\n\n\t\tconst [, wild, minStr, maxStr, step] = partRegex.exec(cronPart)!;\n\t\tlet [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];\n\n\t\t// If '*', set min and max as the minimum and maximum allowed numbers:\n\t\tif (wild) [min, max] = allowedNum[id];\n\t\t// Else if a number was given, but not a maximum nor a step, return it\n\t\t// as only allowed value:\n\t\telse if (!max && !step) return [min];\n\n\t\t// Set min and max as the given numbers, defaulting max to the maximum\n\t\t// allowed, so min is never bigger than max:\n\t\t// This makes min and max be, in the following cases (considering minutes):\n\t\t// -> 1-2 | 1..2\n\t\t// -> 2-1 | 1..2\n\t\t// -> 1/7 | 1, 8, 15, 22, 29, 36, 43, 50, 57\n\t\t[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);\n\n\t\t// Generate a range\n\t\treturn range(min, max, parseInt(step, 10) || 1);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/lib/constants.ts","../../../utilities/dist/esm/chunk-PAWJFY3S.mjs","../../../utilities/src/lib/range.ts","../../src/lib/Cron.ts"],"names":["Time","__defProp","__name"],"mappings":";;;;;;AAAO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,gBAAa,QAAb;AACA,EAAAA,YAAA,iBAAc,QAAd;AACA,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,UAAO,UAAP;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAVW,SAAAA;AAAA,GAAA;AAaL,IAAM,YAAY;AAElB,IAAM,gBAAgB;AAEtB,IAAM,aAAa;AAAA,EACzB,CAAC,GAAG,EAAE;AAAA,EACN,CAAC,GAAG,EAAE;AAAA,EACN,CAAC,GAAG,EAAE;AAAA,EACN,CAAC,GAAG,EAAE;AAAA,EACN,CAAC,GAAG,CAAC;AACN;AAEO,IAAM,aAAa;AAAA,EACzB,aAAa;AAAA,EACb,WAAW;AAAA,EACX,YAAY;AAAA,EACZ,WAAW;AAAA,EACX,UAAU;AAAA,EACV,WAAW;AACZ;AAEO,IAAM,aAAa;AAAA,EACzB,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEO,IAAM,cAAc,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,KAAK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AAW,EAAG,OAAM,IAAI,MAAM,uBAAuB;AAC/D,WAAO,MAAM,IAAI,CAAC,MAAM,MAAM,MAAK,UAAU,MAAM,CAAC,CAAC;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAe,UAAU,UAAkB,IAAsB;AAChE,QAAI,SAAS,SAAS,GAAG,GAAG;AAC3B,YAAM,MAAgB,CAAC;AACvB,iBAAW,QAAQ,SAAS,MAAM,GAAG,EAAG,KAAI,KAAK,GAAG,MAAK,UAAU,MAAM,EAAE,CAAC;AAC5E,aAAO,CAAC,GAAG,IAAI,IAAI,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAAA,IAC9C;AAEA,UAAM,CAAC,EAAE,MAAM,QAAQ,QAAQ,IAAI,IAAI,UAAU,KAAK,QAAQ;AAC9D,QAAI,CAAC,KAAK,GAAG,IAAI,CAAC,SAAS,QAAQ,EAAE,GAAG,SAAS,QAAQ,EAAE,CAAC;AAG5D,QAAI,KAAM,EAAC,KAAK,GAAG,IAAI,WAAW,EAAE;AAAA,aAG3B,CAAC,OAAO,CAAC,KAAM,QAAO,CAAC,GAAG;AAQnC,KAAC,KAAK,GAAG,IAAI,CAAC,KAAK,OAAO,WAAW,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC;AAGjE,WAAO,MAAM,KAAK,KAAK,SAAS,MAAM,EAAE,KAAK,CAAC;AAAA,EAC/C;AACD;AAvHkB;AAAX,IAAM,OAAN","sourcesContent":["export enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\nexport const partRegex = /^(?:(\\*)|(\\d+)(?:-(\\d+))?)(?:\\/(\\d+))?$/;\n\nexport const wildcardRegex = /\\bh\\b|\\B\\?\\B/g;\n\nexport const allowedNum = [\n\t[0, 59],\n\t[0, 23],\n\t[1, 31],\n\t[1, 12],\n\t[0, 6]\n];\n\nexport const predefined = {\n\t'@annually': '0 0 1 1 *',\n\t'@yearly': '0 0 1 1 *',\n\t'@monthly': '0 0 1 * *',\n\t'@weekly': '0 0 * * 0',\n\t'@daily': '0 0 * * *',\n\t'@hourly': '0 * * * *'\n} as const;\n\nexport const cronTokens = {\n\tjan: 1,\n\tfeb: 2,\n\tmar: 3,\n\tapr: 4,\n\tmay: 5,\n\tjun: 6,\n\tjul: 7,\n\taug: 8,\n\tsep: 9,\n\toct: 10,\n\tnov: 11,\n\tdec: 12,\n\tsun: 0,\n\tmon: 1,\n\ttue: 2,\n\twed: 3,\n\tthu: 4,\n\tfri: 5,\n\tsat: 6\n} as const;\n\nexport const tokensRegex = new RegExp(Object.keys(cronTokens).join('|'), 'g');\n","var __defProp = Object.defineProperty;\nvar __name = (target, value) => __defProp(target, \"name\", { value, configurable: true });\n\nexport { __name };\n//# sourceMappingURL=out.js.map\n//# sourceMappingURL=chunk-PAWJFY3S.mjs.map","/**\n * Get an array of numbers with the selected range\n * @param min The minimum value\n * @param max The maximum value\n * @param step The step value\n */\nexport function range(min: number, max: number, step: number): number[] {\n\treturn new Array(Math.floor((max - min) / step) + 1).fill(0).map((_val, i) => min + i * step);\n}\n","import { range } from '@sapphire/utilities';\nimport { allowedNum, cronTokens, partRegex, predefined, Time, tokensRegex, wildcardRegex } from './constants';\n\n/**\n * Handles Cron strings and generates dates based on the cron string provided.\n * @see https://en.wikipedia.org/wiki/Cron\n */\nexport class Cron {\n\tpublic cron: string;\n\tpublic normalized: string;\n\tpublic minutes: number[];\n\tpublic hours: number[];\n\tpublic days: number[];\n\tpublic months: number[];\n\tpublic dows: number[];\n\n\t/**\n\t * @param cron The cron pattern to use\n\t */\n\tpublic constructor(cron: string) {\n\t\tthis.cron = cron.toLowerCase();\n\t\tthis.normalized = Cron.normalize(this.cron);\n\t\t[this.minutes, this.hours, this.days, this.months, this.dows] = Cron.parseString(this.normalized);\n\t}\n\n\t/**\n\t * Get the next date that matches with the current pattern\n\t * @param outset The Date instance to compare with\n\t * @param origin Whether this next call is origin\n\t */\n\tpublic next(outset: Date = new Date(), origin = true): Date {\n\t\tif (!this.days.includes(outset.getUTCDate()) || !this.months.includes(outset.getUTCMonth() + 1) || !this.dows.includes(outset.getUTCDay())) {\n\t\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t\t}\n\t\tif (!origin) return new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), this.hours[0], this.minutes[0]));\n\n\t\tconst now = new Date(outset.getTime() + 60000);\n\n\t\tfor (const hour of this.hours) {\n\t\t\tif (hour < now.getUTCHours()) continue;\n\t\t\tfor (const minute of this.minutes) {\n\t\t\t\tif (hour === now.getUTCHours() && minute < now.getUTCMinutes()) continue;\n\t\t\t\treturn new Date(Date.UTC(outset.getUTCFullYear(), outset.getUTCMonth(), outset.getUTCDate(), hour, minute));\n\t\t\t}\n\t\t}\n\n\t\treturn this.next(new Date(outset.getTime() + Time.Day), false);\n\t}\n\n\t/**\n\t * Normalize the pattern\n\t * @param cron The pattern to normalize\n\t */\n\tprivate static normalize(cron: string): string {\n\t\tif (Reflect.has(predefined, cron)) return Reflect.get(predefined, cron);\n\t\tconst now = new Date();\n\t\tcron = cron\n\t\t\t.split(' ')\n\t\t\t.map((val, i) =>\n\t\t\t\tval.replace(wildcardRegex, (match) => {\n\t\t\t\t\tif (match === 'h') return (Math.floor(Math.random() * allowedNum[i][1]) + allowedNum[i][0]).toString();\n\n\t\t\t\t\tif (match === '?') {\n\t\t\t\t\t\tswitch (i) {\n\t\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\t\treturn now.getUTCMinutes().toString();\n\t\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\t\treturn now.getUTCHours().toString();\n\t\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\t\treturn now.getUTCDate().toString();\n\t\t\t\t\t\t\tcase 3:\n\t\t\t\t\t\t\t\treturn now.getUTCMonth().toString();\n\t\t\t\t\t\t\tcase 4:\n\t\t\t\t\t\t\t\treturn now.getUTCDay().toString();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn match;\n\t\t\t\t})\n\t\t\t)\n\t\t\t.join(' ');\n\t\treturn cron.replace(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));\n\t}\n\n\t/**\n\t * Parse the pattern\n\t * @param cron The pattern to parse\n\t */\n\tprivate static parseString(cron: string): Array<number[]> {\n\t\tconst parts = cron.split(' ');\n\t\tif (parts.length !== 5) throw new Error('Invalid Cron Provided');\n\t\treturn parts.map((part, i) => Cron.parsePart(part, i));\n\t}\n\n\t/**\n\t * Parse the current part\n\t * @param cronPart The part of the pattern to parse\n\t * @param id The id that identifies the current part\n\t */\n\tprivate static parsePart(cronPart: string, id: number): number[] {\n\t\tif (cronPart.includes(',')) {\n\t\t\tconst res: number[] = [];\n\t\t\tfor (const part of cronPart.split(',')) res.push(...Cron.parsePart(part, id));\n\t\t\treturn [...new Set(res)].sort((a, b) => a - b);\n\t\t}\n\n\t\tconst [, wild, minStr, maxStr, step] = partRegex.exec(cronPart)!;\n\t\tlet [min, max] = [parseInt(minStr, 10), parseInt(maxStr, 10)];\n\n\t\t// If '*', set min and max as the minimum and maximum allowed numbers:\n\t\tif (wild) [min, max] = allowedNum[id];\n\t\t// Else if a number was given, but not a maximum nor a step, return it\n\t\t// as only allowed value:\n\t\telse if (!max && !step) return [min];\n\n\t\t// Set min and max as the given numbers, defaulting max to the maximum\n\t\t// allowed, so min is never bigger than max:\n\t\t// This makes min and max be, in the following cases (considering minutes):\n\t\t// -> 1-2 | 1..2\n\t\t// -> 2-1 | 1..2\n\t\t// -> 1/7 | 1, 8, 15, 22, 29, 36, 43, 50, 57\n\t\t[min, max] = [min, max || allowedNum[id][1]].sort((a, b) => a - b);\n\n\t\t// Generate a range\n\t\treturn range(min, max, parseInt(step, 10) || 1);\n\t}\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/cron",
|
|
3
|
-
"version": "1.1.4-next.
|
|
3
|
+
"version": "1.1.4-next.ec73e32c",
|
|
4
4
|
"description": "A cron utility library for JavaScript.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,12 +25,13 @@
|
|
|
25
25
|
"scripts": {
|
|
26
26
|
"test": "vitest run",
|
|
27
27
|
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
|
|
28
|
-
"build": "tsup && yarn build:rename-cjs-index",
|
|
29
|
-
"build:rename-cjs-index": "tsx
|
|
28
|
+
"build": "yarn gen-index && tsup && yarn build:rename-cjs-index",
|
|
29
|
+
"build:rename-cjs-index": "tsx ../../scripts/rename-cjs-index.cts",
|
|
30
30
|
"docs": "typedoc-json-parser",
|
|
31
31
|
"prepack": "yarn build",
|
|
32
32
|
"bump": "cliff-jumper",
|
|
33
|
-
"check-update": "cliff-jumper --dry-run"
|
|
33
|
+
"check-update": "cliff-jumper --dry-run",
|
|
34
|
+
"gen-index": "tsx ../../scripts/gen-index.cts -w cron"
|
|
34
35
|
},
|
|
35
36
|
"repository": {
|
|
36
37
|
"type": "git",
|
|
@@ -65,13 +66,13 @@
|
|
|
65
66
|
"@sapphire/utilities": "^3.16.2"
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
68
|
-
"@favware/cliff-jumper": "^
|
|
69
|
+
"@favware/cliff-jumper": "^4.0.2",
|
|
69
70
|
"@vitest/coverage-v8": "^1.6.0",
|
|
70
|
-
"tsup": "^8.0
|
|
71
|
-
"tsx": "^4.
|
|
71
|
+
"tsup": "^8.1.0",
|
|
72
|
+
"tsx": "^4.15.7",
|
|
72
73
|
"typedoc": "^0.25.13",
|
|
73
74
|
"typedoc-json-parser": "^10.0.0",
|
|
74
|
-
"typescript": "
|
|
75
|
+
"typescript": "~5.4.5",
|
|
75
76
|
"vitest": "^1.6.0"
|
|
76
77
|
}
|
|
77
78
|
}
|