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 ¶
- Camel
- Camel (Cryptic)
- Camel (Long)
- Camel (Multiline)
- Camel (NonASCII)
- Camel (Quoted)
- Camel (Swedish)
- Camel (WithSpace)
- Kebab
- Kebab (Cryptic)
- Kebab (Long)
- Kebab (Multiline)
- Kebab (NonASCII)
- Kebab (Quoted)
- Kebab (Swedish)
- Kebab (WithSpace)
- Pascal
- Pascal (Cryptic)
- Pascal (Long)
- Pascal (Multiline)
- Pascal (NonASCII)
- Pascal (Quoted)
- Pascal (Swedish)
- Pascal (WithSpace)
- Snake
- Snake (Multiline)
- Snake (NonASCII)
- Snake (Quoted)
- Snake (Swedish)
- Snake (WithSpace)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Camel ¶
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 ¶
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 ¶
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 ¶
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.