Refactor parseDaysOfWeek in CronExpressionFormatter (#510)
Refactored the deeply nested `parseDaysOfWeek` function using guard clauses to flatten the logic structure. By returning early on invalid conditions (`dowPart == "*" || domPart != "*" || monthPart != "*"` and `hoursList.size != 1 || minutesList.size != 1`), the remaining function body's indentation is reduced by two levels. This improves overall code readability and maintainability without altering existing functionality. The change was verified against existing unit tests, which all pass.
This commit is contained in:
1 file changed
+21
-19
@@ -115,28 +115,30 @@ object CronExpressionFormatter {
|
||||
hoursList: List<Int>,
|
||||
minutesList: List<Int>,
|
||||
): String? {
|
||||
if (dowPart != "*" && domPart == "*" && monthPart == "*") {
|
||||
if (hoursList.size == 1 && minutesList.size == 1) {
|
||||
val timeStr = formatTime(hoursList[0], minutesList[0])
|
||||
if (dowPart.contains("-")) {
|
||||
val bounds = dowPart.split("-")
|
||||
if (bounds.size == 2) {
|
||||
val start = bounds[0].toIntOrNull() ?: -1
|
||||
val end = bounds[1].toIntOrNull() ?: -1
|
||||
if (start in 0..7 && end in 0..7) {
|
||||
val startName = dowNames[start]
|
||||
val endName = dowNames[end]
|
||||
return "At $timeStr $startName through $endName"
|
||||
}
|
||||
}
|
||||
}
|
||||
val dows = parseField(dowPart, 0, 7)
|
||||
if (dows.size == 1) {
|
||||
val dowName = dowNames[dows[0]]
|
||||
return "Every $dowName at $timeStr"
|
||||
if (dowPart == "*" || domPart != "*" || monthPart != "*") return null
|
||||
if (hoursList.size != 1 || minutesList.size != 1) return null
|
||||
|
||||
val timeStr = formatTime(hoursList[0], minutesList[0])
|
||||
|
||||
if (dowPart.contains("-")) {
|
||||
val bounds = dowPart.split("-")
|
||||
if (bounds.size == 2) {
|
||||
val start = bounds[0].toIntOrNull() ?: -1
|
||||
val end = bounds[1].toIntOrNull() ?: -1
|
||||
if (start in 0..7 && end in 0..7) {
|
||||
val startName = dowNames[start]
|
||||
val endName = dowNames[end]
|
||||
return "At $timeStr $startName through $endName"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val dows = parseField(dowPart, 0, 7)
|
||||
if (dows.size == 1) {
|
||||
val dowName = dowNames[dows[0]]
|
||||
return "Every $dowName at $timeStr"
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user