@vope/utils 1.6.1 → 1.6.2
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/cron.d.ts +177 -0
- package/dist/cron.js +674 -0
- package/dist/index.cjs.js +685 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.js +29 -1
- package/package.json +1 -1
package/dist/cron.js
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import { isNumber, isString } from "lodash-es";
|
|
2
|
+
import { hash8json } from "./convert/json";
|
|
3
|
+
const {
|
|
4
|
+
parseInt,
|
|
5
|
+
isInteger,
|
|
6
|
+
MAX_SAFE_INTEGER
|
|
7
|
+
} = Number;
|
|
8
|
+
class Dic {
|
|
9
|
+
constructor(dics) {
|
|
10
|
+
this.dics = dics;
|
|
11
|
+
this.keys = hash8json(dics, "key");
|
|
12
|
+
this.values = hash8json(dics, "value");
|
|
13
|
+
}
|
|
14
|
+
keys;
|
|
15
|
+
values;
|
|
16
|
+
item(key) {
|
|
17
|
+
return this.keys[key]?.value;
|
|
18
|
+
}
|
|
19
|
+
at(value) {
|
|
20
|
+
return this.values[value]?.key;
|
|
21
|
+
}
|
|
22
|
+
reduce(callbackfn, initialValue) {
|
|
23
|
+
return this.dics.reduce(callbackfn, initialValue);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const weekDic = new Dic([
|
|
27
|
+
{
|
|
28
|
+
key: "SUN",
|
|
29
|
+
value: 0
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
key: "MON",
|
|
33
|
+
value: 1
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
key: "TUE",
|
|
37
|
+
value: 2
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
key: "WED",
|
|
41
|
+
value: 3
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
key: "THU",
|
|
45
|
+
value: 4
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
key: "FRI",
|
|
49
|
+
value: 5
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
key: "SAT",
|
|
53
|
+
value: 6
|
|
54
|
+
}
|
|
55
|
+
]);
|
|
56
|
+
const place_value = 0;
|
|
57
|
+
const default_max_second = 59;
|
|
58
|
+
const default_min_second = 0;
|
|
59
|
+
const default_max_minute = default_max_second;
|
|
60
|
+
const default_min_minute = default_min_second;
|
|
61
|
+
const default_max_hour = 23;
|
|
62
|
+
const default_min_hour = 0;
|
|
63
|
+
const default_max_day = 31;
|
|
64
|
+
const default_min_day = 1;
|
|
65
|
+
const default_max_month = 12;
|
|
66
|
+
const default_min_month = 1;
|
|
67
|
+
const default_max_week = 6;
|
|
68
|
+
const default_min_week = 0;
|
|
69
|
+
const re_single_number_text = "\\d";
|
|
70
|
+
const createCronRegexText = (options = {}) => {
|
|
71
|
+
const {
|
|
72
|
+
enableSlot = true,
|
|
73
|
+
enableSecond = true,
|
|
74
|
+
enableYear = true,
|
|
75
|
+
enableMax = true,
|
|
76
|
+
enableInvalid = true,
|
|
77
|
+
enableDayLast = true,
|
|
78
|
+
enableWeekLast = true,
|
|
79
|
+
enableWeekSupport7 = true,
|
|
80
|
+
enableWeekSupportDic = true,
|
|
81
|
+
enableDicEnumSupportMixin = true,
|
|
82
|
+
enablePrefixZero
|
|
83
|
+
} = options;
|
|
84
|
+
const enableMaxSplit = enableMax && (options.enableIncrementMax ?? true);
|
|
85
|
+
const singleNumberMatch = (end, fromZero) => {
|
|
86
|
+
const start = fromZero ? 0 : 1;
|
|
87
|
+
if (end === start) {
|
|
88
|
+
return `${end}`;
|
|
89
|
+
} else if (end < 3 + start) {
|
|
90
|
+
let text = "";
|
|
91
|
+
for (let i = start; i <= end; i++) {
|
|
92
|
+
text += i;
|
|
93
|
+
}
|
|
94
|
+
return `[${text}]`;
|
|
95
|
+
} else if (start === 0 && end === 9) {
|
|
96
|
+
return re_single_number_text;
|
|
97
|
+
} else {
|
|
98
|
+
return `[${start}-${end}]`;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const numberMatch = (max, fromZero) => {
|
|
102
|
+
if (max === place_value) return `[1-9]${re_single_number_text}*`;
|
|
103
|
+
if (max < 10) return singleNumberMatch(max, fromZero);
|
|
104
|
+
const mod = max % 10;
|
|
105
|
+
const quot = (max - mod) / 10;
|
|
106
|
+
let text = "";
|
|
107
|
+
if (fromZero) {
|
|
108
|
+
text = re_single_number_text;
|
|
109
|
+
} else {
|
|
110
|
+
text = singleNumberMatch(9);
|
|
111
|
+
}
|
|
112
|
+
text += "|";
|
|
113
|
+
if (mod === 9) {
|
|
114
|
+
text += singleNumberMatch(quot) + re_single_number_text;
|
|
115
|
+
} else {
|
|
116
|
+
if (quot > 1) {
|
|
117
|
+
text += singleNumberMatch(quot - 1) + re_single_number_text + "|";
|
|
118
|
+
}
|
|
119
|
+
text += `${quot}${singleNumberMatch(mod, true)}`;
|
|
120
|
+
}
|
|
121
|
+
return `(?:${text})`;
|
|
122
|
+
};
|
|
123
|
+
const numberMatchByPrefix = (max, fromZero) => {
|
|
124
|
+
const text = numberMatch(max, fromZero);
|
|
125
|
+
return enablePrefixZero ? "0*" + text : text;
|
|
126
|
+
};
|
|
127
|
+
const mixin = (text) => {
|
|
128
|
+
let tmp = "";
|
|
129
|
+
for (const char of text) {
|
|
130
|
+
const code = char.charCodeAt(0);
|
|
131
|
+
if (code > 64 && code < 91) {
|
|
132
|
+
tmp += `[${char.toLocaleLowerCase()}${char}]`;
|
|
133
|
+
} else if (code > 96 && code < 123) {
|
|
134
|
+
tmp += `[${char}${char.toLocaleUpperCase()}]`;
|
|
135
|
+
} else {
|
|
136
|
+
tmp += char;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return tmp;
|
|
140
|
+
};
|
|
141
|
+
const generate = ({
|
|
142
|
+
type,
|
|
143
|
+
max,
|
|
144
|
+
split,
|
|
145
|
+
dic,
|
|
146
|
+
invalid,
|
|
147
|
+
last,
|
|
148
|
+
fromZero
|
|
149
|
+
}) => {
|
|
150
|
+
const list = [];
|
|
151
|
+
invalid && list.push("\\?");
|
|
152
|
+
const template = [numberMatchByPrefix(max, fromZero)];
|
|
153
|
+
let lastTemplate;
|
|
154
|
+
if (last) {
|
|
155
|
+
if (type === "D") {
|
|
156
|
+
lastTemplate = `L(?:-${numberMatchByPrefix(max - 1)})?`;
|
|
157
|
+
} else if (type === "w") {
|
|
158
|
+
lastTemplate = `${numberMatchByPrefix(max, true)}L`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (type === "w" && dic) {
|
|
162
|
+
template.push(
|
|
163
|
+
dic.reduce((tmp2, { key }) => {
|
|
164
|
+
const text = enableDicEnumSupportMixin ? mixin(key) : key;
|
|
165
|
+
return tmp2 ? tmp2 + "|" + text : text;
|
|
166
|
+
}, "")
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
let tmp = template.length > 1 ? `(?:${template.join("|")})` : template[0];
|
|
170
|
+
if (lastTemplate) {
|
|
171
|
+
tmp = `(?:\\*|(?:${lastTemplate}|${tmp}(?:-${tmp})?)(?:,(?:${lastTemplate}|${tmp}(?:-${tmp})?))*)`;
|
|
172
|
+
} else {
|
|
173
|
+
tmp = `(?:\\*|${tmp}(?:-${tmp})?(?:,${tmp}(?:-${tmp})?)*)`;
|
|
174
|
+
}
|
|
175
|
+
list.push(`${tmp}(?:\\/${numberMatchByPrefix(split)})?`);
|
|
176
|
+
return list.join("|");
|
|
177
|
+
};
|
|
178
|
+
let regex = "";
|
|
179
|
+
const join = (config, isLast) => {
|
|
180
|
+
const space = "\\s+";
|
|
181
|
+
let code = generate(config);
|
|
182
|
+
if (enableSlot) {
|
|
183
|
+
code = `(?<${config.type}>${code})`;
|
|
184
|
+
} else {
|
|
185
|
+
code = `(${code})`;
|
|
186
|
+
}
|
|
187
|
+
if (!isLast) {
|
|
188
|
+
code += space;
|
|
189
|
+
}
|
|
190
|
+
if (config.partial) {
|
|
191
|
+
if (isLast) {
|
|
192
|
+
regex = regex.slice(0, space.length * -1) + `(?:${space}${code})?`;
|
|
193
|
+
return;
|
|
194
|
+
} else {
|
|
195
|
+
code = `(?:${code})?`;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
regex += code;
|
|
199
|
+
};
|
|
200
|
+
enableSecond && join({
|
|
201
|
+
type: "s",
|
|
202
|
+
max: enableMax ? default_max_second : place_value,
|
|
203
|
+
split: enableMaxSplit ? default_max_second : place_value,
|
|
204
|
+
fromZero: !default_min_second
|
|
205
|
+
});
|
|
206
|
+
join({
|
|
207
|
+
type: "m",
|
|
208
|
+
max: enableMax ? default_max_minute : place_value,
|
|
209
|
+
split: enableMaxSplit ? default_max_minute : place_value,
|
|
210
|
+
fromZero: !default_min_minute
|
|
211
|
+
});
|
|
212
|
+
join({
|
|
213
|
+
type: "h",
|
|
214
|
+
max: enableMax ? default_max_hour : place_value,
|
|
215
|
+
split: enableMaxSplit ? default_max_hour : place_value,
|
|
216
|
+
fromZero: !default_min_hour
|
|
217
|
+
});
|
|
218
|
+
join({
|
|
219
|
+
type: "D",
|
|
220
|
+
max: enableMax ? default_max_day : place_value,
|
|
221
|
+
split: enableMaxSplit ? default_max_day : place_value,
|
|
222
|
+
invalid: enableInvalid,
|
|
223
|
+
last: enableDayLast,
|
|
224
|
+
fromZero: !default_min_day
|
|
225
|
+
});
|
|
226
|
+
join({
|
|
227
|
+
type: "M",
|
|
228
|
+
max: enableMax ? default_max_month : place_value,
|
|
229
|
+
split: enableMaxSplit ? default_max_month : place_value,
|
|
230
|
+
fromZero: !default_min_month
|
|
231
|
+
});
|
|
232
|
+
join((() => {
|
|
233
|
+
const max = enableWeekSupport7 ? 7 : default_max_week;
|
|
234
|
+
const config = {
|
|
235
|
+
type: "w",
|
|
236
|
+
max: enableMax ? max : place_value,
|
|
237
|
+
split: enableMaxSplit ? 7 : place_value,
|
|
238
|
+
fromZero: !default_min_week,
|
|
239
|
+
invalid: enableInvalid,
|
|
240
|
+
last: enableWeekLast
|
|
241
|
+
};
|
|
242
|
+
if (enableWeekSupportDic) {
|
|
243
|
+
config.dic = weekDic;
|
|
244
|
+
}
|
|
245
|
+
return config;
|
|
246
|
+
})(), !enableYear);
|
|
247
|
+
enableYear && join({
|
|
248
|
+
type: "Y",
|
|
249
|
+
max: place_value,
|
|
250
|
+
split: place_value,
|
|
251
|
+
fromZero: true,
|
|
252
|
+
partial: true
|
|
253
|
+
}, true);
|
|
254
|
+
return {
|
|
255
|
+
regex,
|
|
256
|
+
enableSlot,
|
|
257
|
+
enableSecond,
|
|
258
|
+
enableYear,
|
|
259
|
+
enableDayLast,
|
|
260
|
+
enableWeekLast,
|
|
261
|
+
enableInvalid
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
class InvalidValue {
|
|
265
|
+
constructor(isEnableInvalid) {
|
|
266
|
+
this.isEnableInvalid = isEnableInvalid;
|
|
267
|
+
}
|
|
268
|
+
text() {
|
|
269
|
+
return this.isEnableInvalid ? "?" : "*";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const isInvalidValue = (value) => value instanceof InvalidValue;
|
|
273
|
+
class LastValue {
|
|
274
|
+
constructor(value) {
|
|
275
|
+
this.value = value;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
class DayLastValue extends LastValue {
|
|
279
|
+
text() {
|
|
280
|
+
const { value } = this;
|
|
281
|
+
return "L" + (value ? `-${value}` : "");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const isDayLastValue = (value) => value instanceof DayLastValue;
|
|
285
|
+
class WeekLastValue extends LastValue {
|
|
286
|
+
text() {
|
|
287
|
+
return `${this.value}L`;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const isWeekLastValue = (value) => value instanceof WeekLastValue;
|
|
291
|
+
class WildcardValue {
|
|
292
|
+
text() {
|
|
293
|
+
return "*";
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const isWildcardValue = (value) => value instanceof WildcardValue;
|
|
297
|
+
class RangeValue {
|
|
298
|
+
constructor(start, stop) {
|
|
299
|
+
this.start = start;
|
|
300
|
+
this.stop = stop;
|
|
301
|
+
}
|
|
302
|
+
text() {
|
|
303
|
+
return `${this.start}-${this.stop}`;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const isRangeValue = (value) => value instanceof RangeValue;
|
|
307
|
+
class DigitsValue {
|
|
308
|
+
constructor(values, increment) {
|
|
309
|
+
this.values = values;
|
|
310
|
+
this.increment = increment;
|
|
311
|
+
}
|
|
312
|
+
text() {
|
|
313
|
+
const { values, increment } = this;
|
|
314
|
+
let tmp;
|
|
315
|
+
if (Array.isArray(values)) {
|
|
316
|
+
tmp = values.reduce((caches, data) => {
|
|
317
|
+
let tmp2;
|
|
318
|
+
if (isNumber(data)) {
|
|
319
|
+
tmp2 = `${data}`;
|
|
320
|
+
} else {
|
|
321
|
+
tmp2 = data.text();
|
|
322
|
+
}
|
|
323
|
+
return caches ? caches + "," + tmp2 : tmp2;
|
|
324
|
+
}, "");
|
|
325
|
+
} else {
|
|
326
|
+
tmp = values.text();
|
|
327
|
+
}
|
|
328
|
+
return increment ? `${tmp}/${increment}` : tmp;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const isDigitsValue = (value) => value instanceof DigitsValue;
|
|
332
|
+
const re_num = /\d/;
|
|
333
|
+
const parseDigit = (text) => {
|
|
334
|
+
if (re_num.test(text[0])) {
|
|
335
|
+
return parseInt(text);
|
|
336
|
+
} else {
|
|
337
|
+
return weekDic.item(text.toLocaleUpperCase());
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
const parseDigits = (text) => {
|
|
341
|
+
return text.split(",").map((text2) => {
|
|
342
|
+
const [a, b] = text2.split("-");
|
|
343
|
+
if (a === "L" && b === void 0) {
|
|
344
|
+
return new DayLastValue(0);
|
|
345
|
+
} else if (b === void 0) {
|
|
346
|
+
if (a.slice(-1) === "L") {
|
|
347
|
+
return new WeekLastValue(parseInt(a.slice(0, -1)));
|
|
348
|
+
} else {
|
|
349
|
+
return parseDigit(a);
|
|
350
|
+
}
|
|
351
|
+
} else {
|
|
352
|
+
if (a === "L") {
|
|
353
|
+
return new DayLastValue(parseInt(b));
|
|
354
|
+
} else {
|
|
355
|
+
return new RangeValue(parseDigit(a), parseDigit(b));
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
};
|
|
360
|
+
const validate = (value, min, max) => {
|
|
361
|
+
if (isInteger(value)) {
|
|
362
|
+
return value >= min && value <= max;
|
|
363
|
+
} else if (Array.isArray(value)) {
|
|
364
|
+
if (value.length !== 2) return false;
|
|
365
|
+
const [a, b] = value;
|
|
366
|
+
return isInteger(a) && isInteger(b) && a <= b && a >= min && b <= max;
|
|
367
|
+
}
|
|
368
|
+
return false;
|
|
369
|
+
};
|
|
370
|
+
const validateIncrement = (value, min, max) => {
|
|
371
|
+
return isInteger(value) && value > 0 && value >= min && value <= max;
|
|
372
|
+
};
|
|
373
|
+
const formaterDigits = (value, min, max) => {
|
|
374
|
+
let values;
|
|
375
|
+
if (value === "*") {
|
|
376
|
+
values = new WildcardValue();
|
|
377
|
+
} else if (Array.isArray(value)) {
|
|
378
|
+
const tmp = [];
|
|
379
|
+
value.some((data) => {
|
|
380
|
+
if (validate(data, min, max)) {
|
|
381
|
+
tmp.push(
|
|
382
|
+
isNumber(data) ? data : new RangeValue(data[0], data[1])
|
|
383
|
+
);
|
|
384
|
+
return false;
|
|
385
|
+
} else {
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
}) || (values = tmp);
|
|
389
|
+
} else if (validate(value, min, max)) {
|
|
390
|
+
values = [value];
|
|
391
|
+
}
|
|
392
|
+
return values;
|
|
393
|
+
};
|
|
394
|
+
const formaterLastDigits = (value, min, max, prototype) => {
|
|
395
|
+
let values;
|
|
396
|
+
if (value === "*") {
|
|
397
|
+
values = new WildcardValue();
|
|
398
|
+
} else {
|
|
399
|
+
const tmp = [];
|
|
400
|
+
const list = Array.isArray(value) ? value : [value];
|
|
401
|
+
list.some((data) => {
|
|
402
|
+
if (isString(data)) {
|
|
403
|
+
const result = parseDigits(data);
|
|
404
|
+
if (result.length === 1 && result[0] instanceof prototype && validate(result[0].value, min, max - 1)) {
|
|
405
|
+
tmp.push(result[0]);
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
} else if (validate(data, min, max)) {
|
|
409
|
+
tmp.push(
|
|
410
|
+
isNumber(data) ? data : new RangeValue(data[0], data[1])
|
|
411
|
+
);
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
return true;
|
|
415
|
+
}) || (values = tmp);
|
|
416
|
+
}
|
|
417
|
+
return values;
|
|
418
|
+
};
|
|
419
|
+
class Parse {
|
|
420
|
+
constructor(second, minute, hour, day, month, week, year, cron) {
|
|
421
|
+
this.cron = cron;
|
|
422
|
+
if (second) {
|
|
423
|
+
this.second = this.parse(second);
|
|
424
|
+
}
|
|
425
|
+
this.minute = this.parse(minute);
|
|
426
|
+
this.hour = this.parse(hour);
|
|
427
|
+
this.day = this.parse(day);
|
|
428
|
+
this.month = this.parse(month);
|
|
429
|
+
this.week = this.parse(week);
|
|
430
|
+
if (year) {
|
|
431
|
+
this.year = this.parse(year);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
second;
|
|
435
|
+
minute;
|
|
436
|
+
hour;
|
|
437
|
+
day;
|
|
438
|
+
month;
|
|
439
|
+
week;
|
|
440
|
+
year;
|
|
441
|
+
parse(text) {
|
|
442
|
+
if (text === "?") return new InvalidValue(this.cron.isEnableInvalid);
|
|
443
|
+
const [slices, increment] = text.split("/");
|
|
444
|
+
return new DigitsValue(
|
|
445
|
+
slices === "*" ? new WildcardValue() : parseDigits(slices),
|
|
446
|
+
increment ? parseInt(increment) : void 0
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
setSecond(value, increment) {
|
|
450
|
+
if (!this.cron.isEnableSecond) return false;
|
|
451
|
+
const values = formaterDigits(value, default_min_second, default_max_second);
|
|
452
|
+
if (!values) return false;
|
|
453
|
+
const second = new DigitsValue(values);
|
|
454
|
+
if (validateIncrement(increment, default_min_second, default_max_second)) {
|
|
455
|
+
second.increment = increment;
|
|
456
|
+
}
|
|
457
|
+
this.second = second;
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
setMinute(value, increment) {
|
|
461
|
+
const values = formaterDigits(value, default_min_minute, default_max_minute);
|
|
462
|
+
if (!values) return false;
|
|
463
|
+
const minute = new DigitsValue(values);
|
|
464
|
+
if (validateIncrement(increment, default_min_minute, default_max_minute)) {
|
|
465
|
+
minute.increment = increment;
|
|
466
|
+
}
|
|
467
|
+
this.minute = minute;
|
|
468
|
+
return true;
|
|
469
|
+
}
|
|
470
|
+
setHour(value, increment) {
|
|
471
|
+
const values = formaterDigits(value, default_min_hour, default_max_hour);
|
|
472
|
+
if (!values) return false;
|
|
473
|
+
const hour = new DigitsValue(values);
|
|
474
|
+
if (validateIncrement(increment, default_min_hour, default_max_hour)) {
|
|
475
|
+
hour.increment = increment;
|
|
476
|
+
}
|
|
477
|
+
this.hour = hour;
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
setDayInvalid() {
|
|
481
|
+
this.day = new InvalidValue(this.cron.isEnableInvalid);
|
|
482
|
+
if (this.week instanceof InvalidValue) {
|
|
483
|
+
this.week = new DigitsValue(new WildcardValue());
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
setDay(value, increment) {
|
|
487
|
+
let values;
|
|
488
|
+
if (value === "?") {
|
|
489
|
+
this.setDayInvalid();
|
|
490
|
+
return true;
|
|
491
|
+
} else {
|
|
492
|
+
const tmp2 = formaterLastDigits(value, default_min_day, default_max_day, DayLastValue);
|
|
493
|
+
if (!tmp2) return false;
|
|
494
|
+
values = tmp2;
|
|
495
|
+
}
|
|
496
|
+
const tmp = new DigitsValue(values);
|
|
497
|
+
if (validateIncrement(increment, default_min_day, default_max_day)) {
|
|
498
|
+
tmp.increment = increment;
|
|
499
|
+
}
|
|
500
|
+
this.day = tmp;
|
|
501
|
+
const { week } = this;
|
|
502
|
+
if (isDigitsValue(week) && (week.increment !== void 0 || !isWildcardValue(week.values))) {
|
|
503
|
+
if (this.cron.isEnableInvalid) {
|
|
504
|
+
this.week = new InvalidValue(true);
|
|
505
|
+
} else {
|
|
506
|
+
week.values = new WildcardValue();
|
|
507
|
+
week.increment = void 0;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
setMonth(value, increment) {
|
|
513
|
+
const values = formaterDigits(value, default_min_month, default_max_month);
|
|
514
|
+
if (!values) return false;
|
|
515
|
+
const month = new DigitsValue(values);
|
|
516
|
+
if (validateIncrement(increment, default_min_month, default_max_month)) {
|
|
517
|
+
month.increment = increment;
|
|
518
|
+
}
|
|
519
|
+
this.month = month;
|
|
520
|
+
return true;
|
|
521
|
+
}
|
|
522
|
+
setWeekInvalid() {
|
|
523
|
+
this.week = new InvalidValue(this.cron.isEnableInvalid);
|
|
524
|
+
if (this.day instanceof InvalidValue) {
|
|
525
|
+
this.day = new DigitsValue(new WildcardValue());
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
setWeek(value, increment) {
|
|
529
|
+
let values;
|
|
530
|
+
if (value === "?") {
|
|
531
|
+
this.setWeekInvalid();
|
|
532
|
+
return true;
|
|
533
|
+
} else {
|
|
534
|
+
const tmp2 = formaterLastDigits(value, default_min_week, default_max_week, WeekLastValue);
|
|
535
|
+
if (!tmp2) return false;
|
|
536
|
+
values = tmp2;
|
|
537
|
+
}
|
|
538
|
+
const tmp = new DigitsValue(values);
|
|
539
|
+
if (validateIncrement(increment, default_min_week, default_max_week)) {
|
|
540
|
+
tmp.increment = increment;
|
|
541
|
+
}
|
|
542
|
+
this.week = tmp;
|
|
543
|
+
const { day } = this;
|
|
544
|
+
if (isDigitsValue(day) && (day.increment !== void 0 || !isWildcardValue(day.values))) {
|
|
545
|
+
if (this.cron.isEnableInvalid) {
|
|
546
|
+
this.day = new InvalidValue(true);
|
|
547
|
+
} else {
|
|
548
|
+
day.values = new WildcardValue();
|
|
549
|
+
day.increment = void 0;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
setYear(value, increment) {
|
|
555
|
+
if (!this.cron.isEnableYear) return false;
|
|
556
|
+
const values = formaterDigits(value, 0, MAX_SAFE_INTEGER);
|
|
557
|
+
if (!values) return false;
|
|
558
|
+
const year = new DigitsValue(values);
|
|
559
|
+
if (validateIncrement(increment, 0, MAX_SAFE_INTEGER)) {
|
|
560
|
+
year.increment = increment;
|
|
561
|
+
}
|
|
562
|
+
this.year = year;
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
text() {
|
|
566
|
+
const { cron, second, year } = this;
|
|
567
|
+
const list = [];
|
|
568
|
+
if (cron.isEnableSecond) {
|
|
569
|
+
list.push(second ? second.text() : "0");
|
|
570
|
+
}
|
|
571
|
+
list.push(this.minute.text());
|
|
572
|
+
list.push(this.hour.text());
|
|
573
|
+
list.push(this.day.text());
|
|
574
|
+
list.push(this.month.text());
|
|
575
|
+
list.push(this.week.text());
|
|
576
|
+
if (cron.isEnableYear && year) {
|
|
577
|
+
list.push(year.text());
|
|
578
|
+
}
|
|
579
|
+
return list.join(" ");
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
class Cron {
|
|
583
|
+
#options;
|
|
584
|
+
#regex;
|
|
585
|
+
get isEnableSecond() {
|
|
586
|
+
return this.#options.enableSecond;
|
|
587
|
+
}
|
|
588
|
+
get isEnableYear() {
|
|
589
|
+
return this.#options.enableYear;
|
|
590
|
+
}
|
|
591
|
+
get isEnableDayLast() {
|
|
592
|
+
return this.#options.enableDayLast;
|
|
593
|
+
}
|
|
594
|
+
get isEnableWeekLast() {
|
|
595
|
+
return this.#options.enableWeekLast;
|
|
596
|
+
}
|
|
597
|
+
get isEnableInvalid() {
|
|
598
|
+
return this.#options.enableInvalid;
|
|
599
|
+
}
|
|
600
|
+
constructor(options) {
|
|
601
|
+
const { regex } = this.#options = createCronRegexText(options);
|
|
602
|
+
this.#regex = new RegExp(`^${regex}$`);
|
|
603
|
+
}
|
|
604
|
+
parse(text) {
|
|
605
|
+
const matchs = text.trim().match(this.#regex);
|
|
606
|
+
if (!matchs) return;
|
|
607
|
+
const config = this.#options;
|
|
608
|
+
const { enableSecond, enableYear } = config;
|
|
609
|
+
let second;
|
|
610
|
+
let minute;
|
|
611
|
+
let hour;
|
|
612
|
+
let day;
|
|
613
|
+
let month;
|
|
614
|
+
let week;
|
|
615
|
+
let year;
|
|
616
|
+
if (config.enableSlot) {
|
|
617
|
+
const groups = matchs.groups;
|
|
618
|
+
if (enableSecond) {
|
|
619
|
+
second = groups.s;
|
|
620
|
+
}
|
|
621
|
+
minute = groups.m;
|
|
622
|
+
hour = groups.h;
|
|
623
|
+
day = groups.D;
|
|
624
|
+
month = groups.M;
|
|
625
|
+
week = groups.w;
|
|
626
|
+
if (enableYear) {
|
|
627
|
+
year = groups.Y;
|
|
628
|
+
}
|
|
629
|
+
} else {
|
|
630
|
+
let count = 1;
|
|
631
|
+
if (enableSecond) {
|
|
632
|
+
second = matchs[count++];
|
|
633
|
+
}
|
|
634
|
+
minute = matchs[count++];
|
|
635
|
+
hour = matchs[count++];
|
|
636
|
+
day = matchs[count++];
|
|
637
|
+
month = matchs[count++];
|
|
638
|
+
week = matchs[count++];
|
|
639
|
+
if (enableYear) {
|
|
640
|
+
year = matchs[count++];
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
return new Parse(second, minute, hour, day, month, week, year, this);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
export {
|
|
647
|
+
Cron,
|
|
648
|
+
DayLastValue,
|
|
649
|
+
DigitsValue,
|
|
650
|
+
InvalidValue,
|
|
651
|
+
RangeValue,
|
|
652
|
+
WeekLastValue,
|
|
653
|
+
WildcardValue,
|
|
654
|
+
createCronRegexText,
|
|
655
|
+
default_max_day,
|
|
656
|
+
default_max_hour,
|
|
657
|
+
default_max_minute,
|
|
658
|
+
default_max_month,
|
|
659
|
+
default_max_second,
|
|
660
|
+
default_max_week,
|
|
661
|
+
default_min_day,
|
|
662
|
+
default_min_hour,
|
|
663
|
+
default_min_minute,
|
|
664
|
+
default_min_month,
|
|
665
|
+
default_min_second,
|
|
666
|
+
default_min_week,
|
|
667
|
+
isDayLastValue,
|
|
668
|
+
isDigitsValue,
|
|
669
|
+
isInvalidValue,
|
|
670
|
+
isRangeValue,
|
|
671
|
+
isWeekLastValue,
|
|
672
|
+
isWildcardValue,
|
|
673
|
+
weekDic
|
|
674
|
+
};
|