Documentation ¶
Index ¶
- Variables
- func Divide(original string, separator string) (string, string)
- type Divider
- type DividerFunc
- type Joiner
- type JoinerFunc
- type Processor
- type ProcessorFunc
- func If(validator Validator, succeed Processor, failure Processor) ProcessorFunc
- func NewAppender(suffix string) ProcessorFunc
- func NewChain(processors ...Processor) ProcessorFunc
- func NewConditional(p Processor, alternative ...Processor) ProcessorFunc
- func NewExclusivePrepender(prefix string) ProcessorFunc
- func NewPrefixRemover(prefix string) ProcessorFunc
- func NewPrepender(prefix string) ProcessorFunc
- func NewRange(begin, end int) ProcessorFunc
- func NewRangeBegin(begin int) ProcessorFunc
- func NewRangeEnd(end int) ProcessorFunc
- func NewReplacer(replacements map[string]string) ProcessorFunc
- func NewSuffixRemover(suffix string) ProcessorFunc
- func NewTargetedJoiner(expectedIndex int, joinerChar byte) ProcessorFunc
- type Processors
- type Validator
- type ValidatorFunc
Constants ¶
This section is empty.
Variables ¶
var ClearProcessor = ProcessorFunc(func(string) string {
return ""
})
ClearProcessor returns a new string processor which always returns empty string back.
It can be used as a parameter to the library's functions.
var IsMail = NewMatcher(mailExpression)
IsMail returns a validator which returns true and a nil error if the receiver string is an e-mail.
var Logger = logger.NewDev()
Logger prints only errors that should "panic" (I don't like to call panic inside packages).
The user can assign this variable and change it in order to meet his project's meetings, Logger is just a func which accepts a string (func(string)).
To disable the logger assign the unis.Logger to a new logger.NewProd() from "/logger" package.
var OriginProcessor = ProcessorFunc(func(original string) string {
return original
})
OriginProcessor returns a new string processor which always returns the "original" string back. It does nothing.
It can be used as a parameter to the library's functions.
Functions ¶
Types ¶
type Divider ¶
type Divider interface { // Divide takes a string "original" and splits it into two pieces. Divide(original string) (part1 string, part2 string) }
Divider should be implemented by all string dividers.
type DividerFunc ¶
DividerFunc is the alias type of Divider, it implements the Divider also.
func NewDivider ¶
func NewDivider(separator string) DividerFunc
NewDivider returns a new divider which splits a string into two pieces, based on the "separator".
On failure returns the original path as its first return value, and empty as it's second.
func NewInvertOnFailureDivider ¶
func NewInvertOnFailureDivider(divider Divider) DividerFunc
NewInvertOnFailureDivider accepts a Divider "divider" and returns a new one.
It calls the previous "divider" if succed then it returns the result as it is, otherwise it inverts the order of the result.
Rembmer: the "divider" by its nature, returns the original string and empty as second parameter if the divide action has being a failure.
type Joiner ¶
type Joiner interface { // Join takes two pieces of strings // and returns a result of them, as one. Join(part1 string, part2 string) string }
Joiner should be implemented by all string joiners.
type JoinerFunc ¶
JoinerFunc is the alias type of Joiner, it implements the Joiner also.
func NewJoiner ¶
func NewJoiner(jointer string) JoinerFunc
NewJoiner returns a new joiner which joins two strings into one string, based on a "jointer".
func NewJoinerChain ¶
func NewJoinerChain(joiner Joiner, processors ...Processor) JoinerFunc
NewJoinerChain takes a Joiner and a chain of Processors and joins the Processors onto the output of the Joiner.
func (JoinerFunc) Join ¶
func (j JoinerFunc) Join(part1, part2 string) string
Join takes two pieces of strings and returns a result of them, as one.
type Processor ¶
type Processor interface { // Process accepts an "original" string and returns a result based on that. Process(original string) (result string) }
Processor is the most important interface of this package.
It's being used to implement basic string processors. Users can use all these processors to build more on their packages.
A Processor should change the "original" and returns its result based on that.
type ProcessorFunc ¶
ProcessorFunc same as Processor, as func. Implements the Processor.
func If ¶
func If(validator Validator, succeed Processor, failure Processor) ProcessorFunc
If receives a "validator" Validator and two Processors, the first processor will be called when that validator passed, the second processor will be called when the validator failed. Both of the processors ("succeed" and "failure"), as always, can be results of .NewChain.
Returns a new string processor which checks the "validator" against the "original" string, if passed then it runs the "succeed", otherwise it runs the "failure".
Remember: it returns a ProcessorFunc, meaning that can be used in a new chain too.
func NewAppender ¶
func NewAppender(suffix string) ProcessorFunc
NewAppender accepts a "suffix" and returns a new processor which returns the result appended with that "suffix".
func NewChain ¶
func NewChain(processors ...Processor) ProcessorFunc
NewChain returns a new chain of processors the result of the first goes to the second and so on.
func NewConditional ¶
func NewConditional(p Processor, alternative ...Processor) ProcessorFunc
NewConditional runs the 'p' processor, if the string didn't changed then it assumes that that processor has being a failure and it returns a Chain of the 'alternative' processor(s).
func NewExclusivePrepender ¶
func NewExclusivePrepender(prefix string) ProcessorFunc
NewExclusivePrepender accepts a "prefix" and returns a new processor which returns the result prepended with that "prefix" if the "original"'s prefix != prefix. The difference from NewPrepender is that this processor will make sure that the prefix is that "prefix" series of characters, i.e:
- "//path" -> NewPrepender("/") |> "//path" It has a prefix already, so it doesn't prepends the "/" to the "//path", but it doesn't checks if that is the correct prefix.
- "//path" -> NewExclusivePrepender("/") |> "/path" Checks if that is the correct prefix, if so returns as it's, otherwise replace the duplications and prepend the correct prefix.
func NewPrefixRemover ¶
func NewPrefixRemover(prefix string) ProcessorFunc
NewPrefixRemover accepts a "prefix" and returns a new processor which returns the result without that "prefix".
func NewPrepender ¶
func NewPrepender(prefix string) ProcessorFunc
NewPrepender accepts a "prefix" and returns a new processor which returns the result prepended with that "prefix" if the "original"'s prefix != prefix.
func NewRange ¶
func NewRange(begin, end int) ProcessorFunc
NewRange accepts "begin" and "end" indexes. Returns a new processor which tries to return the "original[begin:end]".
func NewRangeBegin ¶
func NewRangeBegin(begin int) ProcessorFunc
NewRangeBegin almost same as NewRange but it accepts only a "begin" index, that means that it assumes that the "end" index is the last of the "original" string.
Returns the "original[begin:]".
func NewRangeEnd ¶
func NewRangeEnd(end int) ProcessorFunc
NewRangeEnd almost same as NewRange but it accepts only an "end" index, that means that it assumes that the "start" index is 0 of the "original".
Returns the "original[0:end]".
func NewReplacer ¶
func NewReplacer(replacements map[string]string) ProcessorFunc
NewReplacer accepts a map of old and new string values. The "old" will be replaced with the "new" one.
Same as for loop with a strings.Replace("original", old, new, -1).
func NewSuffixRemover ¶
func NewSuffixRemover(suffix string) ProcessorFunc
NewSuffixRemover accepts a "suffix" and returns a new processor which returns the result without that "suffix".
func NewTargetedJoiner ¶
func NewTargetedJoiner(expectedIndex int, joinerChar byte) ProcessorFunc
NewTargetedJoiner accepts an "expectedIndex" as int and a "joinerChar" as byte and returns a new processor which returns the result concated with that "joinerChar" if the "original" string[expectedIndex] != joinerChar.
i.e: 1. "path", NewTargetedJoiner(0, '/') |> "/path" 2. "path/anything", NewTargetedJoiner(5, '*') |> "path/*anything".
func (ProcessorFunc) Process ¶
func (p ProcessorFunc) Process(original string) (result string)
Process accepts an "original" string and returns a result based on that.
type Validator ¶
Validator is just another interface for string utilities. All validators should implement this interface. Contains only one function "Valid" which accepts a string and returns a boolean and an error. It should compare that string "str" with something and returns a true, nil or false, err.
Validators can be used side by side with Processors.
See .If for more.
type ValidatorFunc ¶
ValidatorFunc is just an "alias" for the Validator interface. It implements the Validator.
func NewMatcher ¶
func NewMatcher(expression string) ValidatorFunc
NewMatcher returns a new validator which returns true and a nil error if the "expression" matches against a receiver string.