github.com/wfusion/gofusion@v1.1.14/common/utils/string.go (about)

     1  package utils
     2  
     3  import (
     4  	"strings"
     5  	"unicode"
     6  
     7  	"github.com/iancoleman/strcase"
     8  	"github.com/wfusion/gofusion/common/constant"
     9  	"golang.org/x/text/cases"
    10  	"golang.org/x/text/language"
    11  )
    12  
    13  func IsStrBlank(s string) bool {
    14  	return strings.IndexFunc(s, func(r rune) bool { return !(unicode.IsSpace(r)) }) < 0
    15  }
    16  
    17  func IsStrPtrBlank(s *string) bool {
    18  	return s == nil || IsStrBlank(*s)
    19  }
    20  
    21  func IsStrNotBlank(s string) bool {
    22  	return !IsStrBlank(s)
    23  }
    24  
    25  func IsStrPtrNotBlank(s *string) bool {
    26  	return s != nil && IsStrNotBlank(*s)
    27  }
    28  
    29  var (
    30  	keywordFuzzyDelimited = []string{
    31  		constant.Space,
    32  		constant.Colon,
    33  		constant.Hyphen,
    34  		constant.Underline,
    35  	}
    36  )
    37  
    38  func FuzzyKeyword(keyword string) []string {
    39  	words := strings.Fields(constant.NonNumberLetterReg.ReplaceAllString(keyword, " "))
    40  	compact := strings.Join(words, "")
    41  	lowerWords := SliceMapping(words, func(s string) string { return strings.ToLower(s) })
    42  	upperWords := SliceMapping(words, func(s string) string { return strings.ToUpper(s) })
    43  	titleWords := SliceMapping(words, func(s string) string { return cases.Title(language.English).String(s) })
    44  
    45  	s := NewSet(keyword)
    46  	s.Insert(
    47  		compact,
    48  		strings.ToUpper(compact),
    49  		strings.ToLower(compact),
    50  		strcase.ToCamel(keyword),
    51  		strcase.ToLowerCamel(keyword),
    52  		strcase.ToKebab(keyword),
    53  		strcase.ToSnake(keyword),
    54  		strcase.ToScreamingSnake(keyword),
    55  		strcase.ToScreamingKebab(keyword),
    56  	)
    57  	for _, delimited := range keywordFuzzyDelimited {
    58  		s.Insert(
    59  			strings.Join(lowerWords, delimited),
    60  			strings.Join(upperWords, delimited),
    61  			strings.Join(titleWords, delimited),
    62  		)
    63  	}
    64  
    65  	return s.Items()
    66  }
    67  
    68  func init() {
    69  	strcase.ConfigureAcronym("I18n", "i18n")
    70  }