refactor: reduce DurationSeconds complexity with token parsing

This commit is contained in:
2026-06-11 12:31:33 +02:00
parent b72bace16a
commit 7af597d8fc

View File

@@ -230,35 +230,34 @@ func (a Anime) DurationSeconds() float64 {
return 0 return 0
} }
var hours, minutes int var hours, minutes int
var isHours bool var currentValue int
var currentNum string hasValue := false
for _, c := range a.Duration { for _, token := range strings.Fields(strings.ToLower(a.Duration)) {
if c >= '0' && c <= '9' { value, err := strconv.Atoi(token)
currentNum += string(c) if err == nil {
} else if c == ' ' && currentNum != "" { currentValue = value
val, _ := strconv.Atoi(currentNum) hasValue = true
if isHours { continue
hours = val }
} else { if !hasValue {
minutes = val continue
} }
currentNum = ""
} else if len(currentNum) > 0 && (c == 'h' || c == 'H') { switch {
isHours = true case strings.HasPrefix(token, "h"):
val, _ := strconv.Atoi(currentNum) hours = currentValue
hours = val hasValue = false
currentNum = "" case strings.HasPrefix(token, "m"):
minutes = currentValue
hasValue = false
} }
} }
if currentNum != "" {
val, _ := strconv.Atoi(currentNum) if hasValue {
if isHours { minutes = currentValue
hours = val
} else {
minutes = val
}
} }
return float64(hours*60+minutes) * 60 return float64(hours*60+minutes) * 60
} }