Documentation ¶
Overview ¶
Package shaper is an universal string shaping library that can easily build up string transformations step by step.
Example (Output) ¶
package main import ( "fmt" "github.com/go-shaper/shaper" ) func main() { // == Using ready-made filters // Construct pipelines UpCase := shaper.NewShaper().ApplyToUpper() LCase := shaper.NewShaper().ApplyToLower() Replace := shaper.NewShaper().ApplyReplace("test", "biscuit", -1) // Test pipelines fmt.Printf("%s\n", UpCase.Process("This is a test.")) fmt.Printf("%s\n", LCase.Process("This is a test.")) fmt.Printf("%s\n", Replace.Process("This is a test.")) // Demonstrating copy // to retain the `Replace` functionality but have another filter building // on top of it. Note: without the `Copy()`, the next call to Replace will // be affected by the ToUpper RU := Replace.Copy().ApplyToUpper() fmt.Printf("%s\n", RU.Process("This is a test.")) // Note that we can reuse these stacks as many times as we like fmt.Printf("%s\n", Replace.Process("This is also a test. Testificate.")) // We can also add stages later on - though we cannot remove stages using this style Replace.ApplyToUpper() fmt.Printf("%s\n", Replace.Process("This is also a test. Testificate.")) LCase.ApplyReplace("test", "biscuit", -1) fmt.Printf("%s\n", LCase.Process("This is also a test. Testificate.")) // Regexp.ReplaceAll RegReplace := shaper.NewShaper().ApplyRegexpReplaceAll("(?i)ht(ml)", "X$1") fmt.Printf("%s\n", RegReplace.Process("This is html Html HTML.")) // Test trim spTrim := shaper.NewShaper().ApplyTrim() stFrom := " \t\n a long \t lone\t gopher \n\t\r\n" stTo := spTrim.Process(stFrom) fmt.Printf("F: %q\nT: %q\n", stFrom, stTo) spTrim.ApplyRegSpaces() stTo = spTrim.Process(stFrom) fmt.Printf("R: %q\n", stTo) // Test ProcessAny fmt.Printf("%s\n", LCase.ProcessAny("This is also a test. Testificate.")) fmt.Printf("%s\n", LCase.ProcessAny([]string{"This is also a test.", " Testificate."})) // == All done. fmt.Printf("Finished.\n") }
Output: THIS IS A TEST. this is a test. This is a biscuit. THIS IS A BISCUIT. This is also a biscuit. Testificate. THIS IS ALSO A BISCUIT. TESTIFICATE. this is also a biscuit. biscuitificate. This is Xml Xml XML. F: " \t\n a long \t lone\t gopher \n\t\r\n" T: "a long \t lone\t gopher" R: "a long lone gopher" this is also a biscuit. biscuitificate. this is also a biscuit. biscuitificate. Finished.
Index ¶
- func PassThrough(s string) string
- type Shaper
- func (shaper *Shaper) AddShaper(f func(string) string) *Shaper
- func (shaper *Shaper) ApplyRegSpaces() *Shaper
- func (shaper *Shaper) ApplyRegexpReplaceAll(rexp, repl string) *Shaper
- func (shaper *Shaper) ApplyReplace(old, new string, times int) *Shaper
- func (shaper *Shaper) ApplyTitle() *Shaper
- func (shaper *Shaper) ApplyToLower() *Shaper
- func (shaper *Shaper) ApplyToUpper() *Shaper
- func (shaper *Shaper) ApplyTrim() *Shaper
- func (shaper *Shaper) Copy() *Shaper
- func (shaper *Shaper) NoOp() *Shaper
- func (shaper *Shaper) Process(s string) string
- func (shaper *Shaper) ProcessAny(si interface{}) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PassThrough ¶
PassThrough will return the string as-is, used primarily for NewShaper()
Types ¶
type Shaper ¶
Shaper struct holds the filter chain
Example ¶
package main import ( "fmt" "html" "github.com/go-shaper/shaper" ) // Shaper extends shaper.Shaper type Shaper struct { *shaper.Shaper } // NewShaper makes a new Shaper filter func NewShaper() *Shaper { return &Shaper{Shaper: shaper.NewShaper()} } // ApplyHTMLUnescape will apply/add to html.UnescapeString filter to the Shaper func (shpr *Shaper) ApplyHTMLUnescape() *Shaper { shpr.AddShaper(html.UnescapeString) return shpr } func main() { // == Extending shaper.Shaper to add your own filters var hu *Shaper hu = NewShaper() hu.ApplyToUpper() fmt.Printf("%s\n", hu.Process("2 >= 1")) hu.ApplyHTMLUnescape() fmt.Printf("%s\n", hu.Process("2 >= 1")) }
Output: 2 >= 1 2 >= 1
func (*Shaper) ApplyRegSpaces ¶
ApplyRegSpaces will apply/add the regular-spaces filter to the Shaper to consolidate the string to be single-space delimited
func (*Shaper) ApplyRegexpReplaceAll ¶
ApplyRegexpReplaceAll will apply/add the regexp.ReplaceAllString filter to the Shaper
func (*Shaper) ApplyReplace ¶
ApplyReplace will apply/add the strings.Replace filter to the Shaper
func (*Shaper) ApplyTitle ¶
ApplyTitle will apply/add the strings.Title filter to the Shaper
func (*Shaper) ApplyToLower ¶
ApplyToLower will apply/add the strings.ToLower filter to the Shaper
func (*Shaper) ApplyToUpper ¶
ApplyToUpper will apply/add the strings.ToUpper filter to the Shaper
func (*Shaper) ApplyTrim ¶
ApplyTrim will apply/add the strings.TrimSpace filter to the Shaper to trim all leading and trailing white spaces
func (*Shaper) Copy ¶
Copy returns a copy of the original object, instead of editing in-place, so make sure you've already got a reference to the original This should NEVER be hung off of a NewShaper string, or the original NewShaper will be lost
func (*Shaper) NoOp ¶
NoOp will do nothing. It is used to convert the extended class back to the base class.
func (*Shaper) ProcessAny ¶
ProcessAny will do the actual processing using the built-up filter chain on the given `interface{}` type input