A simple Go library for string case conversion. Converts between camelCase, PascalCase, kebab-case, snake_case, and more.
go get github.com/gomantics/sx
package main
import (
"fmt"
"github.com/gomantics/sx"
)
func main() {
// Convert to different cases
fmt.Println(sx.CamelCase("hello-world")) // helloWorld
fmt.Println(sx.PascalCase("hello_world")) // HelloWorld
fmt.Println(sx.KebabCase("HelloWorld")) // hello-world
fmt.Println(sx.SnakeCase("HelloWorld")) // hello_world
fmt.Println(sx.TrainCase("hello-world")) // Hello-World
fmt.Println(sx.FlatCase("hello-world")) // helloworld
// Works with mixed separators and cases
fmt.Println(sx.CamelCase("mixed_caseWith-different.separators")) // mixedCaseWithDifferentSeparators
// Handle complex acronyms
fmt.Println(sx.KebabCase("XMLHttpRequest")) // xml-http-request
fmt.Println(sx.CamelCase("HTML5Parser", sx.WithNormalize(true))) // html5Parser
}
CamelCase()
- converts to camelCasePascalCase()
- converts to PascalCaseKebabCase()
- converts to kebab-caseSnakeCase()
- converts to snake_caseTrainCase()
- converts to Train-CaseFlatCase()
- converts to flatcase (no separators)
SplitByCase()
- splits strings into words by case changes and separatorsUpperFirst()
- capitalizes first characterLowerFirst()
- lowercases first character
Some functions support options for customization:
// Normalize case for strict PascalCase/camelCase
sx.PascalCase("XMLHttpRequest", sx.WithNormalize(true)) // XmlHttpRequest
// Custom separators for splitting
sx.SplitByCase("hello.world", sx.WithSeparators('.')) // ["hello", "world"]
// Custom separator for kebab case
sx.KebabCase("hello world", "|") // hello|world
Contributors are always welcome! Feel free to raise a PR or create an issue first.
This library is highly inspired by scule - a fantastic JavaScript string case utility library by the UnJS team.
MIT