unis: github.com/esemplastic/unis Index | Files | Directories

package unis

import "github.com/esemplastic/unis"

Index

Package Files

chain_processor.go conditional_processor.go divider.go doc.go expression_validator.go joiner.go joiner_processor.go prefix_processor.go processor.go range_processor.go replacer_processor.go suffix_processor.go unis.go unis_test_.go validator.go

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.

func Divide Uses

func Divide(original string, separator string) (string, string)

Divide is an action which runs a new divider based on the "separator" and the "original" string.

type Divider Uses

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 Uses

type DividerFunc func(original string) (string, string)

DividerFunc is the alias type of Divider, it implements the Divider also.

func NewDivider Uses

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 Uses

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.

func (DividerFunc) Divide Uses

func (d DividerFunc) Divide(original string) (string, string)

Divide takes a string "original" and splits it into two pieces.

type Joiner Uses

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 Uses

type JoinerFunc func(part1, part2 string) string

JoinerFunc is the alias type of Joiner, it implements the Joiner also.

func NewJoiner Uses

func NewJoiner(jointer string) JoinerFunc

NewJoiner returns a new joiner which joins two strings into one string, based on a "jointer".

func NewJoinerChain Uses

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 Uses

func (j JoinerFunc) Join(part1, part2 string) string

Join takes two pieces of strings and returns a result of them, as one.

type Processor Uses

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 Uses

type ProcessorFunc func(string) string

ProcessorFunc same as Processor, as func. Implements the Processor.

func If Uses

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 Uses

func NewAppender(suffix string) ProcessorFunc

NewAppender accepts a "suffix" and returns a new processor which returns the result appended with that "suffix".

func NewChain Uses

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 Uses

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 Uses

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: 1. "//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.

1. "//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 Uses

func NewPrefixRemover(prefix string) ProcessorFunc

NewPrefixRemover accepts a "prefix" and returns a new processor which returns the result without that "prefix".

func NewPrepender Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

func NewSuffixRemover(suffix string) ProcessorFunc

NewSuffixRemover accepts a "suffix" and returns a new processor which returns the result without that "suffix".

func NewTargetedJoiner Uses

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 Uses

func (p ProcessorFunc) Process(original string) (result string)

Process accepts an "original" string and returns a result based on that.

type Processors Uses

type Processors []Processor

Processors is a list of string Processor.

type Validator Uses

type Validator interface {
    Valid(str string) (ok bool, err error)
}

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 Uses

type ValidatorFunc func(str string) (bool, error)

ValidatorFunc is just an "alias" for the Validator interface. It implements the Validator.

func NewMatcher Uses

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.

func (ValidatorFunc) Valid Uses

func (v ValidatorFunc) Valid(str string) (bool, error)

Valid 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.

Directories

PathSynopsis
logger

Package unis imports 6 packages (graph) and is imported by 1 packages. Updated 2017-05-10. Refresh now. Tools for package owners.