-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# caps | ||
|
||
caps is a case conversion library for Go. It was built with the following | ||
priorites: configurability, consistency, correctness, ergonomic, and performant | ||
in mind, in that order. | ||
|
||
Out of the box, the following case conversion are supported: | ||
|
||
- Camel Case (e.g. CamelCase) | ||
- Lower Camel Case (e.g. lowerCamelCase) | ||
- Snake Case (e.g. snake_case) | ||
- Screaming Snake Case (e.g. SCREAMING_SNAKE_CASE) | ||
- Kebab Case (e.g. kebab-case) | ||
- Screaming Kebab Case(e.g. SCREAMING-KEBAB-CASE) | ||
- Dot Notation Case (e.g. dot.notation.case) | ||
- Title Case (e.g. Title Case) | ||
- Other deliminations | ||
|
||
Word boundaries are determined by the `caps.Formatter`. The provided implementation, `caps.FormatterImpl`, | ||
delegates the boundary detection to `caps.Tokenizer`. The provided implementation, `caps.TokenizerImpl`, | ||
uses the following tokens as delimiters: `" _.!?:;$-(){}[]#@&+~"`. | ||
|
||
`caps.StdFormatter` also allows users to register `caps.Replacement`s for acronym replacement. The default list is: | ||
|
||
```go | ||
{"Http", "HTTP"} | ||
{"Https", "HTTPS"} | ||
{"Id", "ID"} | ||
{"Ip", "IP"} | ||
{"Html", "HTML"} | ||
{"Xml", "XML"} | ||
{"Json", "JSON"} | ||
{"Csv", "CSV"} | ||
{"Aws", "AWS"} | ||
{"Gcp", "GCP"} | ||
{"Sql", "SQL"} | ||
``` | ||
|
||
If you would like to add or remove entries from that list, you have a few | ||
options. | ||
|
||
You can pass a new instance of `caps.StdFormatter` with a new set of | ||
`caps.Replacement` (likely preferred). | ||
|
||
You can create your own `caps.Formatter`. This could be as simple as | ||
implementing the single `Format` method, calling `caps.DefaultFormatter.Format`, | ||
and then modifying the result. | ||
|
||
Finally, if you are so inclined, you can update `caps.DefaultFormatter`. Just be aware that the | ||
module was not built with thread-safety in mind so you should set it once. | ||
Otherwise, you'll need guard your usage of the library accordingly. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,76 @@ | ||
package caps_test | ||
|
||
func ExampleToCamel() { | ||
// fmt.Println(caps.ToCamel("This is [an] {example}${id32}.")) | ||
// fmt.Println(caps.ToCamel("This is [an] {example}${id32}.break32")) | ||
// fmt.Println(caps.ToCamel("This example allows for $ symbols", caps.Opts{AllowedSymbols: "$"})) | ||
import ( | ||
"fmt" | ||
|
||
// customReplacer := caps.NewFormatter([]caps.R{{"Http", "HTTP"}, {"Https", "HTTPS"}}) | ||
// fmt.Println(caps.ToCamel("No Id just http And Https", caps.Opts{Formatter: customReplacer})) | ||
"github.com/chanced/caps" | ||
) | ||
|
||
// Outputx: | ||
func ExampleToCamel() { | ||
fmt.Println(caps.ToCamel("This is [an] {example}${id32}.")) | ||
fmt.Println(caps.ToCamel("AN_EXAMPLE_STRING")) | ||
// Output: | ||
// ThisIsAnExampleID32 | ||
// AnExampleString | ||
} | ||
|
||
func ExampleToLowerCamel() { | ||
fmt.Println(caps.ToLowerCamel("This is [an] {example}${id32}.")) | ||
// Output: | ||
// thisIsAnExampleID32 | ||
// thisIsAnExampleID32Break32 | ||
// thisExampleAllowsFor$symbols | ||
// noIdJustHTTPAndHTTPS | ||
} | ||
|
||
func ExampleToSnake() { | ||
fmt.Println(caps.ToSnake("This is [an] {example}${id32}.")) | ||
fmt.Println(caps.ToSnake("v3.2.2")) | ||
// Output: | ||
// this_is_an_example_id_32 | ||
// v3_2_2 | ||
} | ||
|
||
func ExampleToScreamingSnake() { | ||
fmt.Println(caps.ToScreamingSnake("This is [an] {example}${id32}.")) | ||
// Output: | ||
// THIS_IS_AN_EXAMPLE_ID_32 | ||
} | ||
|
||
func ExampleToKebab() { | ||
fmt.Println(caps.ToKebab("This is [an] {example}${id32}.")) | ||
// Output: | ||
// this-is-an-example-id-32 | ||
} | ||
|
||
func ExampleToScreamingKebab() { | ||
fmt.Println(caps.ToScreamingKebab("This is [an] {example}${id32}.")) | ||
// Output: | ||
// THIS-IS-AN-EXAMPLE-ID-32 | ||
} | ||
|
||
func ExampleToDot() { | ||
fmt.Println(caps.ToDot("This is [an] {example}${id32}.")) | ||
// Output: | ||
// this.is.an.example.id.32 | ||
} | ||
|
||
func ExampleToScreamingDot() { | ||
fmt.Println(caps.ToScreamingDot("This is [an] {example}${id32}.")) | ||
// Output: | ||
// THIS.IS.AN.EXAMPLE.ID.32 | ||
} | ||
|
||
func ExampleToDelimited() { | ||
// fmt.Println(caps.ToDelimited("A # B _ C", '.', true)) | ||
// fmt.Println(caps.ToDelimited("$id", '.', false)) | ||
// fmt.Println(caps.ToDelimited("$id", '.', true, caps.Opts{AllowedSymbols: "$"})) | ||
// fmt.Println(caps.ToDelimited("fromCamelcaseString", '.', true)) | ||
// Outputx: | ||
// a.b.c | ||
// ID | ||
// $id | ||
// from.camelcase.string | ||
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.#32", '.', true)) | ||
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.break32", '.', false)) | ||
fmt.Println(caps.ToDelimited("This is [an] {example}${id}.v32", '.', true, caps.Opts{AllowedSymbols: "$"})) | ||
|
||
// Output: | ||
// this.is.an.example.id.32 | ||
// THIS.IS.AN.EXAMPLE.ID.BREAK.32 | ||
// this.is.an.example.$.id.v32 | ||
} | ||
|
||
func ExampleToSnake() { | ||
// fmt.Println(caps.ToSnake("A long string with spaces")) | ||
// fmt.Println(caps.ToSnake(strings.ToLower("A_SCREAMING_SNAKE_MUST_BE_LOWERED_FIRST"))) | ||
// fmt.Println(caps.ToSnake("$word", caps.Opts{AllowedSymbols: "$"})) | ||
// OutputX: | ||
// a_long_string_with_spaces | ||
// a_screaming_snake_must_be_lowered_first | ||
// $word | ||
func ExampleToTitle() { | ||
fmt.Println(caps.ToTitle("This is [an] {example}${id32}.")) | ||
// Output: | ||
// This Is An Example ID 32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/chanced/caps | ||
|
||
go 1.19 | ||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
package text | ||
|
||
import "strings" | ||
// import "strings" | ||
|
||
type Text string | ||
// type Text string | ||
|
||
func (t Text) String() string { | ||
return string(t) | ||
} | ||
// func (t Text) String() string { | ||
// return string(t) | ||
// } | ||
|
||
func (t Text) ToLower() Text { | ||
return Text(strings.ToLower(t.String())) | ||
} | ||
// func (t Text) ToLower() Text { | ||
// return Text(strings.ToLower(t.String())) | ||
// } | ||
|
||
func (t Text) ToUpper() Text { | ||
return Text(strings.ToUpper(t.String())) | ||
} | ||
// func (t Text) ToUpper() Text { | ||
// return Text(strings.ToUpper(t.String())) | ||
// } | ||
|
||
func (t Text) ToSnake() Text { | ||
panic("not implemented") | ||
} | ||
// func (t Text) ToSnake() Text { | ||
// panic("not implemented") | ||
// } | ||
|
||
func (t Text) ToScreamingSnake() Text { | ||
// return Text(strcase.ToScreamingSnake(t.String())) | ||
panic("not implemented") | ||
} | ||
// func (t Text) ToScreamingSnake() Text { | ||
// // return Text(strcase.ToScreamingSnake(t.String())) | ||
// panic("not implemented") | ||
// } | ||
|
||
func (t Text) ReplaceAll(old string, new string) Text { | ||
return Text(strings.Replace(t.String(), old, new, -1)) | ||
} | ||
// func (t Text) ReplaceAll(old string, new string) Text { | ||
// return Text(strings.Replace(t.String(), old, new, -1)) | ||
// } | ||
|
||
func (t Text) Replace(old, new string, n int) Text { | ||
return Text(strings.Replace(t.String(), old, new, n)) | ||
} | ||
// func (t Text) Replace(old, new string, n int) Text { | ||
// return Text(strings.Replace(t.String(), old, new, n)) | ||
// } |