2022-04-15 09:42:07 +02:00
|
|
|
package clean
|
2021-12-15 12:24:05 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// State returns the full, normalized state name.
|
2021-12-16 15:26:54 +01:00
|
|
|
func State(s, countryCode string) string {
|
|
|
|
if s == "" || reject(s, txt.ClipName) {
|
|
|
|
return Empty
|
|
|
|
}
|
|
|
|
|
2021-12-15 12:24:05 +01:00
|
|
|
// Remove whitespace from name.
|
2021-12-16 15:26:54 +01:00
|
|
|
s = strings.TrimSpace(s)
|
2021-12-15 12:24:05 +01:00
|
|
|
|
|
|
|
// Empty?
|
2021-12-16 15:26:54 +01:00
|
|
|
if s == "" || s == txt.UnknownStateCode {
|
2021-12-15 12:24:05 +01:00
|
|
|
// State doesn't have a name.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove non-printable and other potentially problematic characters.
|
2021-12-16 15:26:54 +01:00
|
|
|
s = strings.Map(func(r rune) rune {
|
2021-12-15 12:24:05 +01:00
|
|
|
if !unicode.IsPrint(r) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r {
|
|
|
|
case '~', '\\', ':', '|', '"', '?', '*', '<', '>', '{', '}':
|
|
|
|
return -1
|
|
|
|
default:
|
|
|
|
return r
|
|
|
|
}
|
2021-12-16 15:26:54 +01:00
|
|
|
}, s)
|
2021-12-15 12:24:05 +01:00
|
|
|
|
|
|
|
// Normalize country code.
|
|
|
|
countryCode = strings.ToLower(strings.TrimSpace(countryCode))
|
|
|
|
|
|
|
|
// Is the name an abbreviation that should be normalized?
|
|
|
|
if states, found := txt.StatesByCountry[countryCode]; !found {
|
|
|
|
// Unknown country.
|
2021-12-16 15:26:54 +01:00
|
|
|
} else if normalized, found := states[s]; !found {
|
2021-12-15 12:24:05 +01:00
|
|
|
// Unknown abbreviation.
|
|
|
|
} else if normalized != "" {
|
|
|
|
// Yes, use normalized name.
|
2021-12-16 15:26:54 +01:00
|
|
|
s = normalized
|
2021-12-15 12:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return normalized state name.
|
2021-12-16 15:26:54 +01:00
|
|
|
return s
|
2021-12-15 12:24:05 +01:00
|
|
|
}
|