2020-06-30 18:13:46 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2020-07-03 19:20:56 +00:00
|
|
|
func addColonPerhaps(name string) string {
|
|
|
|
if strings.HasPrefix(name, ":") {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return ":" + name
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeColonPerhaps(name string) string {
|
|
|
|
if strings.HasPrefix(name, ":") {
|
|
|
|
return name[1:]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-06-30 18:13:46 +00:00
|
|
|
func UrlToCanonical(name string) string {
|
2020-07-03 19:20:56 +00:00
|
|
|
return removeColonPerhaps(
|
|
|
|
strings.ToLower(strings.ReplaceAll(name, " ", "_")))
|
2020-06-30 18:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DisplayToCanonical(name string) string {
|
2020-07-03 19:20:56 +00:00
|
|
|
return removeColonPerhaps(
|
|
|
|
strings.ToLower(strings.ReplaceAll(name, " ", "_")))
|
2020-06-30 18:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CanonicalToDisplay(name string) (res string) {
|
|
|
|
tmp := strings.Title(name)
|
|
|
|
var afterPoint bool
|
|
|
|
for _, ch := range tmp {
|
|
|
|
if afterPoint {
|
|
|
|
afterPoint = false
|
|
|
|
ch = unicode.ToLower(ch)
|
|
|
|
}
|
|
|
|
switch ch {
|
|
|
|
case '.':
|
|
|
|
afterPoint = true
|
|
|
|
case '_':
|
|
|
|
ch = ' '
|
|
|
|
}
|
|
|
|
res += string(ch)
|
|
|
|
}
|
2020-07-03 19:20:56 +00:00
|
|
|
return addColonPerhaps(res)
|
2020-06-30 18:13:46 +00:00
|
|
|
}
|