Golang : Transform lisp or spinal case to Pascal case example
This is a continuation from the previous tutorial on how to detect the type of cases such as kebab, pascal or camel with a given input string. Below is an example of how to transform a spinal case such as kebab-is-delicious
to Pascal case KebabIsDelicious
.
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 kebabToPascalCase(str string) (pascalCase string) {
// check if str is of kebab type
caseType, _ := detectCaseType(str)
if caseType != "kebabCase" {
return "WARNING : input string must be of kebab, train or lisp case"
}
isToUpper := false
for k, v := range str {
if k == 0 {
pascalCase = strings.ToUpper(string(str[0]))
} else {
if isToUpper {
pascalCase += strings.ToUpper(string(v))
isToUpper = false
} else {
if v == '-' {
isToUpper = true
} else {
pascalCase += string(v)
}
}
}
}
return
}
func main() {
meatToSkew := "this is not a kebab-case string"
result := kebabToPascalCase(meatToSkew)
fmt.Println("[", meatToSkew, "] transform to Pascal case : [", result, "]")
meatToSkew = "this-is-a-kebab-case-string"
result1 := kebabToPascalCase(meatToSkew)
fmt.Println("[", meatToSkew, "] transform to Pascal case : [", result1, "]")
meatToSkew = "kebab-is-delicious"
result2 := kebabToPascalCase(meatToSkew)
fmt.Println("[", meatToSkew, "] transform to Pascal case : [", result2, "]")
}
Output:
[ this is not a kebab-case string ] transform to Pascal case : [ WARNING : input string must be of kebab, train or lisp case ]
[ this-is-a-kebab-case-string ] transform to Pascal case : [ ThisIsAKebabCaseString ]
[ kebab-is-delicious ] transform to Pascal case : [ KebabIsDelicious ]
References:
https://socketloop.com/tutorials/golang-detect-pascal-kebab-screaming-snake-and-camel-cases
See also : Golang : Detect number of faces or vehicles in a photo
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
+5.9k PHP : How to check if an array is empty ?
+8.7k Golang : Random integer with rand.Seed() within a given range
+12.5k Golang : Pass database connection to function called from another package and HTTP Handler
+6k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+7.2k Linux : How to fix Brother HL-1110 printing blank page problem
+14.1k Golang : Chunk split or divide a string into smaller chunk example
+5.5k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+31.9k Golang : Validate email address with regular expression
+7.2k Golang : File system scanning
+6.1k Apt-get to install and uninstall Golang
+36.2k Golang : Convert date or time stamp from string to time.Time type
+14.8k Golang : Basic authentication with .htpasswd file