strcase

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 5 Imported by: 0

README

strcase

NOTE: we learned a lot from package strcase

strcase is a go package for converting string case to various cases (e.g. snake case or camel case) to see the full conversion table below.

Example

s := "AnyKind of_string"
Function Result
ToSnake(s) any_kind_of_string
ToSnakeWithIgnore(s, '.') any_kind.of_string
ToScreamingSnake(s) ANY_KIND_OF_STRING
ToKebab(s) any-kind-of-string
ToScreamingKebab(s) ANY-KIND-OF-STRING
ToDelimited(s, '.') any.kind.of.string
ToScreamingDelimited(s, '.', '', true) ANY.KIND.OF.STRING
ToScreamingDelimited(s, '.', ' ', true) ANY.KIND OF.STRING
ToCamel(s) AnyKindOfString
ToLowerCamel(s) anyKindOfString

Custom Acronyms

Sometimes, text may contain specific acronyms which need to be handled in a certain way.

// For "WebAPIV3Spec":
//  - ToCamel: WebApiv3Spec
//  - ToLowerCamel: webApiv3Spec
//  - ToSnake: web_apiv3_spec
strcase.New(map[string]string{"APIV3": "apiv3"})

Documentation

Overview

Package strcase converts strings to various cases. See the conversion table below:

| Function                        | Result             |
|---------------------------------|--------------------|
| ToSnake(s)                      | any_kind_of_string |
| ToScreamingSnake(s)             | ANY_KIND_OF_STRING |
| ToKebab(s)                      | any-kind-of-string |
| ToScreamingKebab(s)             | ANY-KIND-OF-STRING |
| ToDelimited(s, '.')             | any.kind.of.string |
| ToScreamingDelimited(s, '.')    | ANY.KIND.OF.STRING |
| ToCamel(s)                      | AnyKindOfString    |
| ToLowerCamel(s)                 | anyKindOfString    |

Index

Constants

View Source
const (
	Other byteType = iota
	Upper
	Lower
	Separator
	Digit
)

Variables

This section is empty.

Functions

func NewContext added in v0.14.0

func NewContext(ctx context.Context, v *Strcase) context.Context

NewContext creates a new context with the given Strcase.

Types

type Strcase added in v0.14.0

type Strcase struct {
	// contains filtered or unexported fields
}

func FromContext added in v0.14.0

func FromContext(ctx context.Context) *Strcase

FromContext returns the Strcase from the given context. If not found, it will return the default Strcase.

func New added in v0.13.0

func New(acronyms map[string]string) *Strcase

New creates a new Strcase with the given acronyms.

Examples:

  • "API": "api"
  • "K8s": "k8s"
  • "3D": "3d"
  • `A(1\d{3})`: "a$l1}"
  • `(\d)[vV](\d)`: "${1}v${2}"

func (*Strcase) ToCamel added in v0.14.0

func (ctx *Strcase) ToCamel(s string) string

ToCamel converts a string to CamelCase

func (*Strcase) ToDelimited added in v0.14.0

func (ctx *Strcase) ToDelimited(s string, delimiter uint8) string

ToDelimited converts a string to delimited.snake.case (in this case `delimiter = '.'`)

func (*Strcase) ToKebab added in v0.14.0

func (ctx *Strcase) ToKebab(s string) string

ToKebab converts a string to kebab-case

func (*Strcase) ToLowerCamel added in v0.14.0

func (ctx *Strcase) ToLowerCamel(s string) string

ToLowerCamel converts a string to lowerCamelCase

func (*Strcase) ToScreamingDelimited added in v0.14.0

func (ctx *Strcase) ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bool) string

ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `delimiter = '.'; screaming = true`) or delimited.snake.case (in this case `delimiter = '.'; screaming = false`)

func (*Strcase) ToScreamingKebab added in v0.14.0

func (ctx *Strcase) ToScreamingKebab(s string) string

ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE

func (*Strcase) ToScreamingSnake added in v0.14.0

func (ctx *Strcase) ToScreamingSnake(s string) string

ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE

func (*Strcase) ToSnake added in v0.14.0

func (ctx *Strcase) ToSnake(s string) string

ToSnake converts a string to snake_case

func (*Strcase) ToSnakeWithIgnore added in v0.14.0

func (ctx *Strcase) ToSnakeWithIgnore(s string, ignore string) string

Directories

Path Synopsis
Package camelcase is a micro package to split the words of a camelcase type string into a slice of words.
Package camelcase is a micro package to split the words of a camelcase type string into a slice of words.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL