You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sonic/util/common.go

60 lines
1.3 KiB
Go

package util
import (
"errors"
"github.com/go-playground/validator/v10"
"github.com/go-sonic/sonic/handler/trans"
"github.com/go-sonic/sonic/util/xerr"
"regexp"
"strings"
"unicode/utf8"
)
func IfElse(condition bool, a, b interface{}) interface{} {
if condition {
return a
}
return b
}
func CompositeURL(urls ...string) string {
builder := strings.Builder{}
for i, url := range urls {
if url == "" {
continue
}
url = strings.TrimSuffix(url, "/")
url = strings.TrimPrefix(url, "/")
if i != 0 {
builder.WriteString("/")
}
builder.WriteString(url)
}
return builder.String()
}
var htmlRegexp = regexp.MustCompile(`(<[^<]*?>)|(<[\s]*?/[^<]*?>)|(<[^<]*?/[\s]*?>)`)
func CleanHTMLTag(htmlContent string) string {
if htmlContent == "" {
return ""
}
return htmlRegexp.ReplaceAllString(htmlContent, "")
}
var blankRegexp = regexp.MustCompile(`\s`)
func HTMLFormatWordCount(html string) int64 {
text := CleanHTMLTag(html)
return int64(utf8.RuneCountInString(text) - len(blankRegexp.FindSubmatchIndex(StringToBytes(text))))
}
func WrapJsonBindErr(err error) error {
e := validator.ValidationErrors{}
if errors.As(err, &e) {
return xerr.WithStatus(e, xerr.StatusBadRequest).WithMsg(trans.Translate(e))
}
return xerr.WithStatus(err, xerr.StatusBadRequest)
}