strings2 provides utilities for converting slices of words into various casing conventions. It is intended to supplement Go's standard library strings package with helpers for creating formats such as camelCase, PascalCase, snake_case and kebab-case.
go get github.com/arran4/strings2
Add the module to your project and import it:
import "github.com/arran4/strings2"Words must implement fmt.Stringer. The package defines several helper types which satisfy this interface:
words := []strings2.Word{
strings2.SingleCaseWord("hello"),
strings2.SingleCaseWord("world"),
}The library includes a robust parser to convert strings into typed Word objects, distinguishing between acronyms, casing, and delimiters.
// Auto-detect format and parse
words, err := strings2.Parse("helloWorld")
// Result: [SingleCaseWord("hello"), FirstUpperCaseWord("World")]
// Parse specific format
words = strings2.ParseSnakeCase("hello_world")
// Configure parser
words, err = strings2.Parse("N.E.W. World", strings2.ParserSmartAcronyms(true))strings2.ToCamelCase(words) // "helloWorld"
strings2.ToPascalCase(words) // "HelloWorld"
strings2.ToKebabCase(words) // "hello-world"
strings2.ToSnakeCase(words) // "hello_world"Behaviour can be tuned with options passed to each function. Some commonly used options include:
OptionDelimiter(string)– change the delimiter used between words.OptionCaseMode(CaseMode)– set the case transformation mode. Modes include:CMVerbatimCMFirstTitleCMAllTitleCMFirstLowerCMWhisperingCMScreaming
OptionFirstUpper()– force the result to start with an uppercase letter.OptionFirstLower()– force the result to start with a lowercase letter.
Examples:
// Custom delimiter
fmt.Println(strings2.ToKebabCase(words, strings2.OptionDelimiter("|")))
// Screaming snake case
fmt.Println(strings2.ToSnakeCase(words, strings2.OptionCaseMode(strings2.CMScreaming)))The library also provides a command-line interface that exposes all these options, ensuring that the CLI mode has as much flexibility as the code (without being obligated to use smart defaults).
strings2 camel "hello world"
# Result: helloWorld
strings2 snake --screaming "hello world"
# Result: HELLO_WORLD
strings2 kebab --first-upper "hello world"
# Result: Hello-worldYou can pipe input into the CLI as well:
echo "hello world" | strings2 pascal
# Result: HelloWorldAvailable flags across commands:
--delimiter,-d(string): Override the delimiter--screaming,-S: Enforce uppercase formatting--whispering,-w: Enforce lowercase formatting--first-upper,-U: Capitalize the first letter--first-lower,-l: Lowercase the first letter--mix-case-support,-m: Enable splitting of mixed case words--no-smart-acronyms: Disable acronym preservation--number-splitting: Enable letter-digit boundary splitting
Options are composable so multiple behaviours can be applied at once. See the documentation in `types.go` for details on further options.
## TODO
- Support slices for flags when the gosubc version supports it.
## License
This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details.