MCPcopy Create free account
hub / github.com/goadesign/goa / SnakeCase

Function SnakeCase

codegen/funcs.go:202–243  ·  view source on GitHub ↗

SnakeCase produces the snake_case version of the given CamelCase string. News => news OldNews => old_news CNNNews => cnn_news

(name string)

Source from the content-addressed store, hash-verified

200// OldNews => old_news
201// CNNNews => cnn_news
202func SnakeCase(name string) string {
203 // Special handling for single "words" starting with multiple upper case letters
204 for u, l := range toLower {
205 name = strings.ReplaceAll(name, u, l)
206 }
207
208 // Remove leading and trailing blank spaces and replace any blank spaces in
209 // between with a single underscore
210 name = strings.Join(strings.Fields(name), "_")
211
212 // Special handling for dashes and slashes to convert them into underscores
213 name = strings.ReplaceAll(name, "-", "_")
214 name = strings.ReplaceAll(name, "/", "_")
215
216 var b bytes.Buffer
217 ln := len(name)
218 if ln == 0 {
219 return ""
220 }
221 n := rune(name[0])
222 b.WriteRune(unicode.ToLower(n))
223 var lastLower, isLower, lastUnder, isUnder bool
224 for i := 1; i < ln; i++ {
225 r := rune(name[i])
226 isLower = unicode.IsLower(r) && unicode.IsLetter(r) || unicode.IsDigit(r)
227 isUnder = r == '_'
228 if !isLower && !isUnder {
229 if lastLower && !lastUnder {
230 b.WriteRune('_')
231 } else if ln > i+1 {
232 rn := rune(name[i+1])
233 if unicode.IsLower(rn) && rn != '_' && !lastUnder {
234 b.WriteRune('_')
235 }
236 }
237 }
238 b.WriteRune(unicode.ToLower(r))
239 lastLower = isLower
240 lastUnder = isUnder
241 }
242 return b.String()
243}
244
245// KebabCase produces the kebab-case version of the given CamelCase string.
246func KebabCase(name string) string {

Callers 15

endpointParserFunction · 0.92
sseClientFileFunction · 0.92
sseServerFileFunction · 0.92
endpointParserFunction · 0.92
pkgNameFunction · 0.92
analyzeMethod · 0.92
FieldMethod · 0.92
protoBufMessageDefFunction · 0.92
buildServerDataFunction · 0.92
ConvertFilesFunction · 0.92
ConvertFileFunction · 0.92
attributeNameFunction · 0.92

Calls 1

StringMethod · 0.65

Tested by 1

TestSnakeCaseFunction · 0.68