Skip to content

Commit

Permalink
fix(quartz): Day-of-Week values must be between 1 and 7 #62
Browse files Browse the repository at this point in the history
  • Loading branch information
abichinger committed Oct 31, 2024
1 parent 572de5d commit f8cbe3a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
12 changes: 12 additions & 0 deletions core/src/components/__tests__/cron-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ describe('useCron', () => {
period: 'hour',
expected: `Every Hour at every minute(s)`,
},
{
format: 'crontab',
value: '59 23 ? * 0',
period: 'week',
expected: `Every Week on Sun at 23 : 59`,
},
{
format: 'quartz',
value: '* * * * * *',
Expand All @@ -69,6 +75,12 @@ describe('useCron', () => {
period: 'month',
expected: `Every Month on no specific day and every day of the week at 10 : 15 : 00`,
},
{
format: 'quartz',
value: '59 59 23 ? * 1',
period: 'week',
expected: `Every Week on Sun at 23 : 59 : 59`,
},
]

for (const t of tests) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/components/cron-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class DefaultCronOptions {

fields(format: CronFormat, locale: string): Field[] {
const isQuartz = format == 'quartz'
const items = defaultItems(locale)
const items = defaultItems(locale, format)

const setNoSpecific = (fieldId: string) => {
return (value: UseCronSegmentReturn, { segmentMap }: CronContext) => {
Expand Down
52 changes: 27 additions & 25 deletions core/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FieldItem } from './types'
import type { CronFormat, FieldItem } from './types'

function range(start: number, end: number, step = 1) {
const r = []
Expand Down Expand Up @@ -78,36 +78,38 @@ function genItems(
/**
*
* @param locale - locale code, e.g.: en, en-GB de-DE
* @param [format='crontab'] format of cron expression
* @returns items for minute, hour, day, month and day of week
*/
function defaultItems(localeCode: string) {
function defaultItems(localeCode: string, format: CronFormat = 'crontab') {
const monthName = (month: number, short: boolean = false) => {
return new Date(2021, month - 1, 1).toLocaleDateString(localeCode, {
month: short ? 'short' : 'long',
})
}

const weekdayName = (weekday: number, short: boolean = false) => {
// if weekday is 0, this is the first sunday in 2021
return new Date(2021, 0, 3 + weekday).toLocaleDateString(localeCode, {
weekday: short ? 'short' : 'long',
})
}

return {
secondItems: genItems(0, 59, (value) => pad(value, 2)),
minuteItems: genItems(0, 59, (value) => pad(value, 2)),
hourItems: genItems(0, 23, (value) => pad(value, 2)),
dayItems: genItems(1, 31),
monthItems: genItems(
1,
12,
(value) => {
return new Date(2021, value - 1, 1).toLocaleDateString(localeCode, { month: 'long' })
},
(value) => {
return new Date(2021, value - 1, 1).toLocaleDateString(localeCode, { month: 'short' })
},
),
dayOfWeekItems: genItems(
0,
6,
(value) => {
const date = new Date(2021, 0, 3 + value) // first sunday in 2021
return date.toLocaleDateString(localeCode, { weekday: 'long' })
},
(value) => {
const date = new Date(2021, 0, 3 + value) // first sunday in 2021
return date.toLocaleDateString(localeCode, { weekday: 'short' })
},
),
monthItems: genItems(1, 12, monthName, (value) => monthName(value, true)),
dayOfWeekItems:
format === 'crontab'
? genItems(0, 6, weekdayName, (value) => weekdayName(value, true))
: genItems(
1,
7,
(value) => weekdayName(value - 1),
(value) => weekdayName(value - 1, true),
),
}
}

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

export {
Range,
deepMerge,
defaultItems,
genItems,
isObject,
isSquence,
pad,
Range,
range,
splitArray,
traverse,
Expand Down

0 comments on commit f8cbe3a

Please sign in to comment.