takuoki / gocase

A golang package to convert normal CamelCase to Golang's CamelCase and vice versa.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gocase

GoDoc CI codecov MIT License

A golang package to convert normal CamelCase to Golang's CamelCase and vice versa.

Install

go get -u github.com/takuoki/gocase

Usage

Default converter

str := gocase.To("IpAddress") // "IPAddress"
str := gocase.To("defaultDNSServer") // "defaultDnsServer"

Custom converter

converter, _ := gocase.New(gocase.WithInitialisms("JSON", "CSV"))

str1 := converter.To("IpAddress") // "IpAddress" (no convert)
str2 := converter.To("JsonFile")  // "JSONFile"
str3 := converter.To("CsvFile")   // "CSVFile"

Example

The default converter converts as follows

  • To: converts a string from From to To.
  • Revert: converts a string from To to From.
From To
"" ""
"jsonFile" "jsonFile"
"IpAddress" "IPAddress"
"defaultDnsServer" "defaultDNSServer"
"somethingHttpApiId" "somethingHTTPAPIID"
"somethingUuid" "somethingUUID"
"somethingSip" "somethingSIP"

!!! WARNING !!!

Note that it is impossible to accurately determine the word break in a string of consecutive uppercase words, so the string converted with To and Revert may not match the original string.

example

  • IdB --To-> IDB --Revert-> IDb
  • UUid --To-> UUID --Revert-> Uuid

Reference

For more details, please see initialisms section in Staticcheck.

About

A golang package to convert normal CamelCase to Golang's CamelCase and vice versa.

License:MIT License


Languages

Language:Go 100.0%