Skip to content

Commit 8674ca4

Browse files
committed
Cleaning up, getting ready for some regex-fu.
Signed-off-by: Alexander Trauzzi <[email protected]>
1 parent d321189 commit 8674ca4

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

src/implementation/Client/GRPCClient/jobs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
import IClientJobs from "../../../interfaces/Client/IClientJobs";
1818
import GRPCClient from "./GRPCClient";
19-
import { JobSchedule } from "../../../types/jobs/JobSchedule.type";
19+
import { Schedule } from "../../../types/jobs/JobSchedule.type";
2020
import { Job } from "../../../types/jobs/Job.type";
2121

2222

@@ -31,7 +31,7 @@ export default class GRPCClientJobs implements IClientJobs {
3131
async schedule(
3232
jobName: string,
3333
data: object | string,
34-
schedule: JobSchedule | null = null,
34+
schedule: Schedule | null = null,
3535
dueTime: string | Date | null = null,
3636
repeats: number | null = null,
3737
ttl: string | null = null

src/types/jobs/CronExpression.type.ts

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export enum CronPeriod {
2323
Month,
2424
}
2525

26-
export type TimeValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59;
27-
26+
export type SecondOrMinuteValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59;
27+
export type HourValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23;
2828
export type DayOfMonth = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31;
2929

3030
export enum DayOfWeek {
@@ -53,20 +53,25 @@ export enum Month {
5353
}
5454

5555
export class CronExpressionBuilder {
56-
57-
public static on(period: CronPeriod.Second, ...values: (TimeValue[] | TimeValue)[]): CronExpression;
58-
public static on(period: CronPeriod.Minute, ...values: (TimeValue[] | TimeValue)[]): CronExpression;
59-
public static on(period: CronPeriod.Hour, ...values: (TimeValue[] | TimeValue)[]): CronExpression;
56+
public static on(
57+
period: CronPeriod.Second,
58+
...values: (SecondOrMinuteValue[] | SecondOrMinuteValue)[]
59+
): CronExpression;
60+
public static on(
61+
period: CronPeriod.Minute,
62+
...values: (SecondOrMinuteValue[] | SecondOrMinuteValue)[]
63+
): CronExpression;
64+
public static on(period: CronPeriod.Hour, ...values: (HourValue[] | HourValue)[]): CronExpression;
6065
public static on(period: CronPeriod.DayOfWeek, ...values: (DayOfWeek[] | DayOfWeek)[]): CronExpression;
6166
public static on(period: CronPeriod.DayOfMonth, ...values: (DayOfMonth[] | DayOfMonth)[]): CronExpression;
6267
public static on(period: CronPeriod.Month, ...values: (Month[] | Month)[]): CronExpression;
6368
public static on(period: any, ...values: (any[] | any)[]) {
6469
return new CronExpression().on(period, values.flat());
6570
}
6671

67-
public static through(period: CronPeriod.Second, from: TimeValue, to: TimeValue): CronExpression;
68-
public static through(period: CronPeriod.Minute, from: TimeValue, to: TimeValue): CronExpression;
69-
public static through(period: CronPeriod.Hour, from: TimeValue, to: TimeValue): CronExpression;
72+
public static through(period: CronPeriod.Second, from: SecondOrMinuteValue, to: SecondOrMinuteValue): CronExpression;
73+
public static through(period: CronPeriod.Minute, from: SecondOrMinuteValue, to: SecondOrMinuteValue): CronExpression;
74+
public static through(period: CronPeriod.Hour, from: HourValue, to: HourValue): CronExpression;
7075
public static through(period: CronPeriod.DayOfWeek, from: DayOfWeek, to: DayOfWeek): CronExpression;
7176
public static through(period: CronPeriod.DayOfMonth, from: DayOfMonth, to: DayOfMonth): CronExpression;
7277
public static through(period: CronPeriod.Month, from: Month, to: Month): CronExpression;
@@ -80,15 +85,13 @@ export class CronExpressionBuilder {
8085
}
8186

8287
export class CronExpression {
83-
private static readonly SecondsAndMinutesRegexText = /([0-5]?\d-[0-5]?\d)|([0-5]?\d,?)|(\*(\/[0-5]?\d)?)/;
84-
private static readonly HoursRegexText =
85-
/(([0-1]?\d)|(2[0-3])-([0-1]?\d)|(2[0-3]))|(([0-1]?\d)|(2[0-3]),?)|(\*(\/([0-1]?\d)|(2[0-3]))?)/;
86-
private static readonly DayOfMonthRegexText =
87-
/\*|(\*\/(([0-2]?\d)|(3[0-1])))|(((([0-2]?\d)|(3[0-1]))(-(([0-2]?\d)|(3[0-1])))?))/;
88-
private static readonly MonthRegexText =
89-
/(^(\*\/)?((0?\d)|(1[0-2]))$)|(^\*$)|(^((0?\d)|(1[0-2]))(-((0?\d)|(1[0-2]))?)$)|(^(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:-(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:-(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*$)/;
90-
private static readonly DayOfWeekRegexText =
91-
/\*|(\*\/(0?[0-6])|(0?[0-6](-0?[0-6])?)|((,?(SUN|MON|TUE|WED|THU|FRI|SAT))+)|((SUN|MON|TUE|WED|THU|FRI|SAT)(-(SUN|MON|TUE|WED|THU|FRI|SAT))?))/;
88+
private static readonly SecondsAndMinutesRegexText = /.*/;
89+
private static readonly HoursRegexText = /.*/;
90+
private static readonly DayOfMonthRegexText = /.*/;
91+
// (^(\*\/)?((0?\d)|(1[0-2]))$)|(^\*$)|(^((0?\d)|(1[0-2]))(-((0?\d)|(1[0-2]))?)$)|(^(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:-(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:-(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*$)
92+
private static readonly MonthRegexText = /.*/;
93+
// \*|(\*\/(0?[0-6])|(0?[0-6](-0?[0-6])?)|((,?(SUN|MON|TUE|WED|THU|FRI|SAT))+)|((SUN|MON|TUE|WED|THU|FRI|SAT)(-(SUN|MON|TUE|WED|THU|FRI|SAT))?))
94+
private static readonly DayOfWeekRegexText = /.*/;
9295

9396
private static readonly cronExpressionRegex = new RegExp(
9497
`${CronExpression.SecondsAndMinutesRegexText.source} ${CronExpression.SecondsAndMinutesRegexText.source} ${CronExpression.HoursRegexText.source} ${CronExpression.DayOfMonthRegexText.source} ${CronExpression.MonthRegexText.source} ${CronExpression.DayOfWeekRegexText.source}`,
@@ -105,9 +108,9 @@ export class CronExpression {
105108
private month = "*";
106109
private dayOfWeek = "*";
107110

108-
public on(period: CronPeriod.Second, ...values: (TimeValue[] | TimeValue)[]): this;
109-
public on(period: CronPeriod.Minute, ...values: (TimeValue[] | TimeValue)[]): this;
110-
public on(period: CronPeriod.Hour, ...values: (TimeValue[] | TimeValue)[]): this;
111+
public on(period: CronPeriod.Second, ...values: (SecondOrMinuteValue[] | SecondOrMinuteValue)[]): this;
112+
public on(period: CronPeriod.Minute, ...values: (SecondOrMinuteValue[] | SecondOrMinuteValue)[]): this;
113+
public on(period: CronPeriod.Hour, ...values: (HourValue[] | HourValue)[]): this;
111114
public on(period: CronPeriod.DayOfWeek, ...values: (DayOfWeek[] | DayOfWeek)[]): this;
112115
public on(period: CronPeriod.DayOfMonth, ...values: (DayOfMonth[] | DayOfMonth)[]): this;
113116
public on(period: CronPeriod.Month, ...values: (Month[] | Month)[]): this;
@@ -116,13 +119,13 @@ export class CronExpression {
116119

117120
switch (period) {
118121
case CronPeriod.Second:
119-
this.seconds = this.prepareTimeValueList(fixedValues);
122+
this.seconds = this.prepareSecondOrMinuteValueList(fixedValues);
120123
break;
121124
case CronPeriod.Minute:
122-
this.minutes = this.prepareTimeValueList(fixedValues);
125+
this.minutes = this.prepareSecondOrMinuteValueList(fixedValues);
123126
break;
124127
case CronPeriod.Hour:
125-
this.hours = this.prepareTimeValueList(fixedValues);
128+
this.hours = this.prepareHourValueList(fixedValues);
126129
break;
127130
case CronPeriod.DayOfWeek:
128131
this.dayOfWeek = this.prepareDayOfWeekValueList(fixedValues);
@@ -138,23 +141,22 @@ export class CronExpression {
138141
return this;
139142
}
140143

141-
public through(period: CronPeriod.Second, from: TimeValue, to: TimeValue): CronExpression;
142-
public through(period: CronPeriod.Minute, from: TimeValue, to: TimeValue): CronExpression;
143-
public through(period: CronPeriod.Hour, from: TimeValue, to: TimeValue): CronExpression;
144+
public through(period: CronPeriod.Second, from: SecondOrMinuteValue, to: SecondOrMinuteValue): CronExpression;
145+
public through(period: CronPeriod.Minute, from: SecondOrMinuteValue, to: SecondOrMinuteValue): CronExpression;
146+
public through(period: CronPeriod.Hour, from: SecondOrMinuteValue, to: SecondOrMinuteValue): CronExpression;
144147
public through(period: CronPeriod.DayOfWeek, from: DayOfWeek, to: DayOfWeek): CronExpression;
145148
public through(period: CronPeriod.DayOfMonth, from: DayOfMonth, to: DayOfMonth): CronExpression;
146149
public through(period: CronPeriod.Month, from: Month, to: Month): CronExpression;
147150
public through(period: any, from: any, to: any) {
148-
149151
switch (period) {
150152
case CronPeriod.Second:
151-
this.seconds = this.prepareTimeValueRange(from, to);
153+
this.seconds = this.prepareSecondOrMinuteValueRange(from, to);
152154
break;
153155
case CronPeriod.Minute:
154-
this.minutes = this.prepareTimeValueRange(from, to);
156+
this.minutes = this.prepareSecondOrMinuteValueRange(from, to);
155157
break;
156158
case CronPeriod.Hour:
157-
this.hours = this.prepareTimeValueRange(from, to);
159+
this.hours = this.prepareHourValueRange(from, to);
158160
break;
159161
case CronPeriod.DayOfWeek:
160162
this.dayOfWeek = this.prepareDayOfWeekValueRange(from, to);
@@ -198,22 +200,29 @@ export class CronExpression {
198200
return `${this.seconds} ${this.minutes} ${this.hours} ${this.dayOfMonth} ${this.month} ${this.dayOfWeek}`;
199201
}
200202

201-
private prepareTimeValueRange(from: TimeValue, to: TimeValue) {
202-
if (from >= to)
203-
throw new Error("Invalid range: 'from' must be less than 'to'");
203+
private prepareSecondOrMinuteValueRange(from: SecondOrMinuteValue, to: SecondOrMinuteValue) {
204+
if (from >= to) throw new Error("Invalid range: 'from' must be less than 'to'");
204205

205206
if ([from, to].some((v) => v < 0 || v > 59))
206-
throw new RangeError("Time values must be within 0 and 59, inclusively.");
207+
throw new RangeError("Time values must be within 0 and 59, inclusive.");
208+
209+
return `${from}-${to}`;
210+
}
211+
212+
private prepareHourValueRange(from: HourValue, to: HourValue) {
213+
if (from >= to) throw new Error("Invalid range: 'from' must be less than 'to'");
214+
215+
if ([from, to].some((v) => v < 0 || v > 23))
216+
throw new RangeError("Hour values must be within 0 and 23, inclusive.");
207217

208218
return `${from}-${to}`;
209219
}
210220

211221
private prepareDayOfMonthValueRange(from: DayOfMonth, to: DayOfMonth) {
212-
if (from >= to)
213-
throw new Error("Invalid range: 'from' must be less than 'to'");
222+
if (from >= to) throw new Error("Invalid range: 'from' must be less than 'to'");
214223

215224
if ([from, to].some((v) => v < 1 || v > 31))
216-
throw new RangeError("Day of month values must be within 1 and 31, inclusively.");
225+
throw new RangeError("Day of month values must be within 1 and 31, inclusive.");
217226

218227
return `${from}-${to}`;
219228
}
@@ -232,29 +241,36 @@ export class CronExpression {
232241
return `${from}-${to}`;
233242
}
234243

235-
private prepareTimeValueList(timeValues: TimeValue[]) {
244+
private prepareSecondOrMinuteValueList(timeValues: SecondOrMinuteValue[]) {
236245
if (timeValues.some((v) => v < 0 || v > 59))
237-
throw new RangeError("Time values must be within 0 and 59, inclusively.");
246+
throw new RangeError("Time values must be within 0 and 59, inclusive.");
247+
248+
return [...timeValues].sort((a, b) => a - b).join(",");
249+
}
250+
251+
private prepareHourValueList(timeValues: HourValue[]) {
252+
if (timeValues.some((v) => v < 0 || v > 23))
253+
throw new RangeError("Hour values must be within 0 and 23, inclusive.");
238254

239255
return [...timeValues].sort((a, b) => a - b).join(",");
240256
}
241257

242258
private prepareDayOfMonthValueList(dayOfMonthValues: DayOfMonth[]) {
243259
if (dayOfMonthValues.some((v) => v < 1 || v > 31))
244-
throw new RangeError("Day of month values must be within 1 and 31, inclusively.");
260+
throw new RangeError("Day of month values must be within 1 and 31, inclusive.");
245261

246262
return [...dayOfMonthValues].sort((a, b) => a - b).join(",");
247263
}
248264

249265
private prepareMonthValueList(monthValues: Month[]) {
250266
return [...monthValues]
251-
.sort((left, right) => Object.values(Month).indexOf(left) - Object.values(Month).indexOf(right))
252-
.join(",");
267+
.sort((left, right) => Object.values(Month).indexOf(left) - Object.values(Month).indexOf(right))
268+
.join(",");
253269
}
254270

255271
private prepareDayOfWeekValueList(dayValues: DayOfWeek[]) {
256272
return [...dayValues]
257-
.sort((left, right) => Object.values(DayOfWeek).indexOf(left) - Object.values(DayOfWeek).indexOf(right))
258-
.join(",");
273+
.sort((left, right) => Object.values(DayOfWeek).indexOf(left) - Object.values(DayOfWeek).indexOf(right))
274+
.join(",");
259275
}
260276
}

0 commit comments

Comments
 (0)