The words package provides utilities for converting strings between different
cases, handling initialisms, and splitting words based on specific rules.
The package aims to maximize performance and minimize dependencies. It doesn't use regular expressions.
To install the package, run:
go get github.com/bartdeboer/wordsConverts a given string to snake_case.
func ToSnakeCase(s string) stringExample:
fmt.Println(ToSnakeCase("ThisIsATest")) // Output: this_is_a_testConverts a given string to kebab-case.
func ToKebabCase(s string) stringExample:
fmt.Println(ToKebabCase("ThisIsATest")) // Output: this-is-a-testCapitalizes the first letter of a given string and converts the rest to lowercase.
func Capitalize(s string) stringExample:
fmt.Println(Capitalize("example")) // Output: ExampleConverts a given string to capitalized words, handling initialisms correctly.
func ToCapWords(s string) stringExample:
fmt.Println(ToCapWords("thisIsATest")) // Output: ThisIsATestConverts a given string to mixedCase, handling initialisms correctly.
func ToMixedCase(s string) stringExample:
fmt.Println(ToMixedCase("thisIsATest")) // Output: thisIsATestSplits a string into words based on transitions between alphanumeric and non-alphanumeric characters, as well as transitions between lowercase and uppercase characters.
func SplitWords(s string) []stringExample:
fmt.Println(SplitWords("ThisIsATest")) // Output: ["This", "Is", "A", "Test"]go test ./...go test -bench=.