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
+7.3k Golang : How to stop user from directly running an executable file?
+5.1k Javascript : Change page title to get viewer attention
+7k Golang : Null and nil value
+11.2k Golang : Concatenate (combine) buffer data example
+10.2k Swift : Convert (cast) String to Integer
+4.6k Golang : A program that contain another program and executes it during run-time
+4.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+8k Golang : HttpRouter multiplexer routing example
+7.9k Golang : Append and add item in slice
+15.3k Golang : How to convert(cast) IP address to string?
+25.6k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+12.9k Golang : How to calculate the distance between two coordinates using Haversine formula