shaper

package module
v0.0.0-...-a16abec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2017 License: MIT Imports: 2 Imported by: 1

README

shaper

MIT License GoDoc Go Report Card travis Status codeship Status

TOC

shaper - shape strings into desired forms

The shaper project provides general purpose building blocks to shape strings into desired forms. The cumulative building blocks can easily be chained together. They are used like building pipes in shell.

Architected by Howard C. Shaw III, it needs neither go-routines nor channels. Instead, it creates a composable set of filters you could keep around and apply to a single string at a time. Behind the scenes, it is still building a composition of functions, but by currying that composition into a new function and holding the state of the stack in a struct, it restores the left-right ordering of the filters. Note that the filter stages are basically just compile-time freezes of a call to the currying function; i.e., no matter how complicated the shaping filter chain is, all are done at the compile time, so no run-time overhead when using shaper.

API

Check out the provided examples to see how to use it with the ready-made filters, and the Go Doc document for further details.

To extend and add your own filters take a look at func ExampleShaper in:

> shaper_test.go
package shaper_test

import (
	"fmt"
	"html"
)

import (
	"github.com/go-shaper/shaper"
)

////////////////////////////////////////////////////////////////////////////
// How to use it with the ready-made filters

func Example_output() {
	// == 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.

}

////////////////////////////////////////////////////////////////////////////
// Extending 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 ExampleShaper() {
	// == 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
}

All patches welcome.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PassThrough

func PassThrough(s string) string

PassThrough will return the string as-is, used primarily for NewShaper()

Types

type Shaper

type Shaper struct {
	ShaperStack func(string) string
}

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 NewShaper

func NewShaper() *Shaper

NewShaper makes a new Shaper filter

func (*Shaper) AddShaper

func (shaper *Shaper) AddShaper(f func(string) string) *Shaper

AddShaper is used to apply arbitrary filters

func (*Shaper) ApplyRegSpaces

func (shaper *Shaper) ApplyRegSpaces() *Shaper

ApplyRegSpaces will apply/add the regular-spaces filter to the Shaper to consolidate the string to be single-space delimited

func (*Shaper) ApplyRegexpReplaceAll

func (shaper *Shaper) ApplyRegexpReplaceAll(rexp, repl string) *Shaper

ApplyRegexpReplaceAll will apply/add the regexp.ReplaceAllString filter to the Shaper

func (*Shaper) ApplyReplace

func (shaper *Shaper) ApplyReplace(old, new string, times int) *Shaper

ApplyReplace will apply/add the strings.Replace filter to the Shaper

func (*Shaper) ApplyTitle

func (shaper *Shaper) ApplyTitle() *Shaper

ApplyTitle will apply/add the strings.Title filter to the Shaper

func (*Shaper) ApplyToLower

func (shaper *Shaper) ApplyToLower() *Shaper

ApplyToLower will apply/add the strings.ToLower filter to the Shaper

func (*Shaper) ApplyToUpper

func (shaper *Shaper) ApplyToUpper() *Shaper

ApplyToUpper will apply/add the strings.ToUpper filter to the Shaper

func (*Shaper) ApplyTrim

func (shaper *Shaper) ApplyTrim() *Shaper

ApplyTrim will apply/add the strings.TrimSpace filter to the Shaper to trim all leading and trailing white spaces

func (*Shaper) Copy

func (shaper *Shaper) Copy() *Shaper

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

func (shaper *Shaper) NoOp() *Shaper

NoOp will do nothing. It is used to convert the extended class back to the base class.

func (*Shaper) Process

func (shaper *Shaper) Process(s string) string

Process will actually process a string using the built-up filter chain

func (*Shaper) ProcessAny

func (shaper *Shaper) ProcessAny(si interface{}) string

ProcessAny will do the actual processing using the built-up filter chain on the given `interface{}` type input

Directories

Path Synopsis
Package shpHTML provides string shaping functionalities for HTML.
Package shpHTML provides string shaping functionalities for HTML.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL