github.com/avicd/go-utilx@v0.1.0/conv/string.go (about)

     1  package conv
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  	"unicode"
     8  )
     9  
    10  const (
    11  	Date              = "2006-01-02"
    12  	DateTime          = "2006-01-02 15:04:05"
    13  	DateTimeMirco     = "2006-01-02 15:04:05.000000"
    14  	DateTimeMircoZone = "2006-01-02 15:04:05.000000Z07:00"
    15  	DateTimeMilli     = "2006-01-02 15:04:05.000"
    16  	DateTimeMilliZone = "2006-01-02 15:04:05.000Z07:00"
    17  )
    18  
    19  func CamelCase(text string) string {
    20  	return regexp.MustCompile("[:\\-_]+[a-zA-z]").
    21  		ReplaceAllStringFunc(text, func(s string) string {
    22  			return strings.ToUpper(s[len(s)-1:])
    23  		})
    24  }
    25  
    26  func BigCamelCase(text string) string {
    27  	return FirstUpper(CamelCase(text))
    28  }
    29  
    30  func FirstUpper(text string) string {
    31  	if len(text) < 1 {
    32  		return text
    33  	}
    34  	return strings.ToUpper(text[0:1]) + text[1:]
    35  }
    36  
    37  func UnderLineCase(text string) string {
    38  	return strings.ToLower(regexp.MustCompile("[a-z][A-Z]").
    39  		ReplaceAllStringFunc(text, func(s string) string {
    40  			return s[:1] + "_" + s[1:]
    41  		}))
    42  }
    43  
    44  func Append(dest string, src any, delimiter ...string) string {
    45  	if dest == "" {
    46  		return fmt.Sprint(src)
    47  	}
    48  	var sep string
    49  	if len(delimiter) > 0 {
    50  		sep = delimiter[0]
    51  	}
    52  	return fmt.Sprint(dest, sep, src)
    53  }
    54  
    55  func StrToArr(text string, delimiter string) []string {
    56  	if text == "" {
    57  		return nil
    58  	}
    59  	return strings.Split(text, delimiter)
    60  }
    61  
    62  func IsDigit(text string) bool {
    63  	return strings.IndexFunc(text, func(r rune) bool {
    64  		return !unicode.IsDigit(r)
    65  	}) < 0
    66  }