Skip to content

Commit f8cbe3a

Browse files
committed
fix(quartz): Day-of-Week values must be between 1 and 7 #62
1 parent 572de5d commit f8cbe3a

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

core/src/components/__tests__/cron-core.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ describe('useCron', () => {
5151
period: 'hour',
5252
expected: `Every Hour at every minute(s)`,
5353
},
54+
{
55+
format: 'crontab',
56+
value: '59 23 ? * 0',
57+
period: 'week',
58+
expected: `Every Week on Sun at 23 : 59`,
59+
},
5460
{
5561
format: 'quartz',
5662
value: '* * * * * *',
@@ -69,6 +75,12 @@ describe('useCron', () => {
6975
period: 'month',
7076
expected: `Every Month on no specific day and every day of the week at 10 : 15 : 00`,
7177
},
78+
{
79+
format: 'quartz',
80+
value: '59 59 23 ? * 1',
81+
period: 'week',
82+
expected: `Every Week on Sun at 23 : 59 : 59`,
83+
},
7284
]
7385

7486
for (const t of tests) {

core/src/components/cron-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class DefaultCronOptions {
4747

4848
fields(format: CronFormat, locale: string): Field[] {
4949
const isQuartz = format == 'quartz'
50-
const items = defaultItems(locale)
50+
const items = defaultItems(locale, format)
5151

5252
const setNoSpecific = (fieldId: string) => {
5353
return (value: UseCronSegmentReturn, { segmentMap }: CronContext) => {

core/src/util.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FieldItem } from './types'
1+
import type { CronFormat, FieldItem } from './types'
22

33
function range(start: number, end: number, step = 1) {
44
const r = []
@@ -78,36 +78,38 @@ function genItems(
7878
/**
7979
*
8080
* @param locale - locale code, e.g.: en, en-GB de-DE
81+
* @param [format='crontab'] format of cron expression
8182
* @returns items for minute, hour, day, month and day of week
8283
*/
83-
function defaultItems(localeCode: string) {
84+
function defaultItems(localeCode: string, format: CronFormat = 'crontab') {
85+
const monthName = (month: number, short: boolean = false) => {
86+
return new Date(2021, month - 1, 1).toLocaleDateString(localeCode, {
87+
month: short ? 'short' : 'long',
88+
})
89+
}
90+
91+
const weekdayName = (weekday: number, short: boolean = false) => {
92+
// if weekday is 0, this is the first sunday in 2021
93+
return new Date(2021, 0, 3 + weekday).toLocaleDateString(localeCode, {
94+
weekday: short ? 'short' : 'long',
95+
})
96+
}
97+
8498
return {
8599
secondItems: genItems(0, 59, (value) => pad(value, 2)),
86100
minuteItems: genItems(0, 59, (value) => pad(value, 2)),
87101
hourItems: genItems(0, 23, (value) => pad(value, 2)),
88102
dayItems: genItems(1, 31),
89-
monthItems: genItems(
90-
1,
91-
12,
92-
(value) => {
93-
return new Date(2021, value - 1, 1).toLocaleDateString(localeCode, { month: 'long' })
94-
},
95-
(value) => {
96-
return new Date(2021, value - 1, 1).toLocaleDateString(localeCode, { month: 'short' })
97-
},
98-
),
99-
dayOfWeekItems: genItems(
100-
0,
101-
6,
102-
(value) => {
103-
const date = new Date(2021, 0, 3 + value) // first sunday in 2021
104-
return date.toLocaleDateString(localeCode, { weekday: 'long' })
105-
},
106-
(value) => {
107-
const date = new Date(2021, 0, 3 + value) // first sunday in 2021
108-
return date.toLocaleDateString(localeCode, { weekday: 'short' })
109-
},
110-
),
103+
monthItems: genItems(1, 12, monthName, (value) => monthName(value, true)),
104+
dayOfWeekItems:
105+
format === 'crontab'
106+
? genItems(0, 6, weekdayName, (value) => weekdayName(value, true))
107+
: genItems(
108+
1,
109+
7,
110+
(value) => weekdayName(value - 1),
111+
(value) => weekdayName(value - 1, true),
112+
),
111113
}
112114
}
113115

@@ -202,13 +204,13 @@ function splitArray<T>(arr: T[], chunkSize: number, fill: boolean = true): (T |
202204
}
203205

204206
export {
205-
Range,
206207
deepMerge,
207208
defaultItems,
208209
genItems,
209210
isObject,
210211
isSquence,
211212
pad,
213+
Range,
212214
range,
213215
splitArray,
214216
traverse,

0 commit comments

Comments
 (0)