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:
M57 authored and GitHub committed 2026-07-09 15:26:29 +03:00
1 parent 2f16ed7e62
commit 5bc5d17032
1 file changed
+6 -4
@@ -115,9 +115,11 @@ object CronExpressionFormatter {
hoursList: List<Int>, hoursList: List<Int>,
minutesList: List<Int>, minutesList: List<Int>,
): String? { ): String? {
if (dowPart != "*" && domPart == "*" && monthPart == "*") { if (dowPart == "*" || domPart != "*" || monthPart != "*") return null
if (hoursList.size == 1 && minutesList.size == 1) { if (hoursList.size != 1 || minutesList.size != 1) return null
val timeStr = formatTime(hoursList[0], minutesList[0]) val timeStr = formatTime(hoursList[0], minutesList[0])
if (dowPart.contains("-")) { if (dowPart.contains("-")) {
val bounds = dowPart.split("-") val bounds = dowPart.split("-")
if (bounds.size == 2) { if (bounds.size == 2) {
@@ -130,13 +132,13 @@ object CronExpressionFormatter {
} }
} }
} }
val dows = parseField(dowPart, 0, 7) val dows = parseField(dowPart, 0, 7)
if (dows.size == 1) { if (dows.size == 1) {
val dowName = dowNames[dows[0]] val dowName = dowNames[dows[0]]
return "Every $dowName at $timeStr" return "Every $dowName at $timeStr"
} }
}
}
return null return null
} }