A string case converter for use in Golang applications.
import "github.com/zephinzer/go-strcase"
Use this if you want your string to lookLikeThisFormat
fmt.Printf("camel case: %s", strcase.ToCamel("hello world"))
// output: helloWorld
Useful for: C-like sematics, JSON, YAML
Use this if you want your string to LookLikeThis
fmt.Printf("capital case: %s", strcase.ToCamel("hello world"))
// output: HelloWorld
Aliases: ToCapitalCamel
, ToPascal
Useful for: C-like sematics
Use this if you want your string to looklikethis
fmt.Printf("flat case: %s", strcase.ToFlat("helloGreatBigWorld"))
// output: hellogreatbigworld
Aliases: ToLazy
Useful for: Java/Gopackage names
Use this if you want your string to look-like-this
fmt.Printf("lower kebab case: %s", strcase.ToCamel("hello world"))
// output: hello-world
Aliases: ToCaterpillar
, ToCSS
, ToDash
, ToHyphen
, ToLazy
, ToLisp
Useful for: JSON, YAML
Use this if you want your string to look_like_this
fmt.Printf("lower snake case: %s", strcase.ToCamel("hello world"))
// output: hello_world
Useful for: Python, Perl, Shell, JSON, YAML
Use this if you want your string to LOOK-LIKE-THIS
fmt.Printf("upper kebab case: %s", strcase.ToCamel("hello world"))
// output: HELLO-WORLD
Aliases: ToMacro
Useful for: ?
Use this if you want your string to LOOK_LIKE_THIS
fmt.Printf("upper snake case: %s", strcase.ToCamel("hello world"))
// output: HELLO_WORLD
Useful for: Shell, JSON, YAML
- Only dashes, underscores, spaces are considered as character delimtiers (eg.
a-pizza
,a_pizza
, anda pizza
are seens asa
andpizza
) - Character delimitations take precedence over the other delimiters (eg.
i want 1 Pizza
is[i, want, 1, Pizza]
, butiWant1Pizza
is[i, want1, pizza]
)
- An upper-cased character is considered a delimiter only when the character after that is a lower-cased character (eg.
APizza
is seen asA
andPizza
, butsendAPIRequest
is seen as[send, api, request]
)
- The end of any series of numbers is considered a delimiter. This decision was made since variable names usually cannot begin with a number (eg.
1pizza
is[1, pizza]
, butpizza1
is[pizza1]
)
- After writing the above, the world pizza doesn't hold any more meaning in my mind. Send help.
This package was created mainly for usage within a tool that needs to handle cross-runtime naming conventions such as between SHELL_VARIABLES
and javascriptVars
or python_vars
This package works by first tokenizing the input string into separate words first before applying any transformations. This looks like:
HelloWorld => [hello, world]
helloWorld => [hello, world]
10PizzasPlease => [10, pizzas, please]
1-for-1 => [1, for, 1]
1for1 => [1, for1]
After the above tokenization, the transformations are added:
[hello, world] =(ToCamel)=> helloWorld
[10, pizzas, please] =(ToCamel)=> 10PizzasPlease
[1, for, 1] =(ToCamel)=> 1For1
[1, for1] =(ToCamel)=> 1For1
The working repository is at Gitlab at https://gitlab.com/zephinzer/go-strcase, if you are seeing this on Github, it's just for SEO since y'know all the cool new kids are on Github 😂
A rough changelog when the contributors can remember to add it is here:
Version | Description |
---|---|
v1.0.1 |
Added code comments for just-in-time documentation, added alises for a few of the case names |
v1.0.0 |
Initial release with support for camelCase , CapitalCase , lower-kebab-case , lower_snake_case , UPPER-KEBAB-CASE , UPPER_SNAKE_CASE |
Use this anywhere you need to. Licensed under the MIT license