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
+9.8k Golang : Edge detection with Sobel method
+6.9k Golang : Get Alexa ranking data example
+11.4k How to tell if a binary(executable) file or web application is built with Golang?
+18.2k Golang : Example for RSA package functions
+3.5k Java : Random alphabets, alpha-numeric or numbers only string generator
+15.7k Golang : Get file permission
+9.5k Golang : Turn string or text file into slice example
+25.5k Golang : missing Mercurial command
+10.4k Golang : How to delete element(data) from map ?
+12.4k Golang : Sort and reverse sort a slice of bytes
+5.9k Golang : How to write backslash in string?
+20.8k Golang : Sort and reverse sort a slice of strings