12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package utils
- import (
- "fmt"
- "github.com/mozillazg/go-pinyin"
- "strings"
- "unicode"
- )
- func GenerateIndexCode(sourceName string, indexName string) string {
-
- firstLetters := getFirstLetters(indexName)
-
- indexCode := fmt.Sprintf("%s%s", sourceName, firstLetters)
- return indexCode
- }
- func getFirstLetters(input string) string {
-
- args := pinyin.NewArgs()
- args.Style = pinyin.FirstLetter
-
- var result strings.Builder
-
- for _, r := range input {
- if unicode.IsDigit(r) || unicode.IsUpper(r) {
-
- result.WriteRune(r)
- } else if unicode.Is(unicode.Han, r) {
-
- py := pinyin.Pinyin(string(r), args)
- if len(py) > 0 && len(py[0]) > 0 {
- result.WriteString(py[0][0])
- }
- }
-
- }
- return result.String()
- }
|