(cron: string, opts?: { utc?: boolean })
| 216 | } |
| 217 | |
| 218 | export function cronToHuman(cron: string, opts?: { utc?: boolean }): string { |
| 219 | const utc = opts?.utc ?? false |
| 220 | const parts = cron.trim().split(/\s+/) |
| 221 | if (parts.length !== 5) return cron |
| 222 | |
| 223 | const [minute, hour, dayOfMonth, month, dayOfWeek] = parts as [ |
| 224 | string, |
| 225 | string, |
| 226 | string, |
| 227 | string, |
| 228 | string, |
| 229 | ] |
| 230 | |
| 231 | // Every N minutes: step/N * * * * |
| 232 | const everyMinMatch = minute.match(/^\*\/(\d+)$/) |
| 233 | if ( |
| 234 | everyMinMatch && |
| 235 | hour === '*' && |
| 236 | dayOfMonth === '*' && |
| 237 | month === '*' && |
| 238 | dayOfWeek === '*' |
| 239 | ) { |
| 240 | const n = parseInt(everyMinMatch[1]!, 10) |
| 241 | return n === 1 ? 'Every minute' : `Every ${n} minutes` |
| 242 | } |
| 243 | |
| 244 | // Every hour: 0 * * * * |
| 245 | if ( |
| 246 | minute.match(/^\d+$/) && |
| 247 | hour === '*' && |
| 248 | dayOfMonth === '*' && |
| 249 | month === '*' && |
| 250 | dayOfWeek === '*' |
| 251 | ) { |
| 252 | const m = parseInt(minute, 10) |
| 253 | if (m === 0) return 'Every hour' |
| 254 | return `Every hour at :${m.toString().padStart(2, '0')}` |
| 255 | } |
| 256 | |
| 257 | // Every N hours: 0 step/N * * * |
| 258 | const everyHourMatch = hour.match(/^\*\/(\d+)$/) |
| 259 | if ( |
| 260 | minute.match(/^\d+$/) && |
| 261 | everyHourMatch && |
| 262 | dayOfMonth === '*' && |
| 263 | month === '*' && |
| 264 | dayOfWeek === '*' |
| 265 | ) { |
| 266 | const n = parseInt(everyHourMatch[1]!, 10) |
| 267 | const m = parseInt(minute, 10) |
| 268 | const suffix = m === 0 ? '' : ` at :${m.toString().padStart(2, '0')}` |
| 269 | return n === 1 ? `Every hour${suffix}` : `Every ${n} hours${suffix}` |
| 270 | } |
| 271 | |
| 272 | // --- Remaining cases reference hour+minute: branch on utc ---------------- |
| 273 | |
| 274 | if (!minute.match(/^\d+$/) || !hour.match(/^\d+$/)) return cron |
| 275 | const m = parseInt(minute, 10) |
no test coverage detected