strcase

package module
v0.0.0-...-9f44128 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 0 Imported by: 6

README

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

Documentation

Overview

Package strcase implements fast snake_case, kebab-case, camelCase and PascalCase conversions. The `strcase` package provides utility functions for converting any strings to various case formats. It can convert any string to:

- camelCase or lowerCamelCase

  • example: "theQuickBrownFoxJumpsOverTheLazyDog", `type myInternalType struct {}`
  • usage: Internal (private) variables, functions, methods, and types in Go

- PascalCase or UpperCamelCase

  • 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 https://en.wikipedia.org/wiki/Dash#Unicode for more details and Unicode ASCII punctuation at https://www.unicode.org/charts/PDF/U0000.pdf 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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Camel

func Camel(s string) string

Camel transforms the given string as `camelCase`.

Example
fmt.Println(Camel("sample text"))
fmt.Println(Camel("sample-text"))
fmt.Println(Camel("sample_text"))
fmt.Println(Camel("sample___text"))
fmt.Println(Camel("sampleText"))
fmt.Println(Camel(">> samPLE text <<"))
fmt.Println(Camel("sample 2 Text"))
fmt.Println(Camel("SAMPLE 2 TEXT"))
fmt.Println(Camel("invite-Your-Customers-Add-Invites"))
fmt.Println(Camel("2FA Enabled"))
fmt.Println(Camel("Enabled 2FA"))
Output:

sampleText
sampleText
sampleText
sampleText
sampleText
samPleText
sample2Text
sample2Text
inviteYourCustomersAddInvites
2FaEnabled
enabled2Fa
Example (Cryptic)
fmt.Println(Camel("5test"))
fmt.Println(Camel("test5"))
fmt.Println(Camel("THE5r"))
fmt.Println(Camel("5TEst"))
fmt.Println(Camel("_5TEst"))
fmt.Println(Camel("@%#&5TEst"))
fmt.Println(Camel("edf_6N"))
fmt.Println(Camel("f_pX9"))
fmt.Println(Camel("p_z9Rg"))
fmt.Println(Camel("@49L0S145_¬fwHƒ0TSLNVp"))
fmt.Println(Camel("lk0B@bFmjrLQ_Z6YL"))
Output:

5Test
test5
the5R
5Test
5Test
5Test
edf6N
fPX9
pZ9Rg
49L0S145FwH0Tslnvp
lk0BBFmjrLqZ6Yl
Example (Long)
fmt.Println(Camel("super long sentence that is not really necessary but we need to try it"))
Output:

superLongSentenceThatIsNotReallyNecessaryButWeNeedToTryIt
Example (Multiline)
fmt.Println(
	Camel(
		`here
is
a
multiline
string`,
	),
)
Output:

hereIsAMultilineString
Example (NonASCII)
fmt.Println(Camel("   $#$sample   2    Text   "))
fmt.Println(Camel("___$$Base64Encode"))
fmt.Println(Camel("FOO#BAR#BAZ"))
fmt.Println(Camel("FOO:BAR$BAZ"))
fmt.Println(Camel("FOO#BAR#BAZ"))
fmt.Println(Camel("something.com"))
fmt.Println(Camel("$something%"))
fmt.Println(Camel("something.com"))
fmt.Println(Camel("•¶§ƒ˚foo˙∆˚¬"))
fmt.Println(Camel("•¶§ƒ˚foo˙∆˚¬bar"))
fmt.Println(Camel("•¶§ƒ˚foo˙∆˚¬ bar"))
fmt.Println(Camel("CStringRef"))
Output:

sample2Text
base64Encode
fooBarBaz
fooBarBaz
fooBarBaz
somethingCom
something
somethingCom
foo
fooBar
fooBar
cstringRef
Example (Quoted)
fmt.Println(Camel("\"hello world\""))
Output:

helloWorld
Example (Swedish)
// Swedish and non ASCII char are not supported :(
// open an issue if this is something you need
//
// want:
// när-såg-du-en-kråka-väl-bita-en-man
fmt.Println(Camel("När såg du en kråka väl bita en man?"))
Output:

nRSGDuEnKrKaVLBitaEnMan
Example (WithSpace)
fmt.Println(Camel("   sample   2    Text   "))
Output:

sample2Text

func Kebab

func Kebab(s string) string

Kebab transform the given string as `kebab-case`.

Example
fmt.Println(Kebab("sample text"))
fmt.Println(Kebab("sample-text"))
fmt.Println(Kebab("sample_text"))
fmt.Println(Kebab("sample___text"))
fmt.Println(Kebab("sampleText"))
fmt.Println(Kebab(">> samPLE text <<"))
fmt.Println(Kebab("sample 2 Text"))
fmt.Println(Kebab("SAMPLE 2 TEXT"))
fmt.Println(Kebab("inviteYourCustomersAddInvites"))
fmt.Println(Kebab("2FA Enabled"))
fmt.Println(Kebab("Enabled 2FA"))
Output:

sample-text
sample-text
sample-text
sample-text
sample-text
sam-ple-text
sample-2-text
sample-2-text
invite-your-customers-add-invites
2fa-enabled
enabled-2fa
Example (Cryptic)
fmt.Println(Kebab("5test"))
fmt.Println(Kebab("test5"))
fmt.Println(Kebab("THE5r"))
fmt.Println(Kebab("5TEst"))
fmt.Println(Kebab("_5TEst"))
fmt.Println(Kebab("@%#&5TEst"))
fmt.Println(Kebab("edf_6N"))
fmt.Println(Kebab("f_pX9"))
fmt.Println(Kebab("p_z9Rg"))
fmt.Println(Kebab("@49L0S145_¬fwHƒ0TSLNVp"))
fmt.Println(Kebab("lk0B@bFmjrLQ_Z6YL"))
Output:

5test
test5
the5r
5test
5test
5test
edf-6n
f-p-x9
p-z9-rg
49l0s145-fw-h-0tslnvp
lk0-b-b-fmjr-lq-z6yl
Example (Long)
fmt.Println(Kebab("super long sentence that is not really necessary but we need to try it"))
Output:

super-long-sentence-that-is-not-really-necessary-but-we-need-to-try-it
Example (Multiline)
fmt.Println(
	Kebab(
		`here
is
a
multiline
string`,
	),
)
Output:

here-is-a-multiline-string
Example (NonASCII)
fmt.Println(Kebab("   $#$sample   2    Text   "))
fmt.Println(Kebab("___$$Base64Encode"))
fmt.Println(Kebab("FOO#BAR#BAZ"))
fmt.Println(Kebab("FOO:BAR$BAZ"))
fmt.Println(Kebab("FOO#BAR#BAZ"))
fmt.Println(Kebab("something.com"))
fmt.Println(Kebab("$something%"))
fmt.Println(Kebab("something.com"))
fmt.Println(Kebab("•¶§ƒ˚foo˙∆˚¬"))
fmt.Println(Kebab("•¶§ƒ˚foo˙∆˚¬bar"))
fmt.Println(Kebab("•¶§ƒ˚foo˙∆˚¬ bar"))
fmt.Println(Kebab("CStringRef"))
Output:

sample-2-text
base64-encode
foo-bar-baz
foo-bar-baz
foo-bar-baz
something-com
something
something-com
foo
foo-bar
foo-bar
cstring-ref
Example (Quoted)
fmt.Println(Kebab("\"hello world\""))
Output:

hello-world
Example (Swedish)
// Swedish and non ASCII char are not supported :(
// open an issue if this is something you need
//
// want:
// när-såg-du-en-kråka-väl-bita-en-man
fmt.Println(Kebab("När såg du en kråka väl bita en man?"))
Output:

n-r-s-g-du-en-kr-ka-v-l-bita-en-man
Example (WithSpace)
fmt.Println(Kebab("   sample   2    Text   "))
Output:

sample-2-text

func Pascal

func Pascal(s string) string

Pascal the given string.

Example
fmt.Println(Pascal("sample text"))
fmt.Println(Pascal("sample-text"))
fmt.Println(Pascal("sample_text"))
fmt.Println(Pascal("sample___text"))
fmt.Println(Pascal("sampleText"))
fmt.Println(Pascal(">> samPLE text <<"))
fmt.Println(Pascal("sample 2 Text"))
fmt.Println(Pascal("SAMPLE 2 TEXT"))
fmt.Println(Pascal("invite-Your-Customers-Add-Invites"))
fmt.Println(Pascal("2FA Enabled"))
fmt.Println(Pascal("Enabled 2FA"))
Output:

SampleText
SampleText
SampleText
SampleText
SampleText
SamPleText
Sample2Text
Sample2Text
InviteYourCustomersAddInvites
2FaEnabled
Enabled2Fa
Example (Cryptic)
fmt.Println(Pascal("5test"))
fmt.Println(Pascal("test5"))
fmt.Println(Pascal("THE5r"))
fmt.Println(Pascal("5TEst"))
fmt.Println(Pascal("_5TEst"))
fmt.Println(Pascal("@%#&5TEst"))
fmt.Println(Pascal("edf_6N"))
fmt.Println(Pascal("f_pX9"))
fmt.Println(Pascal("p_z9Rg"))
fmt.Println(Pascal("@49L0S145_¬fwHƒ0TSLNVp"))
fmt.Println(Pascal("lk0B@bFmjrLQ_Z6YL"))
Output:

5Test
Test5
The5R
5Test
5Test
5Test
Edf6N
FPX9
PZ9Rg
49L0S145FwH0Tslnvp
Lk0BBFmjrLqZ6Yl
Example (Long)
fmt.Println(Pascal("super long sentence that is not really necessary but we need to try it"))
Output:

SuperLongSentenceThatIsNotReallyNecessaryButWeNeedToTryIt
Example (Multiline)
fmt.Println(
	Pascal(
		`here
is
a
multiline
string`,
	),
)
Output:

HereIsAMultilineString
Example (NonASCII)
fmt.Println(Pascal("   $#$sample   2    Text   "))
fmt.Println(Pascal("___$$Base64Encode"))
fmt.Println(Pascal("FOO#BAR#BAZ"))
fmt.Println(Pascal("FOO:BAR$BAZ"))
fmt.Println(Pascal("FOO#BAR#BAZ"))
fmt.Println(Pascal("something.com"))
fmt.Println(Pascal("$something%"))
fmt.Println(Pascal("something.com"))
fmt.Println(Pascal("•¶§ƒ˚foo˙∆˚¬"))
fmt.Println(Pascal("•¶§ƒ˚foo˙∆˚¬bar"))
fmt.Println(Pascal("•¶§ƒ˚foo˙∆˚¬ bar"))
fmt.Println(Pascal("CStringRef"))
Output:

Sample2Text
Base64Encode
FooBarBaz
FooBarBaz
FooBarBaz
SomethingCom
Something
SomethingCom
Foo
FooBar
FooBar
CstringRef
Example (Quoted)
fmt.Println(Pascal("\"hello world\""))
Output:

HelloWorld
Example (Swedish)
// Swedish and non ASCII char are not supported :(
// open an issue if this is something you need
//
// want:
// när-såg-du-en-kråka-väl-bita-en-man
fmt.Println(Pascal("När såg du en kråka väl bita en man?"))
Output:

NRSGDuEnKrKaVLBitaEnMan
Example (WithSpace)
fmt.Println(Pascal("   sample   2    Text   "))
Output:

Sample2Text

func Snake

func Snake(s string) string

Snake transforms the given string as `snake_case`.

Example
fmt.Println(Snake("sample text"))
fmt.Println(Snake("sample-text"))
fmt.Println(Snake("sample_text"))
fmt.Println(Snake("sample___text"))
fmt.Println(Snake("sampleText"))
fmt.Println(Snake(">> samPLE text <<"))
fmt.Println(Snake("sample 2 Text"))
fmt.Println(Snake("SAMPLE 2 TEXT"))
fmt.Println(Snake("inviteYourCustomersAddInvites"))
fmt.Println(Snake("2FA Enabled"))
fmt.Println(Snake("Enabled 2FA"))
Output:

sample_text
sample_text
sample_text
sample_text
sample_text
sam_ple_text
sample_2_text
sample_2_text
invite_your_customers_add_invites
2fa_enabled
enabled_2fa
Example (Multiline)
fmt.Println(
	Snake(
		`here
is
a
multiline
string`,
	),
)
Output:

here_is_a_multiline_string
Example (NonASCII)
fmt.Println(Snake("   $#$sample   2    Text   "))
fmt.Println(Snake("___$$Base64Encode"))
fmt.Println(Snake("FOO#BAR#BAZ"))
fmt.Println(Snake("FOO:BAR$BAZ"))
fmt.Println(Snake("FOO#BAR#BAZ"))
fmt.Println(Snake("something.com"))
fmt.Println(Snake("$something%"))
fmt.Println(Snake("something.com"))
fmt.Println(Snake("•¶§ƒ˚foo˙∆˚¬"))
fmt.Println(Snake("•¶§ƒ˚foo˙∆˚¬bar"))
fmt.Println(Snake("•¶§ƒ˚foo˙∆˚¬ bar"))
fmt.Println(Snake("CStringRef"))
Output:

sample_2_text
base64_encode
foo_bar_baz
foo_bar_baz
foo_bar_baz
something_com
something
something_com
foo
foo_bar
foo_bar
cstring_ref
Example (Quoted)
fmt.Println(Snake("\"hello world\""))
Output:

hello_world
Example (Swedish)
// Swedish and non ASCII char are not supported :(
// open an issue if this is something you need
//
// want:
// när-såg-du-en-kråka-väl-bita-en-man
fmt.Println(Snake("När såg du en kråka väl bita en man?"))
Output:

n_r_s_g_du_en_kr_ka_v_l_bita_en_man
Example (WithSpace)
fmt.Println(Snake("   sample   2    Text   "))
Output:

sample_2_text

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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