Documentation ¶
Overview ¶
Package caps is a unicode aware case conversion library.
Index ¶
- Constants
- Variables
- func FormatToken(caser token.Caser, style Style, index int, tok string) string
- func LowerFirst[T ~string](str T) T
- func ToCamel[T ~string](str T, options ...Opts) T
- func ToDelimited[T ~string](str T, delimiter string, lowercase bool, options ...Opts) T
- func ToDotNotation[T ~string](str T, options ...Opts) T
- func ToKebab[T ~string](str T, options ...Opts) T
- func ToLower[T ~string](str T) T
- func ToLowerCamel[T ~string](str T, options ...Opts) T
- func ToScreamingDotNotation[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 ToUpper[T ~string](str T) T
- func UpperFirst[T ~string](str T) T
- func WithoutNumbers[T ~string](s T) T
- type Caps
- func (c Caps) AllowedSymbols() string
- func (c Caps) Converter() Converter
- func (c Caps) LowerFirst(str string) string
- func (c Caps) NumberRules() token.NumberRules
- func (c Caps) ReplaceStyle() ReplaceStyle
- func (c Caps) ToCamel(str string) string
- func (c Caps) ToDelimited(str string, delimiter string, lowercase bool) string
- func (c Caps) ToDotNotation(str string) string
- func (c Caps) ToKebab(str string) string
- func (c Caps) ToLowerCamel(str string) string
- func (c Caps) ToScreamingDotNotation(str string) string
- func (c Caps) ToScreamingKebab(str string) string
- func (c Caps) ToScreamingSnake(str string) string
- func (c Caps) ToSnake(str string) string
- func (c Caps) ToTitle(str string) string
- func (c Caps) UpperFirst(str string) string
- func (c Caps) WithoutNumbers(s string) string
- type Config
- type ConvertRequest
- type Converter
- type ConverterImpldeprecated
- type NumberRules
- type Opts
- type ReplaceStyle
- type Replacement
- type StdConverter
- type StdTokenizer
- type Style
- type Tokenizer
- type TokenizerImpldeprecated
Examples ¶
Constants ¶
const ( // DEFAULT_DELIMITERS is the default set of delimiters in string convert. DEFAULT_DELIMITERS string = " _.!?:;$-(){}[]#@&+~" )
Variables ¶
var DefaultReplacements []Replacement = []Replacement{
{"Acl", "ACL"},
{"Api", "API"},
{"Ascii", "ASCII"},
{"Cpu", "CPU"},
{"Css", "CSS"},
{"Dns", "DNS"},
{"Eof", "EOF"},
{"Guid", "GUID"},
{"Html", "HTML"},
{"Http", "HTTP"},
{"Https", "HTTPS"},
{"Id", "ID"},
{"Ip", "IP"},
{"Json", "JSON"},
{"Lhs", "LHS"},
{"Qps", "QPS"},
{"Ram", "RAM"},
{"Rhs", "RHS"},
{"Rpc", "RPC"},
{"Sla", "SLA"},
{"Smtp", "SMTP"},
{"Sql", "SQL"},
{"Ssh", "SSH"},
{"Tcp", "TCP"},
{"Tls", "TLS"},
{"Ttl", "TTL"},
{"Udp", "UDP"},
{"Ui", "UI"},
{"Uid", "UID"},
{"Uuid", "UUID"},
{"Uri", "URI"},
{"Url", "URL"},
{"Utf8", "UTF8"},
{"Vm", "VM"},
{"Xml", "XML"},
{"Xmpp", "XMPP"},
{"Xsrf", "XSRF"},
{"Xss", "XSS"},
}
DefaultReplacements is the list of Replacements passed to DefaultConverter.
{"Acl", "ACL"}, {"Api", "API"}, {"Ascii", "ASCII"}, {"Cpu", "CPU"}, {"Css", "CSS"}, {"Dns", "DNS"}, {"Eof", "EOF"}, {"Guid", "GUID"}, {"Html", "HTML"}, {"Http", "HTTP"}, {"Https", "HTTPS"}, {"Id", "ID"}, {"Ip", "IP"}, {"Json", "JSON"}, {"Lhs", "LHS"}, {"Qps", "QPS"}, {"Ram", "RAM"}, {"Rhs", "RHS"}, {"Rpc", "RPC"}, {"Sla", "SLA"}, {"Smtp", "SMTP"}, {"Sql", "SQL"}, {"Ssh", "SSH"}, {"Tcp", "TCP"}, {"Tls", "TLS"}, {"Ttl", "TTL"}, {"Udp", "UDP"}, {"Ui", "UI"}, {"Uid", "UID"}, {"Uuid", "UUID"}, {"Uri", "URI"}, {"Url", "URL"}, {"Utf8", "UTF8"}, {"Vm", "VM"}, {"Xml", "XML"}, {"Xmpp", "XMPP"}, {"Xsrf", "XSRF"}, {"Xss", "XSS"},
Functions ¶
func FormatToken ¶
FormatToken formats the str 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 separated 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 ToDotNotation ¶ added in v0.3.0
ToDotNotation 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.ToDotNotation("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.ToDotNotation("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 ToLower ¶ added in v0.2.3
func ToLower[T ~string](str T) T
ToLower returns s with all Unicode letters mapped to their lower case.
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}.")) fmt.Println(caps.ToLowerCamel("entity id")) fmt.Println(caps.ToLowerCamel("entity id", caps.WithReplaceStyleCamel())) }
Output: thisIsAnExampleID32 entityID entityId
func ToScreamingDotNotation ¶ added in v0.3.0
ToScreamingDotNotation transforms the case of str into Screaming Kebab Case (e.g. AN.EXAMPLE.STRING) using either the provided Converter or the DefaultConverter otherwise.
caps.ToScreamingDotNotation("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.ToScreamingDotNotation("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 ToUpper ¶ added in v0.2.3
func ToUpper[T ~string](str T) T
ToUpper returns s with all Unicode letters mapped to their upper case.
func UpperFirst ¶
func UpperFirst[T ~string](str T) T
UpperFirst converts the first rune of str to unicode upper case.
This method does not support special cases (such as Turkish and Azeri)
func WithoutNumbers ¶
func WithoutNumbers[T ~string](s T) T
Without numbers returns the string with all numeric runes removed.
It does not currently use any logic to determine if a rune (e.g. '.') is part of a number. This may change in the future.
Types ¶
type Caps ¶ added in v0.7.0
type Caps struct {
// contains filtered or unexported fields
}
Caps provides configured case conversion methods.
func New ¶ added in v0.7.0
New returns a new Caps instance with the provided options.
if caser is nil, token.DefaultCaser is used (which relies on the default unicode functions)
func (Caps) AllowedSymbols ¶ added in v0.7.0
AllowedSymbols returns the configured AllowedSymbols of c
func (Caps) LowerFirst ¶ added in v0.7.0
LowerFirst converts the first rune of str to lowercase.
func (Caps) NumberRules ¶ added in v0.7.0
func (c Caps) NumberRules() token.NumberRules
NumberRules returns the configured NumberRules of c
func (Caps) ReplaceStyle ¶ added in v0.7.0
func (c Caps) ReplaceStyle() ReplaceStyle
ReplaceStyle returns the configured ReplaceStyle of c
func (Caps) ToCamel ¶ added in v0.7.0
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
func (Caps) ToDelimited ¶ added in v0.7.0
ToDelimited transforms the case of str into a string separated 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
func (Caps) ToDotNotation ¶ added in v0.7.0
ToDotNotation 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.ToDotNotation("This is [an] {example}${id32}.") // this.is.an.example.id.32
func (Caps) ToKebab ¶ added in v0.7.0
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
func (Caps) ToLowerCamel ¶ added in v0.7.0
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
func (Caps) ToScreamingDotNotation ¶ added in v0.7.0
ToScreamingDotNotation transforms the case of str into Screaming Kebab Case (e.g. AN.EXAMPLE.STRING) using either the provided Converter or the DefaultConverter otherwise.
caps.ToScreamingDotNotation("This is [an] {example}${id32}.") // THIS.IS.AN.EXAMPLE.ID.32
func (Caps) ToScreamingKebab ¶ added in v0.7.0
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
func (Caps) ToScreamingSnake ¶ added in v0.7.0
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
func (Caps) ToSnake ¶ added in v0.7.0
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
func (Caps) ToTitle ¶ added in v0.7.0
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
func (Caps) UpperFirst ¶ added in v0.7.0
UpperFirst converts the first rune of str to unicode upper case.
This method does not support special cases (such as Turkish and Azeri)
func (Caps) WithoutNumbers ¶ added in v0.7.0
Without numbers returns the string with all numeric runes removed.
It does not currently use any logic to determine if a rune (e.g. ".") is part of a number. This may change in the future.
type Config ¶ added in v0.7.6
type Config struct { // Any characters within this string will be allowed in the output. // // This does not affect delimiters (e.g. "_", "-", ".") as they are added // post-tokenization. // // Default: // "" AllowedSymbols string // The Converter to use. // // Default: // A StdConverter with the Replacements, Caser, and Tokenizer. Converter Converter // If not set, this will be DefaultReplacements. Replacements []Replacement // ReplaceStyle overwrites the way words are replaced. // // A typical call to ToLowerCamel for "ServeJSON" with a Converter that // contains {"Json": "JSON"} would result in "serveJSON" by using the // ReplaceStyleScreaming variant. If ReplacementStyle was set to // ReplaceStyleCamel, on the call to ToLowerCamel then the result would // be "serveHttp". // // The default replacement style is dependent upon the target Style. 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 token.NumberRules // Special unicode case rules. // See unicode.SpecialCase or token.Caser for more information. // // Default: token.DefaultCaser (which relies on the default unicode // functions) Caser token.Caser // If not set, uses StdTokenizer with the provided delimiters and token.Caser. Tokenizer Tokenizer }
Config include configurable options for Caps instances.
See the documentation for the individual fields for more information.
type ConvertRequest ¶ added in v0.5.0
type Converter ¶ added in v0.2.0
type Converter interface {
Convert(req ConvertRequest) 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.
var DefaultConverter Converter = NewConverter(DefaultReplacements, DefaultTokenizer, token.DefaultCaser)
DefaultConverter is the default Converter instance.
type ConverterImpl
deprecated
added in
v0.2.0
type ConverterImpl = StdConverter
Deprecated: Use StdConverter
type NumberRules ¶ added in v0.7.0
type NumberRules = token.NumberRules
type Opts ¶
type Opts struct { // Any characters within this string will be allowed in the output. // // This does not affect delimiters (e.g. '_', '-', '.') as they are added // post-tokenization. // // Default: // "" AllowedSymbols string // The Converter to use. // // Default: // DefaultConverter Converter Converter // ReplaceStyle overwrites the way Replacements are cased. // // A typical call to ToLowerCamel for "ServeJSON" with a Converter that // contains {"Json": "JSON"} would result in "serveJSON" by using the // ReplaceStyleScreaming variant. If ReplacementStyle was set to // ReplaceStyleCamel then a call to ToLowerCamel then the result would be // "serveHttp". // // The default ReplaceStyle is dependent upon the target Style. 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 token.NumberRules }
Opts include configurable options for case conversion.
See the documentation for the individual fields for more information.
func WithAllowedSymbols ¶ added in v0.7.5
WithAllowedSymbols sets the AllowedSymbols to use
func WithConverter ¶ added in v0.4.1
WithConverter sets the Converter to use
func WithNumberRules ¶ added in v0.4.1
func WithNumberRules(rules NumberRules) Opts
WithNumberRules sets the NumberRules to use
func WithReplaceStyle ¶ added in v0.4.1
func WithReplaceStyle(style ReplaceStyle) Opts
WithReplaceStyle sets the ReplaceStyle to use
There are also methods for each ReplaceStyle (e.g. WithReplaceStyleCamel)
func WithReplaceStyleCamel ¶ added in v0.4.1
func WithReplaceStyleCamel() Opts
WithReplaceStyleCamel indicates Replacements should use the Camel variant (e.g. "Json").
func WithReplaceStyleLower ¶ added in v0.4.1
func WithReplaceStyleLower() Opts
WithReplaceStyleLower indicates Replacements should use the lowercase variant (e.g. "json").
func WithReplaceStyleScreaming ¶ added in v0.4.1
func WithReplaceStyleScreaming() Opts
WithReplaceStyleScreaming indicates Replacements should use the screaming variant (e.g. "JSON").
type ReplaceStyle ¶
type ReplaceStyle uint8
ReplaceStyle is used to indicate the case style that text should be transformed to when performing replacement in a Converter.
For example, a call to ToCamel with a ReplaceStyleScreaming with an input of "MarshalJson" will return "MarshaalJSON" while ReplaceStyleCamel would return "MarshalJson"
const ( ReplaceStyleNotSpecified ReplaceStyle = 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"). )
func (ReplaceStyle) IsCamel ¶ added in v0.5.1
func (rs ReplaceStyle) IsCamel() bool
IsCamel returns true if rs equals ReplaceStyleCamel
func (ReplaceStyle) IsLower ¶ added in v0.5.1
func (rs ReplaceStyle) IsLower() bool
IsLower returns true if rs equals ReplaceStyleLower
func (ReplaceStyle) IsScreaming ¶ added in v0.5.1
func (rs ReplaceStyle) IsScreaming() bool
IsScreaming returns true if rs equals ReplaceStyleScreaming
func (ReplaceStyle) String ¶ added in v0.7.5
func (rs ReplaceStyle) String() string
type Replacement ¶
type StdConverter ¶ added in v0.4.9
type StdConverter struct {
// contains filtered or unexported fields
}
StdConverter 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 can be found in the DefaultReplacements variable.
func NewConverter ¶ added in v0.2.0
func NewConverter(replacements []Replacement, tokenizer Tokenizer, caser token.Caser) StdConverter
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 (StdConverter) Contains ¶ added in v0.4.9
func (sc StdConverter) Contains(key string) bool
Contains reports whether a key is in the Converter's replacement table.
func (StdConverter) Convert ¶ added in v0.4.9
func (sc StdConverter) Convert(req ConvertRequest) string
Convert formats the string with the desired style.
func (*StdConverter) Delete ¶ added in v0.4.9
func (sc *StdConverter) Delete(key string)
Remove deletes the key from the map. Either variant is sufficient.
func (StdConverter) Index ¶ added in v0.4.9
func (sc StdConverter) Index() index.Index
func (StdConverter) Replacements ¶ added in v0.4.9
func (sc StdConverter) Replacements() []Replacement
Replacements returns a slice of Replacement in the lookup trie.
func (*StdConverter) Set ¶ added in v0.4.9
func (sc *StdConverter) Set(key, value string)
Set adds the key/value pair to the table.
type StdTokenizer ¶ added in v0.4.9
type StdTokenizer struct {
// contains filtered or unexported fields
}
StdTokenizer is the provided implementation of the Tokenizer interface.
StdTokenizer 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 StdTokenizer's Tokenize method.
Example: ¶
var DefaultTokenizer StdTokenizer = NewTokenizer(DEFAULT_DELIMITERS, token.DefaultCaser)
DefaultTokenizer is the default Tokenizer.
func NewTokenizer ¶
func NewTokenizer(delimiters string, caser token.Caser) StdTokenizer
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 (StdTokenizer) Tokenize ¶ added in v0.4.9
func (ti StdTokenizer) Tokenize(str string, allowedSymbols string, numberRules NumberRules) []string
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"]
type Style ¶
type Style uint8
const ( StyleNotSpecified Style = iota StyleLower // The output should be lowercase (e.g. "an_example") StyleScreaming // The output should be screaming (e.g. "AN_EXAMPLE") StyleCamel // The output should be camel case (e.g. "AnExample") StyleLowerCamel // The output should be lower camel case (e.g. "anExample") )
func (Style) IsLowerCamel ¶ added in v0.5.1
func (Style) IsScreaming ¶ added in v0.5.1
type Tokenizer ¶
type Tokenizer interface {
Tokenize(value string, allowedSymbols string, numberRules NumberRules) []string
}
Tokenizer is an interface satisfied by tyeps which can
type TokenizerImpl
deprecated
type TokenizerImpl = StdTokenizer
Deprecated: Use StdTokenizer.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package index contains a trie index used by StdConverter to lookup Replacements.
|
Package index contains a trie index used by StdConverter to lookup Replacements. |
Package text contains a single Text type with functions from caps and strings as methods.
|
Package text contains a single Text type with functions from caps and strings as methods. |
Package token contains functions for working with a string as a token.
|
Package token contains functions for working with a string as a token. |