gotools/formatting/stringtools.go

54 lines
895 B
Go
Raw Normal View History

2018-12-19 14:45:37 +01:00
package formatting
import (
"encoding/json"
"fmt"
"strings"
)
// PrettyPrint prints prettier
func PrettyPrint(v interface{}) (err error) {
b, err := json.MarshalIndent(v, "", " ")
if err == nil {
fmt.Println(string(b))
}
return
}
func trimLeadingSlash(s string) string {
if string(s[0]) == "/" {
return s[1:]
}
return s
}
func trimLastSlash(s string) string {
if string(s[len(s)-1]) == "/" {
return s[:len(s)-2]
}
return s
}
func trimSlashes(s string) string {
s = trimLastSlash(s)
s = trimLeadingSlash(s)
return s
}
func splitPath(path pathToDoc) (string, pathToDoc) {
res := strings.SplitN(path.path, "/", 2)
if len(res) > 1 {
return res[0], pathToDoc{res[1], path.doc}
}
return res[0], pathToDoc{"", path.doc}
}
func stringMissingInSlice(a string, list []pathToDoc) bool {
for _, b := range list {
if b.path == a {
return false
}
}
return true
}