Centralize daypart key canonicalization

This commit is contained in:
2026-08-13 01:00:54 +00:00
parent daf0c7efd7
commit 13829cc65c
4 changed files with 46 additions and 40 deletions

View File

@@ -2,7 +2,9 @@ package forecast
import (
"fmt"
"strings"
"time"
"unicode"
"gitea.maximumdirect.net/eric/weatherreporter/internal/timeutil"
"gitea.maximumdirect.net/eric/weatherreporter/internal/weatherdata"
@@ -14,6 +16,25 @@ type DaypartDefinition struct {
End string `json:"end"`
}
// CanonicalDaypartKey returns the stable identity for a configured daypart name.
func CanonicalDaypartKey(value string) string {
lower := strings.ToLower(strings.TrimSpace(value))
var out strings.Builder
lastUnderscore := false
for _, r := range lower {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
out.WriteRune(r)
lastUnderscore = false
continue
}
if !lastUnderscore {
out.WriteByte('_')
lastUnderscore = true
}
}
return strings.Trim(out.String(), "_")
}
type DaypartWindow struct {
Name string `json:"name"`
Start time.Time `json:"start"`