2022-08-28 13:27:41 +00:00
|
|
|
package helper
|
|
|
|
|
2022-09-08 02:54:37 +00:00
|
|
|
import (
|
2022-09-22 09:21:41 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2022-09-23 13:46:34 +00:00
|
|
|
"github.com/dlclark/regexp2"
|
2022-09-22 09:21:41 +00:00
|
|
|
"io"
|
2022-09-08 02:54:37 +00:00
|
|
|
"reflect"
|
2022-09-23 13:46:34 +00:00
|
|
|
"regexp"
|
2022-09-16 10:21:54 +00:00
|
|
|
"strings"
|
2022-09-08 02:54:37 +00:00
|
|
|
)
|
|
|
|
|
2022-08-28 13:27:41 +00:00
|
|
|
func IsContainInArr[T comparable](a T, arr []T) bool {
|
|
|
|
for _, v := range arr {
|
|
|
|
if a == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-09-08 02:54:37 +00:00
|
|
|
|
|
|
|
func StructColumn[T any, M any](arr []M, field string) (r []T) {
|
|
|
|
for i := 0; i < len(arr); i++ {
|
|
|
|
v := reflect.ValueOf(arr[i]).FieldByName(field).Interface()
|
|
|
|
if val, ok := v.(T); ok {
|
|
|
|
r = append(r, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
|
|
|
|
func RangeSlice[T ~int | ~uint | ~int64 | ~int8 | ~int16 | ~int32 | ~uint64](start, end, step T) []T {
|
2022-09-20 13:16:51 +00:00
|
|
|
r := make([]T, 0, int((end-start+1)/step+1))
|
2022-09-15 14:35:32 +00:00
|
|
|
for i := start; i <= end; {
|
|
|
|
r = append(r, i)
|
|
|
|
i = i + step
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2022-09-16 10:21:54 +00:00
|
|
|
|
|
|
|
func StrJoin(s ...string) (str string) {
|
|
|
|
if len(s) == 1 {
|
|
|
|
return s[0]
|
|
|
|
} else if len(s) > 1 {
|
|
|
|
b := strings.Builder{}
|
|
|
|
for _, s2 := range s {
|
|
|
|
b.WriteString(s2)
|
|
|
|
}
|
|
|
|
str = b.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-09-20 13:16:51 +00:00
|
|
|
|
|
|
|
func SlicePagination[T any](arr []T, page, pageSize int) []T {
|
|
|
|
start := (page - 1) * pageSize
|
|
|
|
l := len(arr)
|
|
|
|
if start > l {
|
|
|
|
start = l
|
|
|
|
}
|
|
|
|
end := page * pageSize
|
|
|
|
if l < end {
|
|
|
|
end = l
|
|
|
|
}
|
|
|
|
return arr[start:end]
|
|
|
|
}
|
2022-09-22 09:21:41 +00:00
|
|
|
|
|
|
|
func StringMd5(str string) string {
|
|
|
|
h := md5.New()
|
2022-09-23 13:46:34 +00:00
|
|
|
_, err := io.WriteString(h, str)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-09-22 09:21:41 +00:00
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
2022-09-23 13:46:34 +00:00
|
|
|
|
|
|
|
var allHtmlTag = regexp.MustCompile("</?.*>")
|
|
|
|
|
|
|
|
func StripTags(str, allowable string) string {
|
|
|
|
html := ""
|
|
|
|
if allowable == "" {
|
|
|
|
return allHtmlTag.ReplaceAllString(str, "")
|
|
|
|
}
|
|
|
|
r := strings.Split(allowable, ">")
|
|
|
|
re := ""
|
|
|
|
for _, reg := range r {
|
|
|
|
if reg == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tag := strings.TrimLeft(reg, "<")
|
|
|
|
ree := fmt.Sprintf(`%s|\/%s`, tag, tag)
|
|
|
|
re = fmt.Sprintf("%s|%s", re, ree)
|
|
|
|
}
|
|
|
|
ree := strings.Trim(re, "|")
|
|
|
|
reg := fmt.Sprintf("<(?!%s).*?>", ree)
|
|
|
|
compile, err := regexp2.Compile(reg, regexp2.IgnoreCase)
|
|
|
|
if err != nil {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
html, err = compile.Replace(str, "", 0, -1)
|
|
|
|
if err != nil {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
2022-09-24 09:52:06 +00:00
|
|
|
|
|
|
|
var tag = regexp.MustCompile(`<(.*?)>`)
|
|
|
|
|
|
|
|
func StripTagsX(str, allowable string) string {
|
|
|
|
if allowable == "" {
|
|
|
|
return allHtmlTag.ReplaceAllString(str, "")
|
|
|
|
}
|
|
|
|
tags := tag.ReplaceAllString(allowable, "$1|")
|
|
|
|
or := strings.TrimRight(tags, "|")
|
|
|
|
reg := fmt.Sprintf(`<(/?(%s).*?)>`, or)
|
|
|
|
regx := fmt.Sprintf(`\{\[(/?(%s).*?)\]\}`, or)
|
|
|
|
cp, err := regexp.Compile(reg)
|
|
|
|
if err != nil {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
rep := cp.ReplaceAllString(str, "{[$1]}")
|
|
|
|
tmp := tag.ReplaceAllString(rep, "")
|
|
|
|
rex, err := regexp.Compile(regx)
|
|
|
|
if err != nil {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
html := rex.ReplaceAllString(tmp, "<$1>")
|
|
|
|
return html
|
|
|
|
}
|
2022-09-24 13:42:55 +00:00
|
|
|
|
|
|
|
var tagx = regexp.MustCompile(`(</?[a-z0-9]+?)( |>)`)
|
|
|
|
var selfCloseTags = []string{"area", "base", "basefont", "br", "col", "command", "embed", "frame", "hr", "img", "input", "isindex", "link", "meta", "param", "source", "track", "wbr"}
|
|
|
|
|
|
|
|
func CloseHtmlTag(str string) string {
|
|
|
|
tags := tag.FindAllString(str, -1)
|
|
|
|
if len(tags) < 1 {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
var tagss = make([]string, 0, len(tags))
|
|
|
|
for _, s := range tags {
|
|
|
|
ss := strings.TrimSpace(tagx.FindString(s))
|
|
|
|
if ss[len(ss)-1] != '>' {
|
|
|
|
ss = fmt.Sprintf("%s>", ss)
|
|
|
|
if IsContainInArr(ss, selfCloseTags) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tagss = append(tagss, ss)
|
|
|
|
}
|
|
|
|
r := SliceMap[string, string](ClearClosedTag(tagss), func(s string) string {
|
|
|
|
return fmt.Sprintf("</%s>", strings.Trim(s, "<>"))
|
|
|
|
})
|
|
|
|
return strings.Join(r, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClearClosedTag(s []string) []string {
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if len(s[i:]) < 2 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
l := s[i]
|
|
|
|
r := fmt.Sprintf(`</%s>`, strings.Trim(l, "<>"))
|
|
|
|
if s[i+1] == r {
|
|
|
|
if len(s[i+1:]) > 1 {
|
|
|
|
ss := s[:i]
|
|
|
|
s = append(ss, s[i+2:]...)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
s = s[:i]
|
|
|
|
}
|
|
|
|
i = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SliceMap[T any, R any](arr []T, fun func(T) R) []R {
|
|
|
|
r := make([]R, 0, len(arr))
|
|
|
|
for _, t := range arr {
|
|
|
|
r = append(r, fun(t))
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|