Skip to content

Commit 577e019

Browse files
committed
feat: add spring format #68
1 parent 8d5d038 commit 577e019

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { describe, expect, it, test } from 'vitest'
22

33
import type { CronFormat, Period } from '@/types'
44
import { mount } from '@vue/test-utils'
@@ -28,7 +28,7 @@ function cronToString({ selected: { value: selected }, period }: UseCronReturn):
2828
}
2929

3030
describe('useCron', () => {
31-
it('renders properly', async () => {
31+
describe('renders properly', () => {
3232
const tests: {
3333
format: CronFormat
3434
value: string
@@ -81,12 +81,20 @@ describe('useCron', () => {
8181
period: 'week',
8282
expected: `Every Week on Sun at 23 : 59 : 59`,
8383
},
84+
{
85+
format: 'spring',
86+
value: '59 59 23 ? * 1',
87+
period: 'week',
88+
expected: `Every Week on Mon at 23 : 59 : 59`,
89+
},
8490
]
8591

8692
for (const t of tests) {
87-
const cron = useCron({ format: t.format, initialValue: t.value, initialPeriod: t.period })
88-
await nextTick()
89-
expect(cronToString(cron)).toEqual(t.expected)
93+
test([t.value, t.format].join(', '), async () => {
94+
const cron = useCron({ format: t.format, initialValue: t.value, initialPeriod: t.period })
95+
await nextTick()
96+
expect(cronToString(cron)).toEqual(t.expected)
97+
})
9098
}
9199
})
92100

core/src/components/cron-core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class DefaultCronOptions {
4646
}
4747

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

5252
const setNoSpecific = (fieldId: string) => {
@@ -100,7 +100,7 @@ export class DefaultCronOptions {
100100
}
101101

102102
periods(format: CronFormat): Period[] {
103-
const isQuartz = format == 'quartz'
103+
const isQuartz = format == 'quartz' || format == 'spring'
104104
const second = isQuartz ? [{ id: 'q-second', value: [] }] : []
105105
const secondField = isQuartz ? ['second'] : []
106106
const prefix = isQuartz ? 'q-' : ''

core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CronContext } from './components/cron-core'
22
import type { UseCronSegmentReturn } from './components/cron-segment'
33

4-
export type CronFormat = 'crontab' | 'quartz'
4+
export type CronFormat = 'crontab' | 'quartz' | 'spring'
55

66
export interface CronSegment {
77
field: FieldWrapper

core/src/util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ function defaultItems(localeCode: string, format: CronFormat = 'crontab') {
102102
dayItems: genItems(1, 31),
103103
monthItems: genItems(1, 12, monthName, (value) => monthName(value, true)),
104104
dayOfWeekItems:
105-
format === 'crontab'
106-
? genItems(0, 6, weekdayName, (value) => weekdayName(value, true))
107-
: genItems(
105+
format === 'quartz'
106+
? genItems(
108107
1,
109108
7,
110109
(value) => weekdayName(value - 1),
111110
(value) => weekdayName(value - 1, true),
112-
),
111+
)
112+
: genItems(0, 6, weekdayName, (value) => weekdayName(value, true)),
113113
}
114114
}
115115

0 commit comments

Comments
 (0)