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
+15.9k Golang : Check if a string contains multiple sub-strings in []string?
+18.1k Golang : Set, Get and List environment variables
+6.6k Golang : Levenshtein distance example
+14.6k Golang : Get all local users and print out their home directory, description and group id
+5.6k Golang : Detect variable or constant type
+13.5k Golang : concatenate(combine) strings
+5.9k Apt-get to install and uninstall Golang
+7k Golang : Rot13 and Rot5 algorithms example
+45.5k Golang : Marshal and unmarshal json.RawMessage struct example
+9.8k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+5k Gogland : Datasource explorer
+7k Golang : How to stop user from directly running an executable file?