Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
Just a small function that I wrote to detect pascal, kebab( also known as lisp-case, spinal-case or train-case), camel, snake camel and screaming snake cases. Useful in sniffing out in advance the case type of the input string that your next function going to process. This function uses regular expressions to detect markers in the input string such as compound words or phrases that are separated by underscore or hyphen.
NOTE: The default type returned is normal case
, when the function cannot detect any significant markers such as -
, _
or uppercase letters in the input string.
Here you go!
package main
import (
"errors"
"fmt"
"regexp"
"strings"
)
func detectCaseType(str string) (string, error) {
// types of case to detect
// from https://github.com/qerub/camel-snake-kebab/blob/stable/src/camel_snake_kebab/core.cljc
// PascalCase
// Camel_Snake_Case
// camelCase
// SCREAMING_SNAKE_CASE
// snake_case
// kebab-case
// HTTP-Header-Case
trimmed := strings.TrimSpace(str)
if trimmed == "" {
return "", errors.New("input string cannot be empty")
}
pascalCaseRE := regexp.MustCompile("^[A-Z][a-z]+(?:[A-Z][a-z]+)*$")
camelSnakeCaseRE := regexp.MustCompile("^[A-Z][a-z]+(_[A-Z][a-z]+)*$")
camelCaseRE := regexp.MustCompile("^[a-z]+(?:[A-Z][a-z]+)*$")
screamingSnakeCaseRE := regexp.MustCompile("^[A-Z]+(_[A-Z]+)*$")
snakeCaseRE := regexp.MustCompile("^[a-z]+(_[a-z]+)*$")
kebabCaseRE := regexp.MustCompile("^[a-z]+(-[a-z]+)*$")
// httpHeaderCaseRE -- skip this for now
if pascalCaseRE.MatchString(trimmed) {
return "PascalCase", nil
}
if camelSnakeCaseRE.MatchString(trimmed) {
return "CamelSnakeCase", nil
}
if camelCaseRE.MatchString(trimmed) {
return "CamelCase", nil
}
if screamingSnakeCaseRE.MatchString(trimmed) {
return "ScreamingSnakeCase", nil
}
if snakeCaseRE.MatchString(trimmed) {
return "SnakeCase", nil
}
if kebabCaseRE.MatchString(trimmed) {
return "kebabCase", nil
}
// default
return "normal case", nil
}
func main() {
result, err := detectCaseType("世界你好")
if err != nil {
panic(err)
}
fmt.Println("Result : ", result)
result1, err := detectCaseType("PascalTestCase")
if err != nil {
panic(err)
}
fmt.Println("Result1 : ", result1)
result2, err := detectCaseType("Test_Camel_Snake_Case")
if err != nil {
panic(err)
}
fmt.Println("Result2 : ", result2)
result3, err := detectCaseType("testCaseIfIsCamel")
if err != nil {
panic(err)
}
fmt.Println("Result3 : ", result3)
result4, err := detectCaseType("I_LIKE_TO_SCREAM_CASE")
if err != nil {
panic(err)
}
fmt.Println("Result4 : ", result4)
result5, err := detectCaseType("pascal_or_snake_case")
if err != nil {
panic(err)
}
fmt.Println("Result5 : ", result5)
result6, err := detectCaseType("kebab-pascal-or-snake-case")
if err != nil {
panic(err)
}
fmt.Println("Result6 : ", result6)
result7, err := detectCaseType("kebab-PascalOr_snake-CASE")
if err != nil {
panic(err)
}
fmt.Println("Result7 : ", result7)
}
Output :
Result : normal case
Result1 : PascalCase
Result2 : CamelSnakeCase
Result3 : CamelCase
Result4 : ScreamingSnakeCase
Result5 : SnakeCase
Result6 : kebabCase
Result7 : normal case
References:
https://en.wikipedia.org/wiki/Snake_case
https://github.com/qerub/camel-snake-kebab/blob/stable/src/camelsnakekebab/core.cljc
See also : Golang : Get all upper case or lower case characters from string example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.3k Golang : Logging with logrus
+12.2k Golang : Encrypt and decrypt data with x509 crypto
+8.1k Golang : Get final or effective URL with Request.URL example
+7.2k Golang : Not able to grep log.Println() output
+9k Golang : Gonum standard normal random numbers example
+5.2k Golang : Pad file extension automagically
+6.5k Elasticsearch : Shutdown a local node
+19.1k Golang : Get RGBA values of each image pixel
+12k Golang : Save webcamera frames to video file
+6.4k Golang : Totalize or add-up an array or slice example
+7.2k Golang : Calculate how many weeks left to go in a given year
+14.4k Golang : Send email with attachment(RFC2822) using Gmail API example