Centralize daypart key canonicalization
This commit is contained in:
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user