...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text/text.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package text
    16  
    17  import (
    18  	"regexp"
    19  	"strings"
    20  	"unicode"
    21  )
    22  
    23  // TODO: there must be some package out there that does this well
    24  func Pluralize(singular string) string {
    25  	var plural string
    26  	if strings.HasSuffix(singular, "y") {
    27  		if strings.HasSuffix(singular, "ay") || strings.HasSuffix(singular, "ey") {
    28  			plural = singular + "s"
    29  		} else {
    30  			plural = singular[:len(singular)-1] + "ies"
    31  		}
    32  	} else if strings.HasSuffix(singular, "s") || strings.HasSuffix(singular, "x") || strings.HasSuffix(singular, "sh") {
    33  		plural = singular + "es"
    34  	} else if singular != "" {
    35  		plural = singular + "s"
    36  	}
    37  	return plural
    38  }
    39  
    40  func SnakeCaseStrsToLowerCamelCaseStrs(strs []string) []string {
    41  	out := make([]string, 0)
    42  	for _, s := range strs {
    43  		out = append(out, SnakeCaseToLowerCamelCase(s))
    44  	}
    45  	return out
    46  }
    47  
    48  func SnakeCaseToLowerCamelCase(s string) string {
    49  	return snakeCaseToUpperCamelCase(s)
    50  }
    51  
    52  func SnakeCaseToUpperCamelCase(s string) string {
    53  	return strings.Title(snakeCaseToUpperCamelCase(s))
    54  }
    55  
    56  // Convert a snake_case_string to CamelCaseString.
    57  func snakeCaseToUpperCamelCase(s string) string {
    58  	split := strings.Split(s, "_")
    59  	ret := ""
    60  	for i, v := range split {
    61  		if i == 0 {
    62  			ret += v
    63  		} else {
    64  			ret += strings.Title(v)
    65  		}
    66  	}
    67  	return ret
    68  }
    69  
    70  func SnakeCaseToLowerCase(s string) string {
    71  	split := strings.Split(s, "_")
    72  	ret := ""
    73  	for _, v := range split {
    74  		ret += v
    75  	}
    76  	return ret
    77  }
    78  
    79  // AsSnakeCase returns the given string converted to lowercase snake_case. If the input is already snake_case, no
    80  // change is made. Any transitions in the input from lowercase to uppercase are interpreted as camelCase-style word
    81  // transitions, and are replaced with an underscore.
    82  func AsSnakeCase(s string) string {
    83  	res := regexp.MustCompile("(.)([A-Z][a-z]+)").ReplaceAllString(s, "${1}_${2}")
    84  	return strings.ToLower(regexp.MustCompile("([a-z0-9])([A-Z])").ReplaceAllString(res, "${1}_${2}"))
    85  }
    86  
    87  // Convert a CamelCaseString to kebab-case-string.
    88  func CamelCaseToKebabCase(s string) string {
    89  	kebabed := ""
    90  	for i, r := range s {
    91  		if i != 0 && i != len(s)-1 && unicode.IsUpper(r) {
    92  			kebabed += "-"
    93  		}
    94  		kebabed += strings.ToLower(string(r))
    95  	}
    96  	return kebabed
    97  }
    98  
    99  // Convert a kebab-case-string to lower_snake_case string.
   100  func KebabCaseToLowerSnakeCase(s string) string {
   101  	return strings.ToLower(strings.ReplaceAll(s, "-", "_"))
   102  }
   103  
   104  func SnakeCaseToKebabCase(s string) string {
   105  	return strings.ReplaceAll(s, "_", "-")
   106  }
   107  
   108  func BeginsWithVowel(s string) bool {
   109  	if s == "" {
   110  		return false
   111  	}
   112  	switch strings.ToLower(string(s[0])) {
   113  	case "a", "e", "i", "o", "u":
   114  		return true
   115  	default:
   116  		return false
   117  	}
   118  }
   119  
   120  func UppercaseInitial(str string) string {
   121  	for i, v := range str {
   122  		return string(unicode.ToUpper(v)) + str[i+1:]
   123  	}
   124  	return ""
   125  }
   126  
   127  func LowercaseInitial(str string) string {
   128  	for i, v := range str {
   129  		return string(unicode.ToLower(v)) + str[i+1:]
   130  	}
   131  	return ""
   132  }
   133  
   134  func IndefiniteArticleFor(s string) string {
   135  	if BeginsWithVowel(s) {
   136  		return "an"
   137  	}
   138  	return "a"
   139  }
   140  
   141  func AppendStrAsNewParagraph(base, str string) string {
   142  	if base == "" {
   143  		return str
   144  	}
   145  	return base + "\n\n" + str
   146  }
   147  
   148  func IsPascalCase(s string) bool {
   149  	sampleRegex := regexp.MustCompile("^[A-Z][a-z]*([A-Z][a-z]*)*$")
   150  	return sampleRegex.Match([]byte(s))
   151  }
   152  
   153  func IsSnakeCase(s string) bool {
   154  	stringRegex := regexp.MustCompile("^[a-z1-9]+(_[a-z1-9]+)*$")
   155  	return stringRegex.Match([]byte(s))
   156  }
   157  
   158  func RemoveSpecialCharacters(s string) string {
   159  	specialCharRegex := regexp.MustCompile(`[^0-9A-Za-z ]+`)
   160  	return specialCharRegex.ReplaceAllString(s, "")
   161  }
   162  

View as plain text