enhance(utils):将转换拼音所写的功能提取到公共工具函数。

This commit is contained in:
徐涛
2022-08-15 17:10:25 +08:00
parent 224ae9b07d
commit 83f19efecf
2 changed files with 17 additions and 10 deletions

View File

@@ -1,5 +1,11 @@
package utils
import (
"strings"
"github.com/mozillazg/go-pinyin"
)
func Contains[T string | int | uint](element T, slice []T) bool {
for _, v := range slice {
if v == element {
@@ -8,3 +14,13 @@ func Contains[T string | int | uint](element T, slice []T) bool {
}
return false
}
func PinyinAbbr(source string) string {
abbr := pinyin.Pinyin(source, pinyin.NewArgs())
var abbrCollect = make([]string, 0)
for _, a := range abbr {
abbrCollect = append(abbrCollect, a[0][0:1])
}
finalAbbr := strings.Join(abbrCollect, "")
return finalAbbr
}