Documentation
¶
Index ¶
- Constants
- Variables
- func FormatToken(style Style, index int, tok token.Token) string
- func LowerFirst[T ~string](str T) T
- func ToCamel[T ~string](str T, options ...Opts) T
- func ToDelimited[T ~string](str T, delimiter rune, lowercase bool, options ...Opts) T
- func ToDot[T ~string](str T, options ...Opts) T
- func ToKebab[T ~string](str T, options ...Opts) T
- func ToLowerCamel[T ~string](str T, options ...Opts) T
- func ToScreamingDot[T ~string](str T, options ...Opts) T
- func ToScreamingKebab[T ~string](str T, options ...Opts) T
- func ToScreamingSnake[T ~string](str T, options ...Opts) T
- func ToSnake[T ~string](str T, options ...Opts) T
- func ToTitle[T ~string](str T, options ...Opts) T
- func UpperFirst[T ~string](str T) T
- func WithoutNumbers[T ~string](s T) T
- type Converter
- type ConverterImpl
- func (f ConverterImpl) Contains(key string) bool
- func (r ConverterImpl) Convert(style Style, repStyle ReplaceStyle, input string, join string, ...) string
- func (r *ConverterImpl) Delete(key string)
- func (r ConverterImpl) Lookup(key string) *Replacement
- func (r ConverterImpl) Replacements() []Replacement
- func (r *ConverterImpl) Set(key, value string)
- func (r ConverterImpl) Table(key string) map[string]token.Token
- type Opts
- type ReplaceStyle
- type Replacement
- type Style
- type Tokenizer
- type TokenizerImpl
Examples ¶
Constants ¶
const ( ReplaceStyleNotSpecified = iota ReplaceStyleCamel // Text should be replaced with the Camel variant (e.g. "Json"). ReplaceStyleScreaming // Text should be replaced with the screaming variant (e.g. "JSON"). ReplaceStyleLower // Text should be replaced with the lowercase variant (e.g. "json"). )
const ( // DEFAULT_DELIMITERS is the default set of delimiters in string convert. DEFAULT_DELIMITERS string = " _.!?:;$-(){}[]#@&+~" )
Variables ¶
var ( // DefaultTokenizer is the default Tokenizer. DefaultTokenizer TokenizerImpl = NewTokenizer(DEFAULT_DELIMITERS) // DefaultReplacements is the list of Replacements passed to DefaultConverter. // // {"Http", "HTTP"}, // {"Https", "HTTPS"}, // {"Id", "ID"}, // {"Ip", "IP"}, // {"Html", "HTML"}, // {"Xml", "XML"}, // {"Json", "JSON"}, // {"Csv", "CSV"}, // {"Aws", "AWS"}, // {"Gcp", "GCP"}, // {"Sql", "SQL"}, DefaultReplacements []Replacement = []Replacement{ {"Http", "HTTP"}, {"Https", "HTTPS"}, {"Id", "ID"}, {"Ip", "IP"}, {"Html", "HTML"}, {"Xml", "XML"}, {"Json", "JSON"}, {"Csv", "CSV"}, {"Aws", "AWS"}, {"Gcp", "GCP"}, {"Sql", "SQL"}, } // // replacements: // DefaultReplacements: // { UpperCamel: "Http", Screaming: "HTTP" }, // { UpperCamel: "Https", Screaming: "HTTPS" }, // { UpperCamel: "Html", Screaming: "HTML" }, // { UpperCamel: "Xml", Screaming: "XML" }, // { UpperCamel: "Json", Screaming: "JSON" }, // { UpperCamel: "Csv", Screaming: "CSV" }, // { UpperCamel: "Aws", Screaming: "AWS" }, // { UpperCamel: "Gcp", Screaming: "GCP" }, // { UpperCamel: "Sql", Screaming: "SQL" }, // // tokenizer: // DefaultTokenizer DefaultConverter = NewConverter(DefaultReplacements, DefaultTokenizer) )
Functions ¶
func FormatToken ¶
FormatToken formats the token with the desired style.
func LowerFirst ¶
func LowerFirst[T ~string](str T) T
LowerFirst converts the first rune of str to lowercase.
func ToCamel ¶
ToCamel transforms the case of str into Camel Case (e.g. AnExampleString) using either the provided Converter or the DefaultConverter otherwise.
The default Converter detects case so that "AN_EXAMPLE_STRING" becomes "AnExampleString". It also has a configurable set of replacements, such that "some_json" becomes "SomeJSON" so long as opts.ReplacementStyle is set to ReplaceStyleScreaming. A ReplaceStyle of ReplaceStyleCamel would result in "SomeJson".
caps.ToCamel("This is [an] {example}${id32}.") // ThisIsAnExampleID32
caps.ToCamel("AN_EXAMPLE_STRING", ) // AnExampleString
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToCamel("This is [an] {example}${id32}."))
fmt.Println(caps.ToCamel("AN_EXAMPLE_STRING"))
}
Output: ThisIsAnExampleID32 AnExampleString
func ToDelimited ¶
ToDelimited transforms the case of str into a string seperated by delimiter, using either the provided Converter or the DefaultConverter otherwise.
If lowercase is false, the output will be all uppercase.
See Options for more information on available configuration ¶
Example ¶
caps.ToDelimited("This is [an] {example}${id}.#32", '.', true) // this.is.an.example.id.32
caps.ToDelimited("This is [an] {example}${id}.break32", '.', false) // THIS.IS.AN.EXAMPLE.ID.BREAK.32
caps.ToDelimited("This is [an] {example}${id}.v32", '.', true, caps.Opts{AllowedSymbols: "$"}) // this.is.an.example.$.id.v32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
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 ToDot ¶
ToDot transforms the case of str into Lower Dot Notation Case (e.g. an.example.string) using either the provided Converter or the DefaultConverter otherwise.
caps.ToDot("This is [an] {example}${id32}.") // this.is.an.example.id.32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToDot("This is [an] {example}${id32}."))
}
Output: this.is.an.example.id.32
func ToKebab ¶
ToKebab transforms the case of str into Lower Kebab Case (e.g. an-example-string) using either the provided Converter or the DefaultConverter otherwise.
caps.ToKebab("This is [an] {example}${id32}.") // this-is-an-example-id-32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToKebab("This is [an] {example}${id32}."))
}
Output: this-is-an-example-id-32
func ToLowerCamel ¶
ToLowerCamel transforms the case of str into Lower Camel Case (e.g. anExampleString) using either the provided Converter or the DefaultConverter otherwise.
The default Converter detects case so that "AN_EXAMPLE_STRING" becomes "anExampleString". It also has a configurable set of replacements, such that "some_json" becomes "someJSON" so long as opts.ReplacementStyle is set to ReplaceStyleScreaming. A ReplaceStyle of ReplaceStyleCamel would result in "someJson".
caps.ToLowerCamel("This is [an] {example}${id32}.") // thisIsAnExampleID32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToLowerCamel("This is [an] {example}${id32}."))
}
Output: thisIsAnExampleID32
func ToScreamingDot ¶
ToScreamingKebab transforms the case of str into Screaming Kebab Case (e.g. AN-EXAMPLE-STRING) using either the provided Converter or the DefaultConverter otherwise.
caps.ToScreamingDot("This is [an] {example}${id32}.") // THIS.IS.AN.EXAMPLE.ID.32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToScreamingDot("This is [an] {example}${id32}."))
}
Output: THIS.IS.AN.EXAMPLE.ID.32
func ToScreamingKebab ¶
ToScreamingKebab transforms the case of str into Screaming Kebab Snake (e.g. AN-EXAMPLE-STRING) using either the provided Converter or the DefaultConverter otherwise.
caps.ToScreamingKebab("This is [an] {example}${id32}.") // THIS-IS-AN-EXAMPLE-ID-32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToScreamingKebab("This is [an] {example}${id32}."))
}
Output: THIS-IS-AN-EXAMPLE-ID-32
func ToScreamingSnake ¶
ToScreamingSnake transforms the case of str into Screaming Snake Case (e.g. AN_EXAMPLE_STRING) using either the provided Converter or the DefaultConverter otherwise.
caps.ToScreamingSnake("This is [an] {example}${id32}.") // THIS_IS_AN_EXAMPLE_ID_32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToScreamingSnake("This is [an] {example}${id32}."))
}
Output: THIS_IS_AN_EXAMPLE_ID_32
func ToSnake ¶
ToSnake transforms the case of str into Lower Snake Case (e.g. an_example_string) using either the provided Converter or the DefaultConverter otherwise.
caps.ToSnake("This is [an] {example}${id32}.") // this_is_an_example_id_32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
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 ToTitle ¶
ToTitle transforms the case of str into Title Case (e.g. An Example String) using either the provided Converter or the DefaultConverter otherwise.
caps.ToTitle("This is [an] {example}${id32}.") // This Is An Example ID 32
Example ¶
package main
import (
"fmt"
"github.com/chanced/caps"
)
func main() {
fmt.Println(caps.ToTitle("This is [an] {example}${id32}."))
}
Output: This Is An Example ID 32
func UpperFirst ¶
func UpperFirst[T ~string](str T) T
UpperFirst converts the first rune of str to uppercase.
func WithoutNumbers ¶
func WithoutNumbers[T ~string](s T) T
Without numbers returns the string with numbers removed.
Types ¶
type Converter ¶ added in v0.2.0
type Converter interface {
Convert(style Style, repStyle ReplaceStyle, input string, join string, allowedSymbols []rune, numberRules map[rune]func(index int, r rune, val []rune) bool) string
}
Converter is an interface satisfied by types which can convert the case of a string.
ConverterImpl is provided as a default implementation. If you have edge cases which require custom formatting, you can implement your own Converter by wrapping ConverterImpl:
type MyConverter struct {}
func(MyConverter) Convert(style Style, repStyle ReplaceStyle, input string, join string, allowedSymbols []rune, numberRules map[rune]func(index int, r rune, val []rune) bool) string{
formatted := caps.DefaultConverter.Convert(style, repStyle, input, join, allowedSymbols, numberRules)
if formatted == "something_unusual" {
return "replaced"
}
return formatted
}
Parameters ¶
style: Expected output caps.Style of the string. repStyle: The caps.ReplaceStyle to use if a word needs to be replaced. join: The delimiter to use when joining the words. For Camel, this is an empty string. allowedSymbols: The set of allowed symbols. If set, these should take precedence over any delimiters numberRules: Any custom rules dictating how to handle special characters in numbers.
type ConverterImpl ¶ added in v0.2.0
type ConverterImpl struct {
// contains filtered or unexported fields
}
ConverterImpl contains a table of words to their desired replacement. Tokens will be compared against the keys of this table to determine if the string should be replaced with the value of the table.
This is primarily designed for acronyms but it could be used for other purposes.
The default Replacements:
{ "Http", "HTTP" },
{ "Https", "HTTPS" },
{ "Html", "HTML" },
{ "Xml", "XML" },
{ "Json", "JSON" },
{ "Csv", "CSV" },
{ "Aws", "AWS" },
{ "Gcp", "GCP" },
{ "Sql", "SQL" },
func NewConverter ¶ added in v0.2.0
func NewConverter(replacements []Replacement, tokenizer Tokenizer) ConverterImpl
NewConverter creates a new Converter which is used to convert the input text to the desired output.
replacements are used to make replacements of tokens to the specified formatting (e.g. { "Json", "JSON"}).
tokenizer is used to tokenize the input text.
func (ConverterImpl) Contains ¶ added in v0.2.0
func (f ConverterImpl) Contains(key string) bool
Contains reports whether a key is in the Converter's replacement table.
func (ConverterImpl) Convert ¶ added in v0.2.0
func (r ConverterImpl) Convert(style Style, repStyle ReplaceStyle, input string, join string, allowedSymbols []rune, numberRules map[rune]func(index int, r rune, val []rune) bool) string
Convert formats the string with the desired style.
func (*ConverterImpl) Delete ¶ added in v0.2.0
func (r *ConverterImpl) Delete(key string)
Remove deletes the key from the map. Either variant is sufficient.
func (ConverterImpl) Lookup ¶ added in v0.2.0
func (r ConverterImpl) Lookup(key string) *Replacement
Lookup returns the Replacement for the given key, returning nil if it does not exist.
func (ConverterImpl) Replacements ¶ added in v0.2.0
func (r ConverterImpl) Replacements() []Replacement
Replacements returns a slice of Replacement in the lookup table.
func (*ConverterImpl) Set ¶ added in v0.2.0
func (r *ConverterImpl) Set(key, value string)
Set adds the key/value pair to the table.
type Opts ¶
type Opts struct {
// Any characters within this string will be allowed in the output.
//
// Default:
// ""
AllowedSymbols string
// The Converter to use.
//
// Default:
// DefaultConverter
Converter Converter
// Styles overwrites the way words are replaced.
//
// A typical call to ToLowerCamel for "ServeJSON" with a Replacer that
// contains {"Json": "JSON"} would result in "serveJSON" by using the
// StyleScreaming variant. If ReplacementStyle was set to
// ReplaceStyleUpperCamel, on the call to ToLowerCamel then the result would
// be "serveHttp".
//
// The default replacement style is dependent upon the target casing.
ReplaceStyle ReplaceStyle
// NumberRules are used by the DefaultTokenizer to augment the standard
// rules for determining if a rune is part of a number.
//
// Note, if you add special characters here, they must be present in the
// AllowedSymbols string for them to be part of the output.
NumberRules map[rune]func(index int, r rune, val []rune) bool
}
Opts include configurable options for case conversion.
See the documentation for the individual fields for more information.
type ReplaceStyle ¶
type ReplaceStyle uint8
ReplaceStyle is used to indicate the case style the text should be transformed to when seeking replacement text in a Converter.
When a Replacer is configured, the expected input is:
caps.Replacement{ Camel: "Json", Screaming: "JSON" }
If the ReplaceStyle equals ReplaceStyleScreaming then an input of "MarshalJson" will return "MarshaalJSON" with the above caps.Replacement.
type Replacement ¶
type Tokenizer ¶
type Tokenizer interface {
Tokenize(value string, allowedSymbols []rune, numberRules map[rune]func(index int, r rune, val []rune) bool) []token.Token
}
Tokenizer is an interface satisfied by tyeps which can
type TokenizerImpl ¶
type TokenizerImpl struct {
// contains filtered or unexported fields
}
TokenizerImpl is the provided implementation of the Tokenizer interface.
TokenizerImpl tokenizes the input text into token.Tokens based on a set of delimiters (runes).
If you need custom logic, consider wrapping the logic by implementing Tokenizer and then calling a TokenizerImpl's Tokenize method.
Example: ¶
func NewTokenizer ¶
func NewTokenizer(delimiters string) TokenizerImpl
NewTokenizer creates and returns a new TokenizerImpl which implements the Tokenizer interface.
Tokenizers are used by ConverterImpl to tokenize the input text into token.Tokens that are then formatted.
func (TokenizerImpl) Tokenize ¶
func (t TokenizerImpl) Tokenize(str string, allowedSymbols []rune, numberRules map[rune]func(index int, r rune, val []rune) bool) []token.Token
Tokenize splits a string into a list of token.Tokens based on the case of each rune, it's delimiters, and the specified allowedSymbols.
For example:
t.Tokenize("ASnakecaseVariable", nil) -> ["A", "Snakecase", "Variable"]
Tokenizer attempts to detect formatting, such that a screaming snakecase str will be inferred accordingly.
For example:
t.Tokenize("A_SCREAMING_SNAKECASE_VARIABLE", nil) -> ["A", "SCREAMING", "SNAKECASE", "VARIABLE"]
If allowedSymbols is not nil, then those symbols will be treated as non-delimiters even if they are in the delimiters list.
For example:
t := caps.token.Newizer("_")
t.Tokenize("A_SCREAMING_SNAKECASE_VARIABLE", []rune{'_'}) -> ["A_SCREAMING_SNAKECASE_VARIABLE"]