github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/util/strings/strings.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package strings
    18  
    19  import (
    20  	"golang.org/x/text/cases"
    21  	"golang.org/x/text/language"
    22  	"strings"
    23  )
    24  
    25  var uppercaseAcronym = map[string]string{
    26  	"ID": "id",
    27  }
    28  
    29  // ToCamelCase Converts a string to CamelCase
    30  func toCamelCase(s string, initCase bool) string {
    31  	s = strings.TrimSpace(s)
    32  	if s == "" {
    33  		return s
    34  	}
    35  	if a, ok := uppercaseAcronym[s]; ok {
    36  		s = a
    37  	}
    38  
    39  	n := strings.Builder{}
    40  	n.Grow(len(s))
    41  	capNext := initCase
    42  	for i, v := range []byte(s) {
    43  		vIsCap := v >= 'A' && v <= 'Z'
    44  		vIsLow := v >= 'a' && v <= 'z'
    45  		if capNext {
    46  			if vIsLow {
    47  				v += 'A'
    48  				v -= 'a'
    49  			}
    50  		} else if i == 0 {
    51  			if vIsCap {
    52  				v += 'a'
    53  				v -= 'A'
    54  			}
    55  		}
    56  		if vIsCap || vIsLow {
    57  			n.WriteByte(v)
    58  			capNext = false
    59  		} else if vIsNum := v >= '0' && v <= '9'; vIsNum {
    60  			n.WriteByte(v)
    61  			capNext = true
    62  		} else {
    63  			capNext = v == '_' || v == ' ' || v == '-' || v == '.'
    64  		}
    65  	}
    66  	return n.String()
    67  }
    68  
    69  // ToCamel converts a string to CamelCase
    70  func ToCamel(s string) string {
    71  	return toCamelCase(s, true)
    72  }
    73  
    74  // ToLowerCamel converts a string to lowerCamelCase
    75  func ToLowerCamel(s string) string {
    76  	return toCamelCase(s, false)
    77  }
    78  
    79  func Title(s string) string {
    80  	return cases.Title(language.English).String(s)
    81  }
    82  
    83  func ToUpper(s string) string {
    84  	return strings.ToUpper(s)
    85  }
    86  
    87  func ToLower(s string) string {
    88  	return strings.ToLower(s)
    89  }
    90  
    91  func GitParse(s string) string {
    92  	list := strings.Split(s, "/")
    93  	return strings.Join(list[:2], "/")
    94  }
    95  
    96  func DeHyphenFunc(s string) string {
    97  	return strings.ReplaceAll(s, "-", "")
    98  }
    99  
   100  func LowerHyphenFunc(s string) string {
   101  	return strings.ReplaceAll(s, "-", "_")
   102  }
   103  
   104  func ToHyphen(s string) string {
   105  	return strings.ReplaceAll(s, "-", "_")
   106  }