stringcase

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

README

stringcase

Stringcase provides utilities for converting strings between different naming conventions, including:

  • Kebab-case (ToKebab)
  • Snake_case (ToSnake)
  • CamelCase (ToCamel)
  • PascalCase (ToPascal)
  • Title Case (ToTitle)
  • Space-separated words (ToWords, FromKebab, FromSnake)

Features

  • Handles Go initialisms (e.g., ID, API, HTTP) according to Go naming conventions.
  • Collapses multiple separators and spaces.
  • Converts from and to all major string case styles.

Usage

import "github.com/alextanhongpin/core/types/stringcase"

fmt.Println(stringcase.ToKebab("HelloWorld"))      // "hello-world"
fmt.Println(stringcase.ToSnake("HelloWorld"))      // "hello_world"
fmt.Println(stringcase.ToCamel("user_id"))         // "userID"
fmt.Println(stringcase.ToPascal("user_id"))        // "UserID"
fmt.Println(stringcase.ToTitle("api_response_id")) // "API Response ID"

Supported Initialisms

The following initialisms are handled according to Go conventions:

API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XSRF, XSS

Testing

Run the tests:

go test -v

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultInitialisms = strings.Fields("API APIs ASCII ASCIIs CPU CPUs CSS DNS EOF EOFs GUID GUIDs HTML HTMLs HTTP HTTPS HTTPs ID IDs IP IPs JSON JSONs LHS QPS RAM RAMs RHS RPC RPCs SLA SLAs SMTP SMTPs SQL SQLs SSH SSHs TCP TCPs TLS TTL TTLs UDP UDPs UI UID UIDs UIs URI URIs URL URLs UTF8 UTF8s UUID UUIDs VM VMs XML XMLs XSRF XSRFs XSS")
)

Functions

func FromKebab

func FromKebab(s string) string

FromKebab converts kebab-case to space-separated words.

func FromSnake

func FromSnake(s string) string

FromSnake converts snake_case to space-separated words.

func ToCamel

func ToCamel(s string) string

ToCamel converts a string to camelCase.

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case.

func ToPascal

func ToPascal(s string) string

ToPascal converts a string to PascalCase.

func ToSnake

func ToSnake(s string) string

ToSnake converts a string to snake_case.

func ToTitle

func ToTitle(s string) string

ToTitle converts a string to Title Case.

Types

type Tokenizer added in v0.0.12

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

func NewTokenizer added in v0.0.12

func NewTokenizer(initialisms ...string) *Tokenizer

func (*Tokenizer) Camel added in v0.0.12

func (t *Tokenizer) Camel(text string) string

func (*Tokenizer) Kebab added in v0.0.12

func (t *Tokenizer) Kebab(text string) string

func (*Tokenizer) Pascal added in v0.0.12

func (t *Tokenizer) Pascal(text string) string

func (*Tokenizer) Snake added in v0.0.12

func (t *Tokenizer) Snake(text string) string

func (*Tokenizer) Title added in v0.0.12

func (t *Tokenizer) Title(text string) string

func (*Tokenizer) Tokenize added in v0.0.12

func (t *Tokenizer) Tokenize(text string) iter.Seq[string]

Jump to

Keyboard shortcuts

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