|
| 1 | +package stringFormatter |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "unicode" |
| 6 | +) |
| 7 | + |
| 8 | +type FormattingStyle string |
| 9 | +type CaseSetting int |
| 10 | + |
| 11 | +const ( |
| 12 | + Camel FormattingStyle = "camel" |
| 13 | + Snake FormattingStyle = "snake" |
| 14 | + Kebab FormattingStyle = "kebab" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + ToUpper CaseSetting = 1 |
| 19 | + ToLower CaseSetting = 2 |
| 20 | + NoChanges CaseSetting = 3 |
| 21 | +) |
| 22 | + |
| 23 | +type styleInc struct { |
| 24 | + Index int |
| 25 | + Style FormattingStyle |
| 26 | +} |
| 27 | + |
| 28 | +var styleSigns = map[rune]FormattingStyle{ |
| 29 | + '_': Snake, |
| 30 | + '-': Kebab, |
| 31 | +} |
| 32 | + |
| 33 | +// SetStyle is a function that converts text with code to defined code style. |
| 34 | +/* Set text like a code style to on from FormattingStyle (Camel, Snake, or Kebab) |
| 35 | + * conversion of abbreviations like JSON, USB, and so on is going like a regular text |
| 36 | + * for current version, therefore they these abbreviations could be in a different |
| 37 | + * case after conversion. |
| 38 | + * Case settings apply in the following order : 1 - textCase, 2 - firstSymbol. |
| 39 | + * If you are not applying textCase to text converting from Camel to Snake or Kebab |
| 40 | + * result is lower case styled text. textCase does not apply to Camel style. |
| 41 | + * Parameters: |
| 42 | + * - text - pointer to text |
| 43 | + * - style - new code style |
| 44 | + * - firstSymbol - case settings for first symbol |
| 45 | + * - textCase - case settings for whole text except first symbol |
| 46 | + * Returns : new string with formatted line |
| 47 | + */ |
| 48 | +func SetStyle(text *string, style FormattingStyle, firstSymbol CaseSetting, textCase CaseSetting) string { |
| 49 | + if text == nil { |
| 50 | + return "" |
| 51 | + } |
| 52 | + sb := strings.Builder{} |
| 53 | + sb.Grow(len(*text)) |
| 54 | + stats := defineFormattingStyle(text) |
| 55 | + startIndex := 0 |
| 56 | + endIndex := 0 |
| 57 | + // todo UMV think how to process .... |
| 58 | + // we could have many stats at the same time, probably we should use some config in the future |
| 59 | + // iterate over the map |
| 60 | + for _, v := range stats { |
| 61 | + endIndex = v.Index |
| 62 | + if endIndex < startIndex { |
| 63 | + continue |
| 64 | + } |
| 65 | + sb.WriteString((*text)[startIndex:endIndex]) |
| 66 | + startIndex = v.Index |
| 67 | + |
| 68 | + switch style { |
| 69 | + case Kebab: |
| 70 | + sb.WriteString("-") |
| 71 | + break |
| 72 | + case Snake: |
| 73 | + sb.WriteString("_") |
| 74 | + break |
| 75 | + case Camel: |
| 76 | + // in case of convert to Camel we should skip v.Index (because it is _ or -) |
| 77 | + if v.Style == Camel { |
| 78 | + sb.WriteRune(unicode.ToUpper(rune((*text)[endIndex]))) |
| 79 | + } else { |
| 80 | + sb.WriteRune(unicode.ToUpper(rune((*text)[endIndex+1]))) |
| 81 | + } |
| 82 | + startIndex += 1 |
| 83 | + break |
| 84 | + } |
| 85 | + if v.Style != Camel { |
| 86 | + startIndex += 1 |
| 87 | + } |
| 88 | + } |
| 89 | + sb.WriteString((*text)[startIndex:]) |
| 90 | + result := strings.Builder{} |
| 91 | + if style != Camel { |
| 92 | + switch textCase { |
| 93 | + case ToUpper: |
| 94 | + result.WriteString(strings.ToUpper(sb.String()[1:])) |
| 95 | + break |
| 96 | + case ToLower: |
| 97 | + result.WriteString(strings.ToLower(sb.String()[1:])) |
| 98 | + break |
| 99 | + case NoChanges: |
| 100 | + result.WriteString(sb.String()[1:]) |
| 101 | + break |
| 102 | + } |
| 103 | + } else { |
| 104 | + result.WriteString(sb.String()[1:]) |
| 105 | + } |
| 106 | + |
| 107 | + switch firstSymbol { |
| 108 | + case ToUpper: |
| 109 | + return strings.ToUpper(sb.String()[:1]) + result.String() |
| 110 | + case ToLower: |
| 111 | + return strings.ToLower(sb.String()[:1]) + result.String() |
| 112 | + case NoChanges: |
| 113 | + return sb.String()[:1] + result.String() |
| 114 | + default: |
| 115 | + return sb.String()[:1] + result.String() |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// GetFormattingStyleOptions function that defines formatting style, case of first char and result from string |
| 120 | +/* |
| 121 | + * |
| 122 | + */ |
| 123 | +func GetFormattingStyleOptions(style string) (FormattingStyle, CaseSetting, CaseSetting) { |
| 124 | + styleLower := strings.ToLower(style) |
| 125 | + var formattingStyle FormattingStyle |
| 126 | + firstSymbolCase := ToLower |
| 127 | + textCase := NoChanges |
| 128 | + switch styleLower { |
| 129 | + case string(Camel): |
| 130 | + formattingStyle = Camel |
| 131 | + break |
| 132 | + case string(Snake): |
| 133 | + formattingStyle = Snake |
| 134 | + break |
| 135 | + case string(Kebab): |
| 136 | + formattingStyle = Kebab |
| 137 | + break |
| 138 | + } |
| 139 | + |
| 140 | + runes := []rune(style) |
| 141 | + firstSymbolIsUpper := isSymbolIsUpper(runes[0]) |
| 142 | + if firstSymbolIsUpper { |
| 143 | + firstSymbolCase = ToUpper |
| 144 | + } |
| 145 | + |
| 146 | + if formattingStyle != Camel { |
| 147 | + allSymbolsUpperCase := isStringIsUpper(runes) |
| 148 | + if allSymbolsUpperCase { |
| 149 | + textCase = ToUpper |
| 150 | + } else { |
| 151 | + textCase = ToLower |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return formattingStyle, firstSymbolCase, textCase |
| 156 | +} |
| 157 | + |
| 158 | +// defineFormattingStyle |
| 159 | +/* This function defines what formatting style is using in text |
| 160 | + * If there are no transitions between symbols then here we have NoFormatting style |
| 161 | + * Didn't decide yet what to do if we are having multiple signatures |
| 162 | + * i.e. multiple_signs-at-sameTime . |
| 163 | + * Parameters: |
| 164 | + * - text - a sequence of symbols to check |
| 165 | + * Returns: formatting style using in the text |
| 166 | + */ |
| 167 | +func defineFormattingStyle(text *string) []styleInc { |
| 168 | + // symbol analyze, for camel case pattern -> aA, for kebab -> a-a, for snake -> a_a |
| 169 | + inclusions := make([]styleInc, 0) |
| 170 | + runes := []rune(*text) |
| 171 | + for pos, char := range runes { |
| 172 | + // define style and add stats |
| 173 | + style, ok := styleSigns[char] |
| 174 | + if !ok { |
| 175 | + // 1. Probably current symbol is not a sign and we should continue |
| 176 | + if pos > 0 && pos < len(runes)-1 { |
| 177 | + charIsUpperCase := isSymbolIsUpper(char) |
| 178 | + prevChar := runes[pos-1] |
| 179 | + prevCharIsUpperCase := unicode.IsUpper(prevChar) |
| 180 | + if charIsUpperCase && !prevCharIsUpperCase { |
| 181 | + style = Camel |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + if style != "" { |
| 186 | + inclusions = append(inclusions, styleInc{Index: pos, Style: style}) |
| 187 | + } |
| 188 | + } |
| 189 | + return inclusions |
| 190 | +} |
| 191 | + |
| 192 | +func isSymbolIsUpper(symbol rune) bool { |
| 193 | + return unicode.IsUpper(symbol) && unicode.IsLetter(symbol) |
| 194 | +} |
| 195 | + |
| 196 | +func isStringIsUpper(str []rune) bool { |
| 197 | + isUpper := true |
| 198 | + for _, r := range str { |
| 199 | + if unicode.IsLetter(r) { |
| 200 | + isUpper = isUpper && unicode.IsUpper(r) |
| 201 | + } |
| 202 | + } |
| 203 | + return isUpper |
| 204 | +} |
0 commit comments