Skip to content

Commit c1f80c1

Browse files
committed
fix(quartz): Set day-of-week field to ? by default #79
1 parent b87788c commit c1f80c1

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,25 @@ describe('useCron', () => {
107107
it('format option', () => {
108108
const formats: {
109109
value: CronFormat
110+
expectedValue: string
110111
expectedFields: number
111112
expectedPeriods: number
112113
}[] = [
113114
{
114115
value: 'crontab',
116+
expectedValue: '* * * * *',
115117
expectedFields: 5,
116118
expectedPeriods: 6,
117119
},
118120
{
119121
value: 'quartz',
122+
expectedValue: '* * * * * ?',
123+
expectedFields: 6,
124+
expectedPeriods: 7,
125+
},
126+
{
127+
value: 'spring',
128+
expectedValue: '* * * * * *',
120129
expectedFields: 6,
121130
expectedPeriods: 7,
122131
},
@@ -125,6 +134,7 @@ describe('useCron', () => {
125134
for (const format of formats) {
126135
const cron = useCron({ format: format.value })
127136

137+
expect(cron.cron.value).toEqual(format.expectedValue)
128138
expect(cron.segments.length).toEqual(format.expectedFields)
129139
expect(cron.period.items.length).toEqual(format.expectedPeriods)
130140
}

core/src/components/cron-core.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export interface CronContext {
2828
segmentMap: Map<string, UseCronSegmentReturn>
2929
}
3030

31-
function createCron(len: number, seg: string = '*') {
32-
return new Array(len).fill(seg).join(' ')
31+
function createCron(fields: Field[]) {
32+
return fields.map((f) => f.default ?? '*').join(' ')
3333
}
3434

3535
function isDefined<T>(obj: T | undefined): obj is T {
@@ -41,8 +41,8 @@ export class DefaultCronOptions {
4141

4242
format: CronFormat = 'crontab'
4343

44-
initialValue(len: number, seg: string = '*') {
45-
return createCron(len, seg)
44+
initialValue(fields: Field[]) {
45+
return createCron(fields)
4646
}
4747

4848
fields(format: CronFormat, locale: string): Field[] {
@@ -84,6 +84,7 @@ export class DefaultCronOptions {
8484
{ id: 'month', items: items.monthItems },
8585
{
8686
id: 'dayOfWeek',
87+
default: format === 'quartz' ? '?' : undefined,
8788
items: items.dayOfWeekItems,
8889
onChange: isQuartz ? setNoSpecific('day') : undefined,
8990
segmentFactories: isQuartz
@@ -140,7 +141,7 @@ export function useCron(options: CronOptions) {
140141
const locale = options.locale ?? cronDefaults.locale
141142
const format = options.format ?? cronDefaults.format
142143
const { customLocale, fields = cronDefaults.fields(format, locale) } = options
143-
const initialValue = options.initialValue ?? cronDefaults.initialValue(fields.length)
144+
const initialValue = options.initialValue ?? cronDefaults.initialValue(fields)
144145

145146
const l10n = createL10n(locale, customLocale)
146147
const periods = (options.periods ?? cronDefaults.periods(format)).map((p) => {
@@ -176,7 +177,7 @@ export function useCron(options: CronOptions) {
176177

177178
const fromCron = (value: string) => {
178179
if (!value) {
179-
cron.value = createCron(fields.length)
180+
cron.value = createCron(fields)
180181
return
181182
}
182183

@@ -282,9 +283,13 @@ export function setupCron(
282283
},
283284
)
284285

285-
watch(cron.cron, (value) => {
286-
emit('update:model-value', value)
287-
})
286+
watch(
287+
cron.cron,
288+
(value) => {
289+
emit('update:model-value', value)
290+
},
291+
{ immediate: props.modelValue === undefined },
292+
)
288293

289294
watch(cron.period.selected, (value) => {
290295
emit('update:period', value.id)

core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface Field {
4141
items: FieldItem[]
4242
onChange?: (segment: UseCronSegmentReturn, ctx: CronContext) => void
4343
segmentFactories?: SegmentFromString[]
44+
default?: string
4445
}
4546

4647
export interface Period {

0 commit comments

Comments
 (0)