Skip to content

veggiemonk/strcase

 
 

Repository files navigation

STRCASE

Go Report Card OpenSSF Scorecard License badge

Forked from segmentio repositories:

The case for strcase 🥁

First of all, case is a keyword in Go, so we can't use it as a package name.

Therefore, strcase is a Go package that provides utilities for converting strings between different cases.

It uses only the standard library (0 dependencies). It aims to be fast, see benchmarks, secure, see fuzzing and simple to use.

It can convert any string to:

  • camelCase or lower camel case:
    • example: "theQuickBrownFoxJumpsOverTheLazyDog", type myInternalType struct {}
    • usage: Internal (private) variables, functions, methods, and types in Go
  • PascalCase or upper camel case:
    • example: "TheQuickBrownFoxJumpsOverTheLazyDog", type MyExportedType struct {}
    • usage: Exported (public) variables, functions, methods, and types in Go
  • snake_case:
    • example: "the_quick_brown_fox_jumps_over_the_lazy_dog"
    • usage: naming convention in Python.
  • dash-case or kebab-case:
    • "the-quick-brown-fox-jumps-over-the-lazy-dog"
    • usage: naming convention in CSS, also used in HTML and kubernetes manifests.

Note:

the "dash" is actually an ASCII hyphen a.k.a "hyphen-minus" a.k.a "minus sign", unicode U+002D, represented as -, is often confused with "hyphen", unicode U+2010, represented as ‐ or with "En Dash" unicode U+2013, represented as – .

see Wikipedia for more details and Unicode ASCII punctuation for the full list of dashes. I'm no expert in this area, it seems quite complicated, so if you have any suggestions, please open an issue and let us know.

Installation

go get github.com/veggiemonk/strcase

Usage

package main

import (
    "fmt"

    sc "github.com/veggiemonk/strcase"
)

func main() {
    fmt.Println(sc.Camel("hello world"))  // helloWorld
    fmt.Println(sc.Pascal("hello world")) // HelloWorld
    fmt.Println(sc.Kebab("hello world"))  // hello-world
    fmt.Println(sc.Snake("hello world"))  // hello_world
}

Testing and edge cases

Each function has a corresponding test function in the xxxx_test.go file. Have a look at the tests to see the edge cases.

To run the tests, run go test from the root of the project.

go test -v

Benchmarks

Each function has a corresponding test function in the xxxx_bench_test.go file. To run the benchmarks, run go test from the root of the project.

go test -bench=. -benchmem

Comparing benchmarks

The results of the benchmarks are stored in benchmarks.txt and can be compared with the following command:

go test -bench . -benchmem | tee new.txt

Once you have the results, you can compare them with the previous results. For that we need tools called benchcmp and benchstat.

benchcmp benchmarks.txt new.txt
benchstat benchmarks.txt new.txt

Fuzzing

What is fuzzing? Fuzzing is a way to test software by providing invalid, unexpected, or random data as inputs to a computer program. The program is then monitored for exceptions such as crashes, failing built-in code assertions, or potential memory leaks.

For more information, see the official article on the Go website here.

Each function has a corresponding test function in the xxxx_fuzz_test.go file. To run the fuzzing, run this command from the root of the project.

go test -fuzz=FuzzCamelCase  -fuzztime=10s
go test -fuzz=FuzzKebabCase  -fuzztime=10s
go test -fuzz=FuzzPascalCase -fuzztime=10s
go test -fuzz=FuzzSnakeCase  -fuzztime=10s

Documentation

The documentation is available on pkg.go.dev. or run it locally

go install golang.org/x/pkgsite/cmd/pkgsite@latest && pkgsite

# open http://localhost:8080/github.com/veggiemonk/strcase