strcase

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT, MIT Imports: 4 Imported by: 0

Documentation

Overview

Package strcase is a vendored copy of github.com/ettle/strcase v0.2.0 (MIT, Copyright (c) 2020 Liyan David Chang), internalized because the upstream project is no longer maintained. See LICENSE in this directory for the full license text; the copyright notice is preserved as required by the MIT license.

Only the subset used by this repository is kept (caser, split, convert, initialisms, unicode helpers); upstream tests and the CLI were dropped.

Do not add new public API here — treat it as frozen vendored code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToCamel

func ToCamel(s string) string

ToCamel returns words in camelCase (capitalized words concatenated together, with first word lower case). Also known as lowerCamelCase or mixedCase.

func ToCase

func ToCase(s string, wordCase WordCase, delimiter rune) string

ToCase returns words in given case and delimiter.

func ToGoCamel

func ToGoCamel(s string) string

ToGoCamel returns words in camelCase (capitalized words concatenated together, with first word lower case). Also known as lowerCamelCase or mixedCase.

Respects Go's common initialisms, but first word remains lowercased which is important for code generator use cases (e.g. toJson -> toJSON, httpResponse -> httpResponse).

func ToGoCase

func ToGoCase(s string, wordCase WordCase, delimiter rune) string

ToGoCase returns words in given case and delimiter.

Respects Go's common initialisms (e.g. httpResponse -> HTTPResponse).

func ToGoKebab

func ToGoKebab(s string) string

ToGoKebab returns words in kebab-case (lower case words with dashes). Also known as dash-case.

Respects Go's common initialisms (e.g. http-response -> HTTP-response).

func ToGoPascal

func ToGoPascal(s string) string

ToGoPascal returns words in PascalCase (capitalized words concatenated together). Also known as UpperPascalCase.

Respects Go's common initialisms (e.g. HttpResponse -> HTTPResponse).

func ToGoSnake

func ToGoSnake(s string) string

ToGoSnake returns words in snake_case (lower case words with underscores).

Respects Go's common initialisms (e.g. http_response -> HTTP_response).

func ToKEBAB

func ToKEBAB(s string) string

ToKEBAB returns words in KEBAB-CASE (upper case words with dashes). Also known as SCREAMING-KEBAB-CASE or SCREAMING-DASH-CASE.

func ToKebab

func ToKebab(s string) string

ToKebab returns words in kebab-case (lower case words with dashes). Also known as dash-case.

func ToPascal

func ToPascal(s string) string

ToPascal returns words in PascalCase (capitalized words concatenated together). Also known as UpperPascalCase.

func ToSNAKE

func ToSNAKE(s string) string

ToSNAKE returns words in SNAKE_CASE (upper case words with underscores). Also known as SCREAMING_SNAKE_CASE or UPPER_CASE.

func ToSnake

func ToSnake(s string) string

ToSnake returns words in snake_case (lower case words with underscores).

Types

type Caser

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

Caser allows for customization of parsing and intialisms

func NewCaser

func NewCaser(goInitialisms bool, initialismOverrides map[string]bool, splitFn SplitFn) *Caser

NewCaser returns a configured Caser.

A Caser should be created when you want fine grained control over how the words are split.

Notes on function arguments

goInitialisms: Whether to use Golint's intialisms

initialismOverrides: A mapping of extra initialisms
Keys must be in ALL CAPS. Merged with Golint's if goInitialisms is set.
Setting a key to false will override Golint's.

splitFn: How to separate words
Override the default split function. Consider using NewSplitFn to
configure one instead of writing your own.

func (*Caser) ToCamel

func (c *Caser) ToCamel(s string) string

ToCamel returns words in camelCase (capitalized words concatenated together, with first word lower case). Also known as lowerCamelCase or mixedCase.

func (*Caser) ToCase

func (c *Caser) ToCase(s string, wordCase WordCase, delimiter rune) string

ToCase returns words with a given case and delimiter.

func (*Caser) ToKEBAB

func (c *Caser) ToKEBAB(s string) string

ToKEBAB returns words in KEBAB-CASE (upper case words with dashes). Also known as SCREAMING-KEBAB-CASE or SCREAMING-DASH-CASE.

func (*Caser) ToKebab

func (c *Caser) ToKebab(s string) string

ToKebab returns words in kebab-case (lower case words with dashes). Also known as dash-case.

func (*Caser) ToPascal

func (c *Caser) ToPascal(s string) string

ToPascal returns words in PascalCase (capitalized words concatenated together). Also known as UpperPascalCase.

func (*Caser) ToSNAKE

func (c *Caser) ToSNAKE(s string) string

ToSNAKE returns words in SNAKE_CASE (upper case words with underscores). Also known as SCREAMING_SNAKE_CASE or UPPER_CASE.

func (*Caser) ToSnake

func (c *Caser) ToSnake(s string) string

ToSnake returns words in snake_case (lower case words with underscores).

type SplitAction

type SplitAction int

SplitAction defines if and how to split a string

const (
	// Noop - Continue to next character
	Noop SplitAction = iota
	// Split - Split between words
	// e.g. to split between wordsWithoutDelimiters
	Split
	// SkipSplit - Split the word and drop the character
	// e.g. to split words with delimiters
	SkipSplit
	// Skip - Remove the character completely
	Skip
)

type SplitFn

type SplitFn func(prev, curr, next rune) SplitAction

SplitFn defines how to split a string into words

func NewSplitFn

func NewSplitFn(
	delimiters []rune,
	splitOptions ...SplitOption,
) SplitFn

NewSplitFn returns a SplitFn based on the options provided.

NewSplitFn covers the majority of common options that other strcase libraries provide and should allow you to simply create a custom caser. For more complicated use cases, feel free to write your own SplitFn

type SplitOption

type SplitOption int

SplitOption are options that allow for configuring NewSplitFn

const (
	// SplitCase - FooBar -> Foo_Bar
	SplitCase SplitOption = iota
	// SplitAcronym - FOOBar -> Foo_Bar
	// It won't preserve FOO's case. If you want, you can set the Caser's initialisms so FOO will be in all caps
	SplitAcronym
	// SplitBeforeNumber - port80 -> port_80
	SplitBeforeNumber
	// SplitAfterNumber - 200status -> 200_status
	SplitAfterNumber
	// PreserveNumberFormatting - a.b.2,000.3.c -> a_b_2,000.3_c
	PreserveNumberFormatting
)

type WordCase

type WordCase int

WordCase is an enumeration of the ways to format a word.

const (
	// Original - Preserve the original input strcase
	Original WordCase = iota
	// LowerCase - All letters lower cased (example)
	LowerCase
	// UpperCase - All letters upper cased (EXAMPLE)
	UpperCase
	// TitleCase - Only first letter upper cased (Example)
	TitleCase
	// CamelCase - TitleCase except lower case first word (exampleText)
	// Notably, even if the first word is an initialism, it will be lower
	// cased. This is important for code generators where capital letters
	// mean exported functions. i.e. jsonString(), not JSONString()
	CamelCase
)

Jump to

Keyboard shortcuts

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