/
state.go
54 lines (44 loc) Β· 1.1 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package clean
import (
"strings"
"unicode"
"github.com/photoprism/photoprism/pkg/txt"
)
// State returns the full, normalized state name.
func State(s, countryCode string) string {
if s == "" || reject(s, txt.ClipName) {
return Empty
}
// Remove whitespace from name.
s = strings.TrimSpace(s)
// Empty?
if s == "" || s == txt.UnknownStateCode {
// State doesn't have a name.
return ""
}
// Remove non-printable and other potentially problematic characters.
s = strings.Map(func(r rune) rune {
if !unicode.IsPrint(r) {
return -1
}
switch r {
case '~', '\\', ':', '|', '"', '?', '*', '<', '>', '{', '}':
return -1
default:
return r
}
}, s)
// 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.
} else if normalized, found := states[s]; !found {
// Unknown abbreviation.
} else if normalized != "" {
// Yes, use normalized name.
s = normalized
}
// Return normalized state name.
return s
}