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>,
|
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])
|
|
||||||
if (dowPart.contains("-")) {
|
val timeStr = formatTime(hoursList[0], minutesList[0])
|
||||||
val bounds = dowPart.split("-")
|
|
||||||
if (bounds.size == 2) {
|
if (dowPart.contains("-")) {
|
||||||
val start = bounds[0].toIntOrNull() ?: -1
|
val bounds = dowPart.split("-")
|
||||||
val end = bounds[1].toIntOrNull() ?: -1
|
if (bounds.size == 2) {
|
||||||
if (start in 0..7 && end in 0..7) {
|
val start = bounds[0].toIntOrNull() ?: -1
|
||||||
val startName = dowNames[start]
|
val end = bounds[1].toIntOrNull() ?: -1
|
||||||
val endName = dowNames[end]
|
if (start in 0..7 && end in 0..7) {
|
||||||
return "At $timeStr $startName through $endName"
|
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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val dows = parseField(dowPart, 0, 7)
|
||||||
|
if (dows.size == 1) {
|
||||||
|
val dowName = dowNames[dows[0]]
|
||||||
|
return "Every $dowName at $timeStr"
|
||||||
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user