Skip to content

goloop/scs

Repository files navigation

Go Report Card License License Stay with Ukraine

scs — String Case Style for Go

scs converts identifiers between naming conventions: camelCase, PascalCase, snake_case, kebab-case, SCREAMING_SNAKE_CASE, dot.case, Title Case and Sentence case.

Every conversion is built on one universal tokenizer. Split breaks any input into normalized words; each style is then a different rendering of those words. Because a single tokenizer feeds every renderer, the converters are total — they never return an error and never need to know the input's original style.

scs.ToSnake("HTTPServerID") // "http_server_id"
scs.ToCamel("user_id")      // "userId"
scs.ToKebab("HelloWorld")   // "hello-world"

Features

  • Eight case styles from a single word model.
  • Total functions — any string maps to a well-defined result, no errors.
  • Predictable, documented rules for acronyms, digits and Unicode.
  • Opt-in Go-style initialisms (ID, URL, HTTP) via a reusable, concurrency-safe Caser.
  • Detect with an honest contract — it commits to a style only when the answer is unambiguous.
  • Public tokenizer: Split (slice) and Words (iter.Seq).
  • Zero dependencies.

Installation

go get github.com/goloop/scs/v2
import "github.com/goloop/scs/v2"

Requires Go 1.24 or newer. The package has no third-party dependencies.

Quick start

package main

import (
    "fmt"

    "github.com/goloop/scs/v2"
)

func main() {
    fmt.Println(scs.ToCamel("hello_world"))     // helloWorld
    fmt.Println(scs.ToPascal("hello-world"))    // HelloWorld
    fmt.Println(scs.ToSnake("HelloWorld"))      // hello_world
    fmt.Println(scs.ToKebab("helloWorld"))      // hello-world
    fmt.Println(scs.ToScreamingSnake("userID")) // USER_ID
    fmt.Println(scs.ToSentence("hello_world"))  // Hello world

    // Style chosen at runtime (config, CLI flag, ...).
    style, _ := scs.ParseStyle("kebab")
    fmt.Println(scs.Convert(style, "HTTPServerID")) // http-server-id

    // Go-style all-caps initialisms are opt-in via a reusable Caser.
    c := scs.New(scs.WithAcronyms("ID", "URL", "HTTP"))
    fmt.Println(c.ToPascal("user_id")) // UserID
}

Documentation

Contributing

Contributions are welcome. Please run go test ./..., go vet ./... and gofmt -l . before submitting a pull request.

License

scs is released under the MIT License. See LICENSE.

About

The scs (String Case Style) module implements methods for converting string case to various case styles: camelCase, kebab-case, PascalCase and snake_case.

Topics

Resources

License

Code of conduct

Contributing

Stars

3 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages