Skip to content

Some golang string helper functions focused around case transformation but other things in the future.

License

Notifications You must be signed in to change notification settings

arran4/strings2

Repository files navigation

strings2

Test Status Vet Status Lint Status Fmt Status Go Reference

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.

Installation

go get github.com/arran4/strings2

Add the module to your project and import it:

import "github.com/arran4/strings2"

Usage

Words must implement fmt.Stringer. The package defines several helper types which satisfy this interface:

words := []strings2.Word{
    strings2.SingleCaseWord("hello"),
    strings2.SingleCaseWord("world"),
}

Parsing

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))

Case Conversion Functions

strings2.ToCamelCase(words)  // "helloWorld"
strings2.ToPascalCase(words) // "HelloWorld"
strings2.ToKebabCase(words)  // "hello-world"
strings2.ToSnakeCase(words)  // "hello_world"

Customising Formatting

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:
    • CMVerbatim
    • CMFirstTitle
    • CMAllTitle
    • CMFirstLower
    • CMWhispering
    • CMScreaming
  • 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)))

Options are composable so multiple behaviours can be applied at once. See the documentation in types.go for details on further options.

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

About

Some golang string helper functions focused around case transformation but other things in the future.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages