Documentation
¶
Overview ¶
Package text provides string utilities for the american-english language. Additional functions such as Pointer and Dereference are also provided.
- The following package was created for HTTP web-services and interfacing with external cloud-providers and APIs.
- For all functions where a golang.org/x/text/language.Tag is relevant, golang.org/x/text/language.AmericanEnglish is the default value.
- Note that most functions contain a special Variadic constructor to enable logging, configure a golang.org/x/text/language.Tag, and other settings.
- The package uses log/slog for logging purposes. Such configuration is forced and cannot be modified.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dereference ¶
Dereference dereferences the given string pointer and returns the string value it points to.
The function accepts optional settings of type Variadic that configure the behavior of dereferencing. If the pointer is nil and the [Options.Log] directive is true, a warning log will be printed and an empty string will be returned.
The function returns the string value of the pointer.
Example ¶
package main import ( "fmt" "github.com/poly-gun/text" ) func main() { // initialize a string pointer of underlying value "example" pointer := text.Pointer("example") // establish variable "v" of type string v := text.Dereference(pointer, func(o *text.Options) { o.Log = true // log if the pointer is nil }) fmt.Println(v) // establish variable "v" of type string, without using functional options. // v = text.Dereference(pointer) }
Output: example
func Lowercase ¶
Lowercase will cast a string to all-lower casing.
Example ¶
package main import ( "fmt" "github.com/poly-gun/text" ) func main() { v := "Field-Name" fmt.Println(text.Lowercase(v)) }
Output: field-name
func Pointer ¶
Pointer creates a pointer to the specified string value.
The function accepts optional settings of type Variadic that configures the behavior of the pointer creation. If the Log option in the settings is true and the value is an empty string, a warning log will be printed.
The function returns a pointer to the string parameter.
Example ¶
package main import ( "fmt" "github.com/poly-gun/text" ) func main() { // create a pointer of type string with reference value: "example" pointer := text.Pointer("example", func(o *text.Options) { o.Log = true // log if the string value is an empty string }) fmt.Println(*(pointer)) // create a pointer of type string with reference value: "example", without using functional options. // pointer = text.Pointer("example") }
Output: example
func Title ¶
Title will cast a string to title casing. If no options are provided, cases.NoLower will be appended by default.
Example ¶
package main import ( "fmt" "github.com/poly-gun/text" ) func main() { // v represents a name that should be otherwise capitalized (titled) v := "jacob b. sanders" fmt.Println(text.Title(v)) }
Output: Jacob B. Sanders
Types ¶
type Options ¶
type Options struct { // Log represents an optional flag that will log when potential, unexpected behavior could occur. E.g. // when using the [Dereference] function, log a warning that the pointer was nil. Defaults to false. Log bool // Language represents the language tag used in the [Options] struct. [Pointer] defaults to [language.AmericanEnglish]. // // - See [language.Tag] for type information. Language language.Tag // Options represents an array of [cases.Option]. These are only applicable to certain casing functions. Defaults to an empty array. Options []cases.Option }
Options is the configuration structure optionally mutated via the Variadic constructor used throughout the package.
type Variadic ¶
type Variadic func(o *Options)
Variadic represents a functional constructor for the Options type. Typical callers of Variadic won't need to perform nil checks as all implementations first construct an Options reference using packaged default(s).
Example ¶
package main import ( "fmt" "github.com/poly-gun/text" ) func main() { v := text.Dereference(nil, func(o *text.Options) { o.Log = true }) pointer := text.Pointer("example") v = text.Dereference(pointer, func(o *text.Options) { o.Log = true }) fmt.Println(v) }
Output: example