kaspad/util/btcstrings.go
stasatdaglabs 8a234bf4a3 [DEV-242] Parameter names should be also camelCase in JSON-RPC (#118)
* [DEV-242] Modified some help functionality to convert to lowercase camel case instead of just lowercase.

* [DEV-242] Corrected help functionality for struct field names.

* [DEV-242] Corrected help functionality for struct names.

* [DEV-242] Cleaned up toLowercaseCamelCase.

* [DEV-242] Renamed toLowercaseCamelCase to toCamelCase.

* [DEV-242] Converted the rest of the stuff in rpcserverhelp.go to camelCase. Fixed a bug in the camelCase converter.

* [DEV-242] camelCase-ified the last few RPC parameter names.

* [DEV-242] Fixed an off-by-one bug in toCamelCase.

* [DEV-242] Changed back from "jsonRpc" to "jsonrpc".

* [DEV-242] Moved toCamelCase into utils, wrote unit tests for it, and fixed an off-by-one bug.

* [DEV-242] Re-exported DefaultHomeDir because it's required in windows_service.go.

* [DEV-242] Added a comment above DefaultHomeDir to satisfy golint.

* [DEV-242] Formatted config/config.go.
2018-11-05 18:50:55 +02:00

70 lines
1.9 KiB
Go

package util
import (
"strings"
"unicode"
)
// ToCamelCase converts a camelCase-ish string into a typical JSON camelCase string.
// Example conversion: MyJSONVariable -> myJsonVariable
func ToCamelCase(str string) string {
if len(str) == 0 {
return ""
}
// Split the string into words
words := make([]string, 0)
wordStartIndex := 0
wordEndIndex := -1
var previousCharacter rune
for i, character := range str {
if i > 0 {
if unicode.IsLower(previousCharacter) && unicode.IsUpper(character) {
// previousCharacter is definitely the end of a word
wordEndIndex = i - 1
word := str[wordStartIndex:i]
words = append(words, word)
} else if unicode.IsUpper(previousCharacter) && unicode.IsLower(character) {
// previousCharacter is definitely the start of a word
wordStartIndex = i - 1
if wordStartIndex-wordEndIndex > 1 {
// This handles consequent uppercase words, such as acronyms.
// Example: getBlockDAGInfo
// ^^^
word := str[wordEndIndex+1 : wordStartIndex]
words = append(words, word)
}
}
}
previousCharacter = character
}
if unicode.IsUpper(previousCharacter) {
// This handles consequent uppercase words, such as acronyms, at the end of the string
// Example: TxID
// ^^
for i := len(str) - 1; i >= 0; i-- {
if unicode.IsLower(rune(str[i])) {
break
}
wordStartIndex = i
}
}
lastWord := str[wordStartIndex:]
words = append(words, lastWord)
// Build a PascalCase string out of the words
var camelCaseBuilder strings.Builder
for _, word := range words {
lowercaseWord := strings.ToLower(word)
capitalizedWord := strings.ToUpper(string(lowercaseWord[0])) + lowercaseWord[1:]
camelCaseBuilder.WriteString(capitalizedWord)
}
camelCaseString := camelCaseBuilder.String()
// Un-capitalize the first character to covert PascalCase into camelCase
return strings.ToLower(string(camelCaseString[0])) + camelCaseString[1:]
}