github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/util/acronym.go (about)

     1  // Package util - Content managed by Project Forge, see [projectforge.md] for details.
     2  package util
     3  
     4  import (
     5  	"strings"
     6  	"unicode"
     7  
     8  	"github.com/iancoleman/strcase"
     9  	"github.com/pkg/errors"
    10  	"github.com/samber/lo"
    11  )
    12  
    13  func StringToCamel(s string, extraAcronyms ...string) string {
    14  	return acr(strcase.ToCamel(s), extraAcronyms...)
    15  }
    16  
    17  func StringToTitle(s string, extraAcronyms ...string) string {
    18  	ret := strings.Builder{}
    19  	runes := []rune(StringToCamel(s, extraAcronyms...))
    20  	lo.ForEach(runes, func(c rune, idx int) {
    21  		if idx > 0 && idx < len(runes)-1 && unicode.IsUpper(c) {
    22  			if !unicode.IsUpper(runes[idx+1]) {
    23  				ret.WriteRune(' ')
    24  			} else if !unicode.IsUpper(runes[idx-1]) {
    25  				ret.WriteRune(' ')
    26  			}
    27  		}
    28  		ret.WriteRune(c)
    29  	})
    30  	return ret.String()
    31  }
    32  
    33  func StringToLowerCamel(s string, extraAcronyms ...string) string {
    34  	return acr(strcase.ToLowerCamel(s), extraAcronyms...)
    35  }
    36  
    37  func StringToSnake(s string, extraAcronyms ...string) string {
    38  	return acr(strcase.ToSnake(s), extraAcronyms...)
    39  }
    40  
    41  var acronyms []string
    42  
    43  func InitAcronyms(extras ...string) error {
    44  	if len(acronyms) > 0 {
    45  		return errors.New("double initialization of acronyms")
    46  	}
    47  	x := []string{"Api", "Html", "Id", "Ip", "Json", "Sql", "Xml", "Uri", "Url"}
    48  	x = append(x, lo.Map(extras, func(s string, _ int) string {
    49  		return strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
    50  	})...)
    51  	lo.ForEach(x, func(x string, _ int) {
    52  		strcase.ConfigureAcronym(strings.ToUpper(x), strings.ToLower(x))
    53  	})
    54  	acronyms = x
    55  	return nil
    56  }
    57  
    58  func acr(ret string, extraAcronyms ...string) string {
    59  	proc := func(a string) {
    60  		var lastIdx int
    61  		for {
    62  			i := strings.Index(ret[lastIdx:], a)
    63  			if i == -1 {
    64  				if strings.EqualFold(a, ret) && unicode.IsUpper(rune(ret[0])) {
    65  					ret = strings.ToUpper(a)
    66  				}
    67  				break
    68  			}
    69  			i += lastIdx
    70  			lastIdx = i + len(a)
    71  			if lastIdx >= len(ret) {
    72  				ret = ret[:i] + strings.ToUpper(a) + ret[lastIdx:]
    73  			} else {
    74  				s := string(ret[lastIdx])
    75  				if strings.ToUpper(s) == s {
    76  					ret = ret[:i] + strings.ToUpper(a) + ret[lastIdx:]
    77  				} else {
    78  					lastIdx++
    79  				}
    80  			}
    81  		}
    82  	}
    83  	lo.ForEach(acronyms, func(a string, _ int) {
    84  		proc(a)
    85  	})
    86  	lo.ForEach(extraAcronyms, func(a string, _ int) {
    87  		proc(a)
    88  	})
    89  	return ret
    90  }