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
+16.1k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+27.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+34.9k Golang : Integer is between a range
+10.8k Golang : How to determine a prime number?
+11.1k Golang : Find age or leap age from date of birth example
+14.8k JavaScript/JQuery : Detect or intercept enter key pressed example
+20.8k Golang : Get password from console input without echo or masked
+18.9k Golang : Check if directory exist and create if does not exist
+11k Use systeminfo to find out installed Windows Hotfix(s) or updates
+4.6k Nginx and PageSpeed build from source CentOS example
+10.5k Golang : Get UDP client IP address and differentiate clients by port number
+19.3k Golang : Close channel after ticker stopped example