|
1 |
| -import type { FieldItem } from './types' |
| 1 | +import type { CronFormat, FieldItem } from './types' |
2 | 2 |
|
3 | 3 | function range(start: number, end: number, step = 1) {
|
4 | 4 | const r = []
|
@@ -78,36 +78,38 @@ function genItems(
|
78 | 78 | /**
|
79 | 79 | *
|
80 | 80 | * @param locale - locale code, e.g.: en, en-GB de-DE
|
| 81 | + * @param [format='crontab'] format of cron expression |
81 | 82 | * @returns items for minute, hour, day, month and day of week
|
82 | 83 | */
|
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 | + |
84 | 98 | return {
|
85 | 99 | secondItems: genItems(0, 59, (value) => pad(value, 2)),
|
86 | 100 | minuteItems: genItems(0, 59, (value) => pad(value, 2)),
|
87 | 101 | hourItems: genItems(0, 23, (value) => pad(value, 2)),
|
88 | 102 | 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 | + ), |
111 | 113 | }
|
112 | 114 | }
|
113 | 115 |
|
@@ -202,13 +204,13 @@ function splitArray<T>(arr: T[], chunkSize: number, fill: boolean = true): (T |
|
202 | 204 | }
|
203 | 205 |
|
204 | 206 | export {
|
205 |
| - Range, |
206 | 207 | deepMerge,
|
207 | 208 | defaultItems,
|
208 | 209 | genItems,
|
209 | 210 | isObject,
|
210 | 211 | isSquence,
|
211 | 212 | pad,
|
| 213 | + Range, |
212 | 214 | range,
|
213 | 215 | splitArray,
|
214 | 216 | traverse,
|
|
0 commit comments