1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 19:56:16 +00:00
mycorrhiza/util/util.go
2020-07-04 00:20:56 +05:00

50 lines
871 B
Go

package util
import (
"strings"
"unicode"
)
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
}
func UrlToCanonical(name string) string {
return removeColonPerhaps(
strings.ToLower(strings.ReplaceAll(name, " ", "_")))
}
func DisplayToCanonical(name string) string {
return removeColonPerhaps(
strings.ToLower(strings.ReplaceAll(name, " ", "_")))
}
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)
}
return addColonPerhaps(res)
}